C dynamic 3D array using malloc

This is a discussion on C dynamic 3D array using malloc within the C Programming forums, part of the General Programming Boards category; I am trying to create a dynamic 3D array to store 3 graphics points [x, y, z] in each location ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    C dynamic 3D array using malloc

    I am trying to create a dynamic 3D array to store 3 graphics points [x, y, z] in each location (hence the use of a struct).

    array[i][j][k]

    The i & j dimentions store the frame data.
    The k dimension is needed because the data values change for each frame of the image.

    At the beginning of the program all values are unknown hence the use of dynamic arrays, and all data is read from a file.

    The reason i need to use realloc is that to start with i only know the x & y dimensions so i can read in the initial frame data (as such i have only initially allocated 1 frame) but once the initial frame is read in i can then read the actual number of frames and have to then resize the array in the k dimension.

    The problem im having is that the
    Code:
     skeleton[i] = realloc(*skeleton[i], (num_allocated_hierarchy_y * sizeof(OFFSET *)));
    line is segfaulting.

    any help would be much appreciated???

    Code:
    // Skeletal Structure
    typedef struct {
        
        double x;
        double y;
        double z;
        double angle;
    	int used;
        
    } OFFSET;
    
    OFFSET *** skeleton = NULL;
    OFFSET *allElements = NULL;
    int num_elements_hierarchy_x = 0, num_elements_hierarchy_y = 0, num_elements_frames = 0;
    int num_allocated_hierarchy_x = 0, num_allocated_hierarchy_y = 0, num_allocated_frames = 0;
    int array_size_frames = 1;
    int array_size_hierarchy = 0;
    
    void Add_Skeleton() {
    
    	if(num_elements_hierarchy_x == num_allocated_hierarchy_x || num_elements_hierarchy_y == num_allocated_hierarchy_y || num_elements_frames == num_allocated_frames) { // Are more refs required?
        
            if (num_allocated_hierarchy_x == 0) {
                
                num_allocated_hierarchy_x = array_size_hierarchy; 
                
            } else {
                
                num_allocated_hierarchy_x *= 2; // Double the number of refs allocated
                
            }
    		
    		if (num_allocated_hierarchy_y == 0) {
                
                num_allocated_hierarchy_y = array_size_hierarchy; 
                
            } else {
                
                num_allocated_hierarchy_y *= 2; // Double the number of refs allocated
                
            }
    		
    		if (num_allocated_frames == 0) {
                
                num_allocated_frames = array_size_frames; 
                
            } else {
                
                num_allocated_frames *= 2; // Double the number of refs allocated
                
            }
            	
    		 //  Array Iterators
    		int i, j;
    		
    		//  Allocate 3D Array
    		allElements = realloc(allElements, (num_allocated_hierarchy_x * num_allocated_hierarchy_y * num_allocated_frames * sizeof(OFFSET)));
    		
    		skeleton = realloc(skeleton, (num_allocated_hierarchy_x * sizeof(OFFSET **)));
        
    		for(i = 0; i < num_allocated_hierarchy_x; i++) {
    			
    			skeleton[i] = realloc(*skeleton[i], (num_allocated_hierarchy_y * sizeof(OFFSET *)));  
    
    			for(j = 0; j < num_allocated_hierarchy_y; j++) {
    			
    				skeleton[i][j] = allElements + (i * num_allocated_hierarchy_y * num_allocated_frames) + (j * num_allocated_frames);
    			
    			}
    		
    		}
    
            
            // If the reallocation didn't go so well,
            // inform the user and bail out
            if (!skeleton) {
                
                fprintf(stderr, "ERROR: Couldn't realloc memory!\n");
                return;
                
            }
            
        }
        
    	printf("Allocated!\n");
    	
        return;
    	
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    allElements = realloc(allElements,
    You should never use realloc that way. If it fails, you've just lost everything you had allocated.
    Code:
    temp = realloc( thishere, ... );
    if( temp )
        thishere = temp;
    else
        ...realloc failed, better figure out what to do...
    And so...
    Code:
    xtemp = realloc( array, x * sizeof array )
    if( xtemp )
    {
        array = xtemp;
        for( xcount = 0; xcount < x; xcount++ )
        {
            ytemp = realloc( array[ x ], y * sizeof *array );
            if( ytemp )
            {
                array[ x ] = ytemp;
                for( ycount = 0; ycount < y; ycount++ )
                {
                    ztemp = realloc( array[ x ][ y ], z * sizeof **array );
                    if( ztemp )
                        array[ x ][y ] = ztemp;
    That looks about right.

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

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Thanks i will try that when i get some time and let you know how it goes!

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    ok so i've applied what you suggested to my code:

    Code:
     
    int i, j;
    OFFSET ***xtemp, **ytemp, *ztemp;
    		
    		xtemp = realloc(skeleton, (num_allocated_hierarchy_x * sizeof(skeleton)));
    		if( xtemp ) {
    			
    			skeleton = xtemp;
    			for( i = 0; i < num_allocated_hierarchy_x; i++ ) {
    			
    				ytemp = realloc( skeleton[num_allocated_hierarchy_x], (num_allocated_hierarchy_y * sizeof(*skeleton)));
    				if( ytemp ) {
    				
    					skeleton[num_allocated_hierarchy_x] = ytemp;
    					for( j = 0; j < num_allocated_hierarchy_y; j++ ) {
    					
    						ztemp = realloc( skeleton[num_allocated_hierarchy_x][num_allocated_hierarchy_y], (num_allocated_frames * sizeof(**skeleton)));
    						if( ztemp )
    							skeleton[num_allocated_hierarchy_x][num_allocated_hierarchy_y] = ztemp;
    					}
    				
    				}
    			
    			}
    		
    		}
    i'm still getting a segfault… this time on line:
    Code:
    ytemp = realloc( skeleton[num_allocated_hierarchy_x], (num_allocated_hierarchy_y * sizeof(*skeleton)));

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Does skeleton[ X ] actually have valid memory? If you have an invalid pointer there, your realloc will fail, because it will try to access some place you don't have. It sounds like your array wasn't set up properly in the first place, so when you try to resize it, it fails.

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

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    i'm trying to use this function to both set up and reallocate the array... i thought that was possible???

    Idle

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, but consider:
    Code:
    char ** s = malloc( X * sizezof *s );
    This just gives you a bunch of pointers to characters. They don't point anywhere valid. They're just uninintialized pointers. If you try to realloc on one of them, since it's not set to NULL, it's going to go to whatever that spot is and try to reallocate whatever that block is, which is why it's seg faulting.

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

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    ohh ok so if i changed it so that that line was initialised to NULL the first time the functions run it would work then?

    Idle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Using dynamic allocation and malloc
    By jh294 in forum C Programming
    Replies: 5
    Last Post: 04-01-2011, 01:13 PM
  2. Dynamic Memory Allocation (malloc vs calloc)
    By lostandconfused in forum C Programming
    Replies: 10
    Last Post: 09-01-2010, 01:56 PM
  3. Replies: 8
    Last Post: 12-23-2009, 12:55 PM
  4. Dynamic array - malloc
    By erasm in forum C Programming
    Replies: 4
    Last Post: 10-04-2009, 04:10 PM
  5. dynamic array malloc()
    By el_chupacabra in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 06:46 AM

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