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
That is correct.
To help avoid shooting yourself in the foot, you might consider the following practices:
The following function would be a good way to make sure you always cleanup a structure properly, avoid memory leaks, and avoid accidentally freeing already-freed memory and causing a segmentation fault:
eg:
Good luck!