title: Why am I getting "corrupted double-linked list" error?
body: <h1>The program</h1>
  
  <p>I am writing a program in C that reads in a document of polynomial coefficients and can evaluate the roots of the polynomials. </p>
  
  <p>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: </p>
  
  <pre><code>typedef struct
  {
      unsigned int nterms;       /* number of terms */
      double complex *polyCoef;  /* coefficients    */
  } polynomial;
  </code></pre>
  
  <p>The text files are in the following format, where each line represents a polynomial and each number represents a coefficient: </p>
  
  <pre><code>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
  </code></pre>
  
  <hr>
  
  <h1>The Problem</h1>
  
  <p>I am getting some odd behavior in my implementation attempt. 
  With the following code, I get </p>
  
  <blockquote>
    <p><strong>* Error in `./hw6': corrupted double-linked list: 0x0000000000a1c240 *</strong></p>
  </blockquote>
  
  <pre><code>    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 &lt;= 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-&gt;polyCoef to the reverse of coefficients */            
              for(int lcv = 0; lcv &lt;= coeffCount - 1; lcv++){
  
                  p-&gt;polyCoef[lcv] = atof(coefficients[coeffCount - lcv - 1]) + 0.00*I; 
              }
              coeffCount = 0; 
  
              polynomials[size] = p;
              printf("P-&gt;NTERMS: %i\n", p-&gt;nterms); 
              size++; 
  
          } else {
              fprintf(stderr, "%s %i: Line too long. Polynomial ignored\n",
                          __FILE__, __LINE__); 
          }
  
      }
      fclose(PolyFile); 
      /**************************/
      for(int j = 0; j &lt;= size - 1; j++){
          printf("J: %i\n", j); 
          printf("IN FOR LOOP: %i\n", (polynomials[j])-&gt;nterms); 
          // printPoly(polynomials[j]); 
          printf("\n"); 
      }
  </code></pre>
  
  <p>. . . </p>
  
  <pre><code>/*---------------------------------------------------------------------------
    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-&gt;nterms = nterms; 
    p-&gt;polyCoef = (double complex*)malloc(sizeof(double complex)*nterms); 
  
    /* Error out if problem with malloc */
    if(p-&gt;polyCoef == NULL){
      fprintf(stderr, "%s %i:Error allocating memory\n", __FILE__, __LINE__); 
      exit(1); 
    }
  
    /* Set the coefficients to 0 */
    for(lcv = 0; lcv &lt; nterms; lcv++){
      (p-&gt;polyCoef)[lcv] = 0.00 + 0.00*I; 
    }
  }
  </code></pre>
  
  <p>If I remove the <code>fclose(PolyFile);</code>, the code continues, but debug printing shows that <code>polynomials[0]-&gt;nterms</code> is equal to a random, changing, very large number.  I have no idea why this is happening. </p>