PDA

View Full Version : memory allocation in C



c2tarun
November 6th, 2010, 03:32 AM
what if we allocate certain amount of memory by malloc at the beginning of the program but dont free it at the end???
will it be remain like that or when the program terminates it will be freed automatically.

TiBaal89
November 6th, 2010, 03:50 AM
It will be freed. I think that task gets thrown to the OS if you don't do it yourself. In something like Ubuntu, I'd definitely expect it would be freed.

trent.josephsen
November 6th, 2010, 03:56 AM
It probably will be freed, but you should free it anyway. There are systems where malloc'ed memory is not freed on program termination, and the C standard says nothing about the matter, so don't depend on it.

A good rule of thumb (for C) is to structure your program so that a call to malloc() and the corresponding call to free() are in the same subroutine.

saulgoode
November 6th, 2010, 03:58 AM
More specifically, memory is allocated to a particular process and will automatically be freed when that process terminates. (So if your program forks a process which malloc's some memory then when that process ends, the malloc'ed memory will automatically be free'd, even though your program is still running.)

msakms
November 6th, 2010, 04:06 AM
The Linux kernel memory management procedure insures that the allocated memory address space is cleaned up once the process is terminated
Resource "Understanding the Linux Virtual Memory Manager"