c - Freeing memory of structure linked list using expiration of function scope -


my goal run api call repeatedly , check memory leaks.
api takes in integer input , returns pointer first structure in linked list of structures output. each structure may have struct variables of other types.
in following code have tried represent problem.

the problem in callapi() function. need dispose of memory occupied structure 'output' api() returns. if use free(output) here, cause memory leak because since points linked list of nested structures. (source of info : http://en.wikibooks.org/wiki/c_programming/memory_management)

question: when leaving exiting callapi() call, 'output' nested structure expire when control goes out of function main()? free entire memory occupied?
please suggest solution way solve memory leak problem.

can problem overcome c++?

typedef struct{   int dev_size;   char *dev_name;   dev_stat *next_dev;   mem_stat *mem_info; } dev_stat  typedef struct{   int mem_capacity;   char *mem_name; } mem_stat  int main() {   int input;   int return_val;   int callapi(int);   while(1)   {      return_val=callapi(input);      print return_val;   } }  int callapi(int ip) {   //update: memory allocation unnecessary done inside api() call   //dev_stat *output=(dev_stat *)calloc(2,sizeof(dev_stat));   int ret_val;   ret_val=api(ip,&output);   free(output);   output=null;   return ret_val; } 

the simple answer is, no, memory not "expire" when exit function.

the api should provide way "free" returned value, if complex structure. if doesn't, traversing structure , freeing way out.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -