title: glibc corrupted double linked list error while running a C code on 64 bit machine
body: <p>I have a large C code that works fine on 32 bit machine but fails on 64 bit machines with </p>
  
  <p><code>*** glibc detected *** ./XXX.exe: corrupted double-linked list:</code> error. </p>
  
  <p>I have run Valgrind and have used print statements to exactly pinpoint where the error is coming from but am clueless about the fix. 
  The actual code is very big but I am giving the relevant portions here. Hoping someone can help me troubleshoot it.
  The actual error is coming from the <code>Buf_Close()</code> module where it tries to free <code>Buf_elm_p p</code> as <code>if (p != NULL) free(p);</code></p>
  
  <p>These are functions that the main code calls and I am only giving this here as the error is somewhere here. The calling sequence from the main code is: </p>
  
  <p><code>1. Buf_Init
  2. Buf_New_p
  3. Buf_Close</code></p>
  
  <pre><code>Buf_bf_p 
  Buf_Init( size_t elm_size, 
        int    nelm_buf, 
        long   nbuf )
  /*******************************************************************************
  !C
  !Description: Buf_Init (buffer initialize) initializes buffer data structures 
   and buffers.
  
  !Input Parameters:
   elm_size   element size (bytes)
   nelm_buf   number of elements per buffer
   nbuf       number of buffers
  
  !Output Parameters:
   (returns)      pointer to the buffer data structure 
                  or NULL for an error return
  *****************************************************************************/
  {
    Buf_bf_p bf;
    long buf_size;
    long ibuf;
  
    /* Calculate buffer size and check */
    buf_size = ((long) elm_size) * nelm_buf;
  
    /*Allocate the buffer data structure */
    if ((bf = (Buf_bf_p) malloc(sizeof(Buf_bf_t))) == NULL)
      {Buf_Error(&amp;BUF_NOMEMORY, "Init");  return NULL;}
    bf-&gt;key = BUF_KEY;
    bf-&gt;elm_size = elm_size;
    bf-&gt;nelm_buf = nelm_buf;
    bf-&gt;nbuf = nbuf;
    bf-&gt;buf_size = buf_size;
    bf-&gt;fp = NULL;
    bf-&gt;access = NO_FILE;
    bf-&gt;nbuf_alloc = 1;
    bf-&gt;ibuf_end = 0;
    bf-&gt;ibuf_newest = 0;
    bf-&gt;ibuf_oldest = 0;
    bf-&gt;nelm = 0;
  
    /* Allocate the buffer status data structure */
    bf-&gt;nbstat = max(NBSTAT_START, bf-&gt;nbuf + 1);
    if ((bf-&gt;bstat = (Buf_bstat_t *) 
       malloc(bf-&gt;nbstat * sizeof(Buf_bstat_t))) == NULL)
      {Buf_Error(&amp;BUF_NOMEMORY, "Init");  return NULL;}
  
    /* Allocate the first buffer */
    bf-&gt;bstat[0].loc = MEM_ONLY;
    if( (bf-&gt;bstat[0].buf_p = (Buf_elm_p) malloc(bf-&gt;buf_size)) == NULL)
    { Buf_Error(&amp;BUF_NOMEMORY, "Init");  
      return NULL;
    }
    else
    {
       /* initialize */
       memset( bf-&gt;bstat[0].buf_p, '\0', bf-&gt;buf_size ); 
    }
  
    bf-&gt;bstat[0].newer = -1;
    bf-&gt;bstat[0].older = -1;
  
    /* Initialize the rest of the buffer status array */
    printf("bf-&gt;nbstat %d\n", bf-&gt;nbstat);
    for (ibuf = 1; ibuf &lt; bf-&gt;nbstat; ibuf++) {
      bf-&gt;bstat[ibuf].loc = NOT_ALLOC;
      bf-&gt;bstat[ibuf].buf_p = NULL;
      bf-&gt;bstat[ibuf].newer = -1;
      bf-&gt;bstat[ibuf].older = -1;
      bf-&gt;bstat[ibuf].initialized = 1;
    }
  
    return bf;
  }
  
  Buf_elm_p 
  Buf_New_p( Buf_bf_p bf, 
             long *ielm )
  /*******************************************************************************
  !C
  !Description: Buf_New_p (new buffer element pointer) returns a memory 
   location and element number of a new element; elements are number
   sequentially as they are allocated.
  
  !Input Parameters:
   bf     pointer to the buffer data structure 
  
  !Output Parameters:
   ielm       new element number
   (returns)      pointer to memory location of new element 
                  or NULL for error
  !Notes:
     1. 'Buf_Init' must be called before this routine to initialize 
        the buffer data structure.
     2. If there is no more space in memory and disk write access is allowed, 
        the oldest buffer is written to disk and the memory is re-used.
     3. If the file is opened with 'read only' access this routine will return 
        an error.
  
  !END
  ******************************************************************************/
  {
    long ibuf, jelm, jbuf, kbuf;
    long nbuf_cmplt;
    Buf_elm_p p;
    long dsk_loc, eof_loc;
  
  
    /* New element number/location */
    *ielm = bf-&gt;nelm++;
    ibuf = *ielm / bf-&gt;nelm_buf;
    jelm = *ielm % bf-&gt;nelm_buf;
  
    /* Are we at the past the end of the last buffer? */
    if (ibuf &gt; bf-&gt;ibuf_end) {
  
      if (ibuf != (bf-&gt;ibuf_end + 1))
        {Buf_Error(&amp;BUF_BADBUF, "New_p");  return NULL;}
  
      /* Re-allocate buffer status data structure if not large enough */
      if( ibuf &gt;= bf-&gt;nbstat ) 
      {
         bf-&gt;nbstat += min(bf-&gt;nbstat, MAX_NEW_NBSTAT);
         if( (bf-&gt;bstat = realloc(bf-&gt;bstat, bf-&gt;nbstat * sizeof(Buf_bstat_t))) 
             == NULL)
        {  Buf_Error(&amp;BUF_NOMEMORY, "New_p");  
            return NULL;
        }
      }
  
      if (bf-&gt;nbuf_alloc &lt; bf-&gt;nbuf  ||  bf-&gt;access == NO_FILE) {
  
        /* Allocate a new buffer */
        if( (p = (Buf_elm_p) malloc(bf-&gt;buf_size)) == NULL)
        {  Buf_Error(&amp;BUF_NOMEMORY, "New_p");  
           return NULL;
        }
        else
        {
           /* initialize */
           memset( p, '\0', bf-&gt;buf_size ); 
        }
        bf-&gt;nbuf_alloc++;
        if (bf-&gt;nbuf &lt; bf-&gt;nbuf_alloc) bf-&gt;nbuf = bf-&gt;nbuf_alloc;
  
      } else {
  
        /* Re-use an existing buffer */
  
        /* Get the oldest buffer */
        jbuf = bf-&gt;ibuf_oldest;
        /* Delete oldest buffer from old/new pointer list */
        p = bf-&gt;bstat[jbuf].buf_p;
        bf-&gt;ibuf_oldest = bf-&gt;bstat[jbuf].newer;
        bf-&gt;bstat[bf-&gt;ibuf_oldest].older = -1;
  
        bf-&gt;bstat[jbuf].buf_p = NULL;
        bf-&gt;bstat[jbuf].older = -1;
        bf-&gt;bstat[jbuf].newer = -1;
        bf-&gt;bstat[jbuf].initialized = 1;
  
      }
  
      /* Put current buffer in old/new pointer list */
      bf-&gt;bstat[ibuf].loc = MEM_ONLY;
      bf-&gt;bstat[ibuf].buf_p = p;
      bf-&gt;bstat[ibuf].older = bf-&gt;ibuf_newest;
      bf-&gt;bstat[ibuf].newer = -1;
      bf-&gt;bstat[ibuf].initialized = 1;
  
      bf-&gt;ibuf_end = ibuf;
      bf-&gt;bstat[bf-&gt;ibuf_newest].newer = ibuf;
      bf-&gt;ibuf_newest = ibuf;
  
    }
  
    /* Calculate pointer to memory location of element */
    p = (unsigned char *) bf-&gt;bstat[ibuf].buf_p + (jelm * bf-&gt;elm_size);
  
    return p;
  }
  
  
  int 
  Buf_Close( Buf_bf_p bf )
  /*******************************************************************************
  !C
  !Description: Buf_Close (buffer cache file close) writes the remainder of the 
   cache to the disk cache file and closes the file and frees memory of the 
   buffer data structure and buffers.
  
  !Input Parameters:
   bf     pointer to the buffer data structure 
  
   Notes:
     1. 'Buf_Create' or 'Buf_Open' must be called before this routine to open
        the file.
  
  !END
  *****************************************************************************/
  {
    int i;
    long dsk_loc;
    logical_t cmplt_flag;
    /* int b; */
    Buf_elm_p p;
    long ibuf, nelm_wrt;
    int  nb;
    unsigned char header[HEADER_SIZE];
  
    /* Write remaining buffers which are still only in memory */
    for (ibuf = 0; ibuf &lt; (bf-&gt;ibuf_end + 1); ibuf++)
    /* for (ibuf = 0; ibuf &lt; (bf-&gt;ibuf_end); ibuf++)*/{
      p = bf-&gt;bstat[ibuf].buf_p;
      /* Free the buffer memory */
  **THIS FOLLOWING LINE IS WHERE THE ERROR IS COMING FROM**
  
  **VALGRIND SHOWS `Conditional jump or move depends on uninitialised value(s)` ERROR**
  
  **BUT AM NOT SURE HOW `p` is coming out to be uninitialized`**
  
  if (p != NULL) free(p);  
  }
  
    /* Free the buffer status memory */
    free(bf-&gt;bstat);
  
    /* Free the buffer cache data structure */
    bf-&gt;fp = (FILE *)NULL;
    bf-&gt;key = 0;
    free(bf);  
  
    printf("buf here 5\n");
    return BUF_NORMAL;
  }
  </code></pre>