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;
}
n1toint niter, n, i = 0, j = 0, k = 0, n1 = n + 1;whilenis still indeterminate. You ought to setn1 = n+1;after you read inn.malloc().malloc()cast? (or was it edited out?)