Thread: Corrupted double-linked list error

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    Corrupted double-linked list error

    I call a file with the list of months from January-December and store them in a structure of its own.

    Then I'm wanting to allocate a 2d array so I can store the counts of the months for each year seen in the next file I'm going to open.

    Anyways I'm having issues with this part of my code i believe...


    The point of this is to initialize my 2d array of ints
    Code:
        int **array_of_years = malloc( 11* sizeof(**array_of_years));
    The point of this is to give my 2d array of ints enough space to store a count for each month for the years 2000-2009.
    I use calloc because I want the memory to have the value of 0 so I can just up the counts as I go.
    Code:
           for(i = 0; i < counter1; i++)
         array_of_years[i] = calloc(10 , sizeof(**array_of_years));
    I have the program print Hi, I'm alive to test it out. Anyways this works on Dev-C, but when I try to run this on gcc it gives me the corrupted double-linked list error.

    Reading up on this it looks like I probably overlapped the memory or something along those lines? Not familiar with this error at all. Any help to fix this would be greatly appreciated.

    I stripped a lot of my code out to try to find out the source of the problem which is why I think it's the code I've given above.

    Anyways I'll post the rest of the code just to show you what I have.
    Thanks

    Also this must be dynamically allocated for the counts.
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    struct struct_of_months
    {
           char months[20];
    };
    
    
    int main(void)
    {
        FILE *fp;
        char buffer[50];
        int counter1 = 0;
        char *p;
        int i;
        struct struct_of_months *array_of_months = malloc (sizeof(*array_of_months));
        int **array_of_years = malloc( 11* sizeof(**array_of_years));
    
    //Opens the first file and stores the months in array of structures
        if ( (fp = fopen("months.txt", "r" )) == NULL )
        {
        printf("Couldn't open file\n");
        exit(1); 
        }
        while(fgets(buffer, sizeof(buffer), fp))
        {
            if (counter1>0)
               array_of_months=realloc(array_of_months,(counter1+1)*sizeof(*array_of_months)); 
            if ((p = strchr(buffer, '\n')) != NULL)
               *p = '\0';
            strcpy(array_of_months[counter1].months, buffer);  
            counter1++;         
        }   
    fclose( fp );
    
    //Supposed to allocate enough memory for the 2d array.
           for(i = 0; i < counter1; i++)
            array_of_years[i] = calloc(10 , sizeof(**array_of_years));
              printf("Hi, I'm alive\n");
                                        
    free(array_of_months);
    free(array_of_years);
    getchar();
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct struct_of_months *array_of_months = malloc (sizeof(*array_of_months));
    That gives you 1 'struct struct_of_months'.
    Code:
        int **array_of_years = malloc( 11* sizeof(**array_of_years));
    That gives you 11 ints.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Oops I just changed that to...


    Code:
        int **array_of_years = malloc(sizeof(**array_of_years));
    I meant to have it simply initialize the 2d array. Not give 11 ints. Is that even the right way to initialize a 2d array of ints that I have to dynamically allocate?

    The structure part is right. I just needed one structure initialized then while grabbing the file below I just reallocated more memory as I needed it. So I think that is correct.

    But even with taking the 11 out it still gives me the same error. Could it be with how I initialize the 2d array_of_years? or how I use calloc on it? or free it?

    because when I take this part out....

    Code:
       	for(i = 0; i < counter1; i++)
         array_of_years[i] = calloc(10 , sizeof(**array_of_years));
    The error isn't there anymore.
    Leads me to believe I messed up somehow with the calloc.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Found out what the problem was.

    2 problems actually

    1.) i wasn't allocating enough memory for the array of years.
    2.) I also wasn't freeing the memory of that array correctly. I put in a for loop to free the memory in the arrays, then I freed the arrays themselves.

    Finally getting the hang of it

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Right, so this thread has nothing to do with doubly-linked lists...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    That was just the error I was getting...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double linked list
    By marlaFC in forum C Programming
    Replies: 6
    Last Post: 11-19-2011, 08:54 PM
  2. Double-linked list error in image smoothing program
    By mwhar in forum C Programming
    Replies: 4
    Last Post: 04-27-2011, 06:56 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. glibc: corrupted double-linked list
    By Sale in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2006, 08:21 AM
  5. Double linked list error, help!
    By raell in forum C Programming
    Replies: 10
    Last Post: 03-07-2004, 06:28 PM