C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 17th, 2012, 01:57 AM
metaathron metaathron is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 metaathron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > How to free pointer to array of struct


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap