Re: Doubts about free()
- From: James Kuyper <jameskuyper@xxxxxxxxxxx>
- Date: Fri, 31 Oct 2008 10:37:05 GMT
pushpakulkar@xxxxxxxxx wrote:
Hi all,
If I have a piece of code something like this
int main()
{
char *s1;
s1= (char *) malloc(sizeof(char) * 200)
while(somecondition)
{
................... //do something
}
free(s1);
while(true)
{
;
} //Infinite loop
}
This is just a sample code, please ignore the syntax errors.
In the above piece of code during execution malloc results in
allocating space physically in the RAM. ...
Where the memory is located is a implementation detail. It might actually be stored on a hard disk, or somewhere else entirely; on machines with virtual memory, any sufficiently large allocation will probably reside on a hard disk for at least part of its lifetime, if you hold onto it for sufficiently long.
... Now after the first while loop
execution is complete free(s1) is called. At this point of time is
memory returned back to the OS or not always.
The answer is different on different systems. You'll have to identify a specific system to get a specific answer.
Assuming there is a need for memory by some other process and no other
free memory is available, is it the case malloc for the other process
would fail under such circumstances. Does the OS do some kind of
tracking and free up this memory. Is there any way to force freeing
up the memory to the system back.
The C standard library provides no such mechanism. I'm not familiar with any way of doing so on any of the Unix-like systems that most of my programs run on.
.
- References:
- Doubts about free()
- From: pushpakulkar
- Doubts about free()
- Prev by Date: Re: Who defines the platform specific macros eg LINUX VXWORKS etc
- Next by Date: 9 / 11 payback time
- Previous by thread: Doubts about free()
- Next by thread: Re: Doubts about free()
- Index(es):
Relevant Pages
|