Doubts about free()



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. 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. I guess memory is just
marked as freed for future use by the same process.

I have read that in some implementations of OS like some unix flavors
even after free() is executed memory is not released back to the
system. In the above piece of code is it the case that the memory is
never freed up at all in such implementations and remains tied up till
the process dies.

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.

Thanks and Regards,
Pushpa
.



Relevant Pages

  • Re: Fast string operations
    ... > is why people use unsafe code now and then to use pointer arithmetic to ... > loop over arrays without all the unnecessary bounds checking. ... don't return the trimmed string". ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Performance Improvement of complex data structure (hash of hashes of hashes)
    ... Anno Siegel wrote: ... >> Here is the code that I'm using to build up this data structure. ... loop through and increment the ... I know that memory allocation in C is expensive, ...
    (comp.lang.perl.misc)
  • Re: I want to learn forth but...
    ... on each pass of the loop. ... deeper in the execution stack. ... memory, and the part I quoted gave another loop approach (cyclic ...
    (comp.lang.forth)
  • Re: Cost of calling a standard library function
    ... It accesses/reads memory using esi 4 ... > safly move it within the cache, without having to go via ebx. ... try it the same thing on a different earlier CPU, ... should check it out...for "tight inner loop" stuff, ...
    (alt.lang.asm)
  • Re: Restoring a NorthStar Horizon, problems with SRAM board
    ... A-D FDC is a memory mapped IO device so I would expect some behavior ... loop read/write functions - which puts the system into a two instruction ... SRAM on the Vector Graphics Flashwriter video card located at $F000 ...
    (comp.sys.northstar)