Thread: Double Pointer Memory Allocation: Problems

  1. #1
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    Double Pointer Memory Allocation: Problems

    Ok i have this:
    Code:
    void array_dump();
    void print_array();
    void free_color_array();
    
    int width = 256;
    int height = 256;
    
    struct RGB
    {
    	unsigned char red;
    	unsigned char green;
    	unsigned char blue;
    };
    
    struct RGB **color_array;
    
    main()
    {
    	array_dump();
    	print_array();
    	free_color_array();
    	
    }
    
    void print_array()
    {
    	int i;
    	int j;
    
    	for(i = 0; i < height; i++)
    	{
    		for(j = 0; j < width; j++)
    		{
    			printf("(%d ", (unsigned char)color_array[i][j].blue);
    			printf("%d ", (unsigned char)color_array[i][j].green);
    			printf("%d) ", (unsigned char)color_array[i][j].red);
    		}
    		printf("\n");
    	}
    }
    
    void array_dump()
    {
    	int i;
    	int j;
    		
    	color_array = (struct RGB**)malloc(sizeof(unsigned char)*3*height);
    	for(i = 0; i < height; i++)
    	{
    		color_array[i] = (struct RGB*)malloc(sizeof(unsigned char)*3*width);
    	}
    	
    	for(i = 0; i < height; i++)
    	{
    		for(j = 0; j < width; j++)
    		{
    			color_array[i][j].blue = (unsigned char)20;
    			color_array[i][j].green = (unsigned char)20;
    			color_array[i][j].red = (unsigned char)20;
    		}
    	}
    }
    
    void free_color_array()
    {
    	int i;
    	int j;
    	
    	for(i = 0; i < height; i++)
    	{
    		free(color_array[i]);
    	}
    	free(color_array);
    }
    Very simple right.

    The problem is that it seg-falts, but the real problem is where i found it seg-falting

    if i change my array_dump() function to this:
    Code:
    void array_dump()
    {
    	int i;
    	int j;
    		
    	color_array = (struct RGB**)malloc(sizeof(unsigned char)*3*height);
    	for(i = 0; i < height; i++)
    	{
    		color_array[i] = (struct RGB*)malloc(sizeof(unsigned char)*3*width);
    	}
    }
    *Notice that it is the exact same code but the two bottom for loops are cut out, so i am just allocating the array and not setting the values

    This will run fine and print out the system junk gathered durning allocation when print_array() is called.
    If you will also Notice that the two for loops that where cut out of array_dump() are the exact same in structure to the ones in print_array(), the only difference is the what is happening inside the for loops.

    So i tell my self, maybe my color_array is not being allocated correctly, which could in turn allow my print_array() function to be wayward and just look at any thing it likes to. So i add one to width and height before i enter the for loops in print_array() and low in behold it prints all the way to the end and then seg-falt. This should mean that my malloc code in array_dump is working correctly. SO WHY ARE THE LAST FOR LOOPS IN array_dump SEG-FALTING?

    Thanks for any help

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    color[j] is a pointer
    pointer takes sizeof(void*) bytes

    height pointers take height * sizeof (color[j]) bytes that a little bit more than you actually allocating (you overwrite 256 bytes)

    read FAQ about malloc casting and use suggested technic to avoid such problems
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > color_array = (struct RGB**)malloc(sizeof(unsigned char)*3*height);
    > color_array[i] = (struct RGB*)malloc(sizeof(unsigned char)*3*width);
    Because you have a pointer to a struct, you add in ad-hoc scaling factors.
    Your first one is wrong anyway because pointers are more usually 4 bytes rather than 3.


    p = malloc ( num * sizeof *p );
    ALWAYS works, and you don't have to do any more thinking about it.

    So in your case
    color_array = malloc( height * sizeof *color_array );
    color_array[i] = malloc( width * sizeof *color_array[i] );

    Oh, and drop the casting of malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Thanks alot Salem, i havent used C in a while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM