0

I am beginner in C and this is my first program using malloc() function. I think there might be some problem in using this function.I wanted to use an array(cyclelength) where a solution for a range of number(user input) will be put. so the array size depends on user so i used malloc(). but the program is crashing. Here is my code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x,y,num,count,p,k;
    for(;;){
        printf("enter first integer. must be between 1 and 100000\n");
        scanf("%d", &x);
        printf("enter second integer. must be between 1 and 100000. must not equal the first integer.\n");
        scanf("%d", &y);
        if(x>=1 && x<100000 && y>=1 && y<100000 && x!=y){
            break;
        }
        else{
            printf("try the whole process again\n");
        }
    }
    if (x<y){
        int j;
        j=y;
        y=x;
        x=j;
    } //making x always greater than y
    int *cyclelength=malloc(5000*sizeof(int));
    if (NULL==cyclelength){
        printf("process aborted");
    }
    else{
        /*solution part for the range of number. and solution for each number  put into cyclelength.*/
        num=y;
        while(num<=x){
            p=1;
            k=num;
            while(k!=1){
                if(k%2==0)
                    k=k/2;
                else
                    k=3*k+1;
                p+=1;
                }
            count=0;
            cyclelength[count]=p;
            num+=1;
            count+=1;
        }
        free(cyclelength);
        cyclelength=NULL;
    }
    int c=0;
    int max=cyclelength[c];
    for(;c<x-y;c+=1){
        if(max<cyclelength[c+1]){
            max=cyclelength[c+1];
        }
    }
    printf("%d,%d,%d",x,y,max);
    return 0;
}
1
  • 2
    You should post the output of the crash with the question. It will help you get a better answer faster. Commented Jul 8, 2012 at 17:45

3 Answers 3

4

You're calling free(cyclelength) and then accessing the memory it points to (or, rather, the memory that it pointed to).

(And your error handling could stand some improvement; you print "process aborted", but then continue processing.)

Sign up to request clarification or add additional context in comments.

Comments

4

You are using cyclelength after you freed it and set it to NULL:

        free(cyclelength);
        cyclelength=NULL;
    }
    int c=0;
    int max=cyclelength[c];
    for(;c<x-y;c+=1){
        if(max<cyclelength[c+1]){
            max=cyclelength[c+1];
        }

That's undefined behaviour and likely to crash.

Comments

0

You are using cyclelength after dealloating it so your program is crash.
Try this :

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int x,y,num,count,p,k;
    for(;;){
        printf("enter first integer. must be between 1 and 100000\n");
        scanf("%d", &x);
        printf("enter second integer. must be between 1 and 100000. must not equal the first integer.\n");
        scanf("%d", &y);
        if(x>=1 && x<100000 && y>=1 && y<100000 && x!=y){
            break;
        }
        else{
            printf("try the whole process again\n");
        }
    }
    if (x<y){
        int j;
        j=y;
        y=x;
        x=j;
    } //making x always greater than y
    int *cyclelength=(int *)malloc(5000*sizeof(int));
    if (NULL==cyclelength){
        printf("process aborted");
    }
    else{
        /*solution part for the range of number. and solution for each number  put into cyclelength.*/
        num=y;
        while(num<=x){
            p=1;
            k=num;
            while(k!=1){
                if(k%2==0)
                    k=k/2;
                else
                    k=3*k+1;
                p+=1;
                }
            count=0;
            cyclelength[count]=p;
            num+=1;
            count+=1;
        }        
        // don't assign null to cyclelength
        //cyclelength=NULL;
    }
    int c=0;
    int max=cyclelength[c];
    for(;c<x-y;c+=1){
        if(max<cyclelength[c+1]){
            max=cyclelength[c+1];
        }
    }
    printf("%d,%d,%d",x,y,max);
    // free here
    free(cyclelength);
    return 0;
}

2 Comments

well.. thanx everyone... i have got my problem about malloc function.. there were another problem in this code regarding declaring count variable.. that was a silly mistake. i am sorry for that.
You should also change this line int *cyclelength=malloc(5000*sizeof(int)); to int *cyclelength=(int *)malloc(5000*sizeof(int));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.