Thread: Safe to declare an array of structures that contain arrays?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Safe to declare an array of structures that contain arrays?

    Is it safe to declare an array of structures, if the strucure itself contains dynamically allocated arrays? Below is some sample code.

    The program I'm working on seems to work, but I'm worried that the array of structures will get me in trouble later.

    Code:
    #include <stdio.h>
    
    
    struct MyStruct{
    
    	double V1;     
    	double V2;      
    	double * A1;  
    	double * A2; 		             
    
    };
    
    
    int main() {
    
    
    	int n = 10; 
    
    	struct MyStruct *x;
    	x = (struct MyStruct*)malloc((n)*sizeof(struct MyStruct));
    
    
    	for( i = 0; i < n; i++ ){			
    		x[i].A1 = (double*)malloc(60*sizeof(double));
    		x[i].A2 = (double*)malloc(60*sizeof(double));
    	}
    
    
    	//
    	//
    	// insert useful code here
    	//
    	//
    	    
    	    
    	for( i = 0; i < n; i++ ){
    		free(x[i].A1);
    		free(x[i].A2);
    	}	  	
    	
    	free(x);     	      
    
    
    return 0;       
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    As long as you're fastidious in your memory cleanup you should be fine. However, you should not need to cast the return value of malloc if you're compiling this as C code, and doing so can hide problems.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rags_to_riches View Post
    As long as you're fastidious in your memory cleanup you should be fine. However, you should not need to cast the return value of malloc if you're compiling this as C code, and doing so can hide problems.
    Like including the proper header file.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Also, a useful idiom is to use sizeof(*p) to determine the size of each block of memory you're allocating, instead of using a hard-coded type like sizeof(struct something). That way, if the type of p changes, the malloc() statements don't become out of date (which can be dangerous). For example, this
    Code:
    	struct MyStruct *x;
    	x = (struct MyStruct*)malloc((n)*sizeof(struct MyStruct));
    can be written as
    Code:
    	struct MyStruct *x = malloc(n * sizeof(*x));
    Never fear for efficiency: the sizeof() is still evaluated at compile-time, so it's just as efficient.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Technically, you don't have a structure which has an array. You have a structure which has a pointer. There's a big difference. If it were an actual array, you wouldn't have to free the array (in fact, you could not free it implicitly, rather, you would free the actual structure--if you had allocated it dynamically).


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

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by rags_to_riches View Post
    As long as you're fastidious in your memory cleanup you should be fine. However, you should not need to cast the return value of malloc if you're compiling this as C code, and doing so can hide problems.
    I think I'm actually compiling this as C++. I'm using mkoctfile to compile, and the file I'm using ends in .cc.

    When I tried to compile without the casting, I got some message about conversion from MyStruct* to void*.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Quote Originally Posted by quzah View Post
    Technically, you don't have a structure which has an array. You have a structure which has a pointer. There's a big difference. If it were an actual array, you wouldn't have to free the array (in fact, you could not free it implicitly, rather, you would free the actual structure--if you had allocated it dynamically).

    Quzah.
    That makes sense. It would explain why my program hasn't crashed.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, that's what happens in C++. If you're really writing C code, it's best to use .c files and compile as C. There are some subtle differences that can trip you up.

    This particular difference is because C allows pointers of any type to be assigned to and from void* pointers; but C++ only allows assignments to void* pointers. Assignments from void* pointers, like
    Code:
    char *p = malloc(10);
    need to have a cast in C++.

    There's an entry in the FAQ about this.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. 2 Dimenstional Array vs Array of Structures
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2004, 04:51 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM