
December 17th, 2012, 01:57 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 1
Time spent in forums: 30 m 3 sec
Reputation Power: 0
|
|
|
How to free pointer to array of struct
Hello, I have problem with memory leaks. I am not able to dealocate space of the pointer to array of tStruct. I need to by able to access first, second and third later, so I set items[0]..items[2] to NULL, free it ane afterwards i free *items. But it still leaves some memory allocated.
As it is homework, i can't change headers of functions.
Please help me.
Code:
typedef struct item{
char *string;
tGPS gps;
struct item *next;
}tStruct;
tStruct *first, *second, *third;
tGPS *gps;
tStruct *AllocStruct(char *string, tGPS gps) {
tStruct *item;
item = (tStruct*)malloc(sizeof(tStruct));
item->string=(char*)malloc(strlen(string)+1);
strcpy(item->string, string);
item->gps = *gps;
item->next = NULL;
return item;
}
tStruct **FindThreeClosest(tGPS *gps){
tStruct **items = (tStruct**)malloc(3*sizeof(tStruct*));
items[0] = first;
items[1] = second;
items[2] = third;
return items;
}
int main(){
tStruct **items;
item = FindThreeClosest(&gps);
//some code
items[0]=NULL;
items[1]=NULL;
items[2]=NULL;
free(items[0]);
free(items[1]);
free(items[2]);
free(*items);
*item = NULL;
}
|