Thread: multidimensional array and malloc

  1. #1
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222

    Question multidimensional array and malloc

    Im trying to use malloc to allocate memory for a multi dimensional array of ints. After this code has finished i want to be able to address it with plane[x][y]. Does any one know what i'm doing wrong here?

    Code:
    	
                         // Declare variables 	
                         int plane_width = 10, plane_height = 10, x, y; 	
                         int * plane;	 	 	
                         
                         // Allocate memory for first dimension of  plane 	
                         if ( ( ( plane = malloc( sizeof( int * ) * plane_width ) ) == NULL ) ) { 
                            fprintf( stderr, "No memory available\n" );
                            exit( EXIT_FAILURE ); 
               	     }	 	
                         // Allocate memory for second dimension of plane
                         for ( x = 0; x < plane_width; x++ ) {
                 		if ( ( ( plane[x] = ( int * ) malloc( sizeof( int ) * plane_height ) ) == NULL ) ) { 
    			      fprintf( stderr, "No memory available\n" );
           			      exit( EXIT_FAILURE ); 		
                           } 		
                            // Set tile to default value
                            for ( y = 0; y < plane_height; y++ ) { 	
               		  plane[x][y] = TILE_UNINITIALISED; 		
                            } 
    	           }
    "Assumptions are the mother of all **** ups!"

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Start with
    int **plane;

    Lose the malloc casts - they're hiding your real problem.

    The general form is T = malloc ( sizeof(*T) * num ), so in your case
    plane = malloc( sizeof( *plane ) * plane_width )
    plane[x] = malloc( sizeof( *plane[x] ) * plane_height )
    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.

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    thanx heaps salem!
    "Assumptions are the mother of all **** ups!"

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    44
    Just on a side note, there's another way of doing the same thing:

    There's been a few times when I've used a library which required the use of the flat[x+w*y] form but where I've wanted the convenience of using two indices like arr2d[y][x].

    Code:
    int * flat;
    int ** arr2d;
    unsigned int i;
    
    flat=malloc(sizeof *flat * w * h);
    if (!flat) {
      /* handle the error */
    }
    
    arr2d=malloc(sizeof *arr2d * h);
    if (!arr2d) {
      /* handle the error */
    }
    
    for (i=0;i<h;i++) {
      arr2d[i]=flat+i*w;
    }
    You can index the flat array using:

    flat[x+w*y]

    but also can use:

    arr2d[y][x]

    to refer to the same element. (Note that the dimensions are reversed.)

    Ian Woods

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And if you make the array dimensions a power of 2, then you can access the linear array as a 2D array by using a simple shift.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing an array created with malloc as a param
    By mariano_donati in forum C Programming
    Replies: 12
    Last Post: 02-20-2008, 12:26 PM
  2. Replies: 19
    Last Post: 12-17-2007, 02:57 AM
  3. malloc or array initialization
    By JBull in forum C Programming
    Replies: 5
    Last Post: 12-03-2007, 12:51 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. How to allocate memory in multidimensional array?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-15-2001, 10:07 AM