| c programming questionForum: Other Help Topics Topic: c programming question started by: Lii  Posted by Lii on Feb. 20 2005,07:26 can someone clear something for me, please? let's say I have the following code: 1: int main(){ 2: SomeFunction(); 3: return 0;} 4: 5: int SomeFunction(){ 6: char *ptr; 7: ptr=(char *)malloc(sizeof(char)*20); 8: //...do something with ptr 9: free(ptr); 10: return 0;} my question is: what happens if I don't use the free(ptr) on line 9? When is the memory deallocated? -When I return from the SomeFunction function on line 10? -When I exit the program on line 3? -Never?  Posted by clacker on Feb. 20 2005,23:35 Lii, when you leave out the free memory isn't freed, ever.  This is known as a "memory leak." When you exit the function memory can't be freed by default, what if another function needed that memory and you put the pointer into a global variable?  Posted by Lii on Feb. 21 2005,06:18  
 that's really sad!   thx for your time.  Posted by clacker on Feb. 21 2005,11:58 not that sad, I mean it causes crashes and all, but memory leaks are all gone when you restart the machine, so it isn't really "never" it's until restart. |