freeing dynamic 2D array

This is a discussion on freeing dynamic 2D array within the C Programming forums, part of the General Programming Boards category; Hi! I'm having problems freeing the Button[i].Items variable. Please take a look at the code. Code: typedef struct BUTTON { ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    freeing dynamic 2D array

    Hi!

    I'm having problems freeing the Button[i].Items variable. Please take a look at the code.

    Code:
    typedef struct BUTTON
    {
    	short NoOfItems;
    	char **Items;
    } BUTTON_COMPONENT;
    
    .
    .
    .
    
    
    for (i = 0; i < 3; i++)
    {
    if ((Button[i].Items = (char **) malloc (sizeof (char) * Button[i].NoOfItems)) == NULL)
    {
    	ErrorMsg ("Button[i].Items is null!");
    }
    else
    {
    	if ((Button[i].Items[0] = (char *) malloc (sizeof (char) * (strlen ("First Button") + 1))) == NULL)
    	{
    		ErrorMsg ("Button[i].Items[0] is null!");
    	}
    	if ((Button[i].Items[1] = (char *) malloc (sizeof (char) * (strlen ("Second Button") + 1))) == NULL)
    	{
             	ErrorMsg ("Button[i].Items[1] is null!");
    	}
    	if ((Button[i].Items[2] = (char *) malloc (sizeof (char) * (strlen ("Third Button") + 1))) == NULL)
    	{
    		ErrorMsg ("Button[i].Items[2] is null!");
    	}
    }
    
    // do stuff
    
    for (j = 0; j < Button[i].NoOfItems; j++)
    {
        free (Button[i].Items[j]);
        Button[i].Items[j] = NULL;
    }
    free (Button[i].Items); // here is where the program hangs. Can someone please tell me WHY?
    Button[i].Items = NULL;
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Deity
    Join Date
    Jun 2003
    Location
    Iceland
    Posts
    11

    Lightbulb

    How about this:
    Code:
    free (Button->Items); // not sure if this works
    free (Button); // this should work just fine, but frees the entire struct rather than just 'Items'
    Chaos, panic, pandemonium... My work here is done!

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    >free (Button);

    This is working just fine but this will not free entire structure. If you have pointers inside your structure you must free them first and than you can free the structure.


    But, I think I found a problem.

    Code:
    if ((Button[i].Items = (char **) malloc (sizeof (char) * Button[i].NoOfItems)) == NULL)
    {
    	ErrorMsg ("Button[i].Items is null!");
    }
    
    The problem is in that sizeof (char). Now I  determine the size of char * (sizeof (char *)).
    What do you folks think about that. Is this right?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Uuu, didn't see that post from Salem.

    Thanks Salem.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. put matrix from file into 2d dynamic array
    By meriororen in forum C Programming
    Replies: 3
    Last Post: 06-08-2009, 08:51 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. dynamic 2D array allocation
    By deprekate in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 04:25 AM
  4. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  5. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21