1

I am writing this C code which takes in a file and reads in values from it, the code doesn't do anything yet, but this is what I have so far. The program is crashing in the block that is calling four mallocs. The program works fine if I comment out y, f, and yp. I don't know what it is causing it. So any help will be appreciated.

Note: I am testing this on ubuntu with gcc. And I did trying casting the malloc to "(float *)" but I still get the same error.

int main( int argc, char *argv[])
{
    FILE *rhs, *output;
    int niter, n, i = 0, j = 0, k = 0, n1 = n + 1;

    rhs = fopen(argv[1], "r");
    // ab+ opens file for writting and creates the file if need be
    output = fopen(argv[2], "ab+");
    niter = atoi(argv[3]);

    // check if files open up or not, if not exit.
    if((rhs == NULL) || (output == NULL))
    {
        printf("Error Opening files.\n");
        exit(1);
    }

    // read in N
    fscanf(rhs, "%d", &n);

    // THIS IS THE BLOCK CAUSING THE CRASH
    // CODE WORKS WHEN I COMMENT OUT LINES AND ONLY LEAVE ONE OF THEM IN
    // generate array to hold values from rhs file
    float *numbers = malloc(sizeof(float) * ((n1)*(n1)));
    float *y = malloc(sizeof(float) * ((n1)*(n1)));
    float *f = malloc(sizeof(float) * ((n1)*(n1)));
    float *yp = malloc(sizeof(float) * ((n1)*(n1)));

    // get numbers and store into array
    while(fscanf(rhs, "%f", &numbers[i]) != EOF)
    {
        printf("In while %f\n", numbers[i]);
        i++;
    }

    fclose(rhs);

    return 0;

}

7
  • 1
    You initialise n1 to int niter, n, i = 0, j = 0, k = 0, n1 = n + 1; while n is still indeterminate. You ought to set n1 = n+1; after you read in n. Commented Mar 19, 2013 at 8:27
  • @DanielFischer: That should be an answer, not a comment. Commented Mar 19, 2013 at 8:27
  • You shouldn't cast the result of malloc(). Commented Mar 19, 2013 at 8:29
  • @DietrichEpp where is there a malloc() cast? (or was it edited out?) Commented Mar 19, 2013 at 8:31
  • Yup, that fixed it. Thanks! -- the n1 not being initialized correctly. Commented Mar 19, 2013 at 8:31

4 Answers 4

2

One issue is:

You are initializing n1 with an uninitialized value from n:

int niter, n, i = 0, j = 0, k = 0, n1 = n + 1;
                                        ^
                                        +-- "n" is not initialized here, might have any value.
                                            thus, "n1" is also not initialized to a known value.

Therefore, your call to malloc most likely recieves a too large value to be allocated at all. Initialize "n1" after you have read "n":

// read in N
fscanf(rhs, "%d", &n);
n1 = n + 1;

In any case, it is worth checking the return value from malloc() to see if it returned NULL in case the memory could not be allocated.

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

2 Comments

That fixed it. Sigh...can't believe I didn't catch that...thanks for the help, really appreciate it. :)
No issue - good to hear that this was really the primary problem :)
1

The n1 contains a garbage at the moment of the malloc() call. Thus you simply try to allocate a HUGE amount of memory.

Comments

0

Check whether memory has been allocated successfully or not by checking the value of numbers, y, f, yp, if their value is NULL, then memory can't be allocated. How large is the value of n1?

1 Comment

"How large is the value of n1" : cannot be determined. It is n+1, but as originally presented the OP's code never initializes n, therefore its usage as an rvalue is indeterminate and utterly unknown; thus so is n1.
0

It is because n1 is undefined at the point you call malloc().

int niter, n, i = 0, j = 0, k = 0, n1 = n + 1;

The above line didn't initialize n, so n1 = n + 1 assigns an undefined value to n1.

You may need to put n1 = n + 1; after fscanf(rhs, "%d", &n);.

Comments

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.