Reputation: 6068
I am writing a program in C that reads in a document of polynomial coefficients and can evaluate the roots of the polynomials.
In one of my functions I am attempting to read through the text file, and create a list of "polynomials". Polynomials are defined as follows:
typedef struct
{
unsigned int nterms; /* number of terms */
double complex *polyCoef; /* coefficients */
} polynomial;
The text files are in the following format, where each line represents a polynomial and each number represents a coefficient:
1 0 0 0 2 -1
16 70 -169 -580 75
1 0 4 0 -5
0 -9 3 5 -3
5 -4 3 -2 0
1.0 -3.4 5.4531 -4.2077 1.5092 -0.2030
I am getting some odd behavior in my implementation attempt. With the following code, I get
* Error in `./hw6': corrupted double-linked list: 0x0000000000a1c240 *
PolyFile = fopen(argv[2], "r");
if(NULL == PolyFile){ /* If the file fails to open */
fprintf(stderr, "Error: Input file '%s' not found\n", argv[2]);
return(-1);
}
/****** Read in the polynomials *************/
polynomials = malloc(sizeof(polynomial*) * size); /* Initialize */
if(polynomials == NULL){
fprintf(stderr, "%s %i: Could not allocate memory\n", __FILE__, __LINE__);
exit(-99);
}
/* Read all of the data from the file */
while(fgets(String, MAX_STR_LEN, PolyFile)) {
strLen = strlen(String); /* Determine size of line */
/* Ensure that the line is not too long */
if(strLen <= MAX_STR_LEN){
/* Create the polynomial */
p = (polynomial*)malloc(sizeof(polynomial));
token = strtok(String, " ");
while(token){
coefficients[coeffCount] = token;
token = strtok(NULL, " ");
coeffCount++;
}
createPoly(p, coeffCount);
/* Set p->polyCoef to the reverse of coefficients */
for(int lcv = 0; lcv <= coeffCount - 1; lcv++){
p->polyCoef[lcv] = atof(coefficients[coeffCount - lcv - 1]) + 0.00*I;
}
coeffCount = 0;
polynomials[size] = p;
printf("P->NTERMS: %i\n", p->nterms);
size++;
} else {
fprintf(stderr, "%s %i: Line too long. Polynomial ignored\n",
__FILE__, __LINE__);
}
}
fclose(PolyFile);
/**************************/
for(int j = 0; j <= size - 1; j++){
printf("J: %i\n", j);
printf("IN FOR LOOP: %i\n", (polynomials[j])->nterms);
// printPoly(polynomials[j]);
printf("\n");
}
. . .
/*---------------------------------------------------------------------------
Creates a polynomial data structure with nterms. This allocates storage
for the actual polynomial.
Where: polynomial *p - Pointer to polynomial data structure to create
unsigned int nterms - The number of elements to create
Returns: nothing
Errors: prints an error and exits
---------------------------------------------------------------------------*/
void createPoly(polynomial *p, unsigned int nterms){
int lcv; /* loop control variable */
/* Create a polynomial struct */
p->nterms = nterms;
p->polyCoef = (double complex*)malloc(sizeof(double complex)*nterms);
/* Error out if problem with malloc */
if(p->polyCoef == NULL){
fprintf(stderr, "%s %i:Error allocating memory\n", __FILE__, __LINE__);
exit(1);
}
/* Set the coefficients to 0 */
for(lcv = 0; lcv < nterms; lcv++){
(p->polyCoef)[lcv] = 0.00 + 0.00*I;
}
}
If I remove the fclose(PolyFile);
, the code continues, but debug printing shows that polynomials[0]->nterms
is equal to a random, changing, very large number. I have no idea why this is happening.
Upvotes: 1
Views: 4942
Reputation: 6068
The problem occurred because at the time of the initialization of polynomials
, size
was equal to 0:
polynomials = malloc(sizeof(polynomial*) * size);
This was fixed by allocating the proper size, using the number of lines in the file.
Upvotes: 1