Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

free the space allocated in c with malloc

I have a question about this code:

typedef struct pop {
unsigned long int *np; // matrix
unsigned long int f;
long double fp; 
unsigned long int *R; // matrix
unsigned long int *C; // matrix
unsigned long int Dp;
unsigned long int Ds;
unsigned long int count;
struct popolazione *ptrTempLst; // pointer
struct popolazione *leftTree;  // left tree pointer
struct popolazione *rightTree; // right tree pointer
} Node; 

When I free space allocated for this struct, prior have I to free pointer to matrix inside struct?

For example,

 Node *ptr=(Node *) malloc(sizeOf(Node));
 ptr->np=(unsigned long int *)malloc(10*sizeOf(unsigned long int));

 /*code code code*/

 // is necessary: free(ptr->np); 

 free(ptr);

Thanks in advance

like image 692
sangi Avatar asked May 24 '12 18:05

sangi


1 Answers

Yes.

Every call to malloc must have a matching call to free.

like image 173
user703016 Avatar answered Sep 21 '22 16:09

user703016