Thread: 2D array becoming "deallocaded"

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    Halifax, N.S.
    Posts
    4

    2D array becoming "deallocaded"

    Hello. I'm having trouble with a structure that points to a dynamically allocated 2D array. For some reason, after I allocate the array, it becomes "deallocated" in the middle of the program.

    I have a pointer to a structure which has a pointer to a 2D array. The full program gives a bus error, which happens when I try to access a cell in the structure's array. I've removed a bunch of the code that actually does stuff for this post. I can still see though where the problem is coming from through the debugger (I just have no clue why it's there!)

    The main program:
    Code:
    int main()
    {
    	Map *map_p = map_create_p(10, 10);
    	
    	Creature *player_p = creature_create_p();
    	
    	printf("This is just busywork.\n");
    	
    	return 0;
    }
    This is the problem structure:
    Code:
    struct map
    {
    	unsigned int maxRows;
    	unsigned int maxCols;
    	
    	char **tiles_p; // A 2D array
    };
    
    typedef struct map Map;
    How I create a Map structure:
    Code:
    Map *map_create_p(unsigned int maxRows, unsigned int maxCols)
    {
    	int r, c;
    	Map *map_p;
    	
    	map_p = malloc(sizeof(map_p));
    	
    	map_p->maxRows = maxRows;
    	map_p->maxCols = maxCols;
    	
    	map_p->tiles_p = calloc(maxRows, sizeof(map_p->tiles_p));
    	
    	for (r = 0; r < maxRows; r++)
    	{
    		map_p->tiles_p[r] = calloc(maxCols, sizeof(map_p->tiles[r]));
    		
    		for (c = 0; c < maxCols; c++)
    		{
    			map_p->tiles_p[r][c] = '.';
    		}
    	}
    	
    	return map_p;
    }
    When I first create a Map structure, I can access the elements of its tiles_p array fine. But after a few lines I can't access them anymore.

    This is what running it through gdb is like (with spaces added for emphasis):

    Code:
    (gdb) br 13
    Breakpoint 1 at 0x109e4: file main.c, line 13.
    (gdb) r
    Starting program: /users/cs/macdona2/other/tehrogue/tehrogue
    
    Breakpoint 1, main () at main.c:13
    13              Map *map_p = map_create_p(10, 10);
    (gdb) print map_p
    $1 = (Map *) 0x0
    (gdb) n
    15              Creature *player_p = creature_create_p();
    (gdb) print map_p
    $2 = (Map *) 0x210e0
    
    (gdb) print map_p->tiles_p[0][0]
    $3 = 46 '.'          // *** Everything is fine. ***
    
    (gdb) print player_p
    $4 = (Creature *) 0x0
    (gdb) n
    23              printf("This is just busywork.\n");
    
    (gdb) print map_p->tiles_p[0][0]
    Cannot access memory at address 0x2151000 // *** How did this happen?! ***
    
    (gdb)
    The strange thing is that this only happens when I create the Creature structure. If I comment that line out the array in the Map structure stays fine.

    Here's the Creature structure by the way:
    Code:
    struct creature
    {
    	unsigned int row;
    	unsigned int col;
    	
    	Map *map_p;
    	
    	char symbol;
    };
    
    typedef struct creature Creature;
    And here is how I create a Creature structure:
    Code:
    Creature *creature_create_p()
    {
    	Creature *cp = malloc(sizeof(cp));
    	
    	cp->row = 0;
    	cp-col = 0;
    	
    	cp->map_p = NULL;
    	
    	return cp;
    }
    If someone can help me figure this out I'd really appreciate it.

    Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Use the idiom
    Code:
    p = malloc(sizeof *p);
    instead of
    Code:
    p = malloc(sizeof p);
    (And copy and paste to avoid miscellaneous unrelated typos.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    Halifax, N.S.
    Posts
    4

    Smile

    Thank you. That seemed to fix everything.

    I should probably go read the FAQs now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM