Re: Doubts about free()



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.
.



Relevant Pages

  • Re: noise as a snore in memory when computer is in idle
    ... | snore sound when you closed it without a disk in it. ... | memory which is most often used for RAM-type memory (or flash memory ... Go to the hard disk manufacturer's web site and download their diagnostic software ...
    (microsoft.public.security.virus)
  • Re: "Never change a running system"
    ... > Probably due to some internal memory conflict... ... If the temporary files were corrupted in the ... > even the stuff which was in My Documents had been lost. ... > preferably not in My Documents, but on the hard disk which (can be accessed ...
    (sci.lang)
  • Re: unexpected result
    ... Dad: "Son, although I admire your goal, I've been reading a lot in CLC ... and it doesnt cause your hard disk to get erased. ... for every out of control memory access you have in C then trust me, ...
    (comp.lang.c)
  • Re: adding memory question
    ... performance increase from the memory upgrade. ... free space is not very much for windows and possibly part of your overall ... If I need more memory AND more hard drive, that might really be the time ... think that your money would be far better spent on a hard disk ...
    (alt.sys.pc-clone.dell)
  • Re: Delphi - desktop, Web, or USB?
    ... A few days ago I was ordering a new compact Dell, and was tempted to order a USB memory stick with the capacity of 8 GB. ... What I really want to carry is the contents of the hard disk, not the hardware needed to access the hard disk. ... Having all the software on a USB stick together with the corresponding data reduces what I have to carry to something I can carry in the same pocket as the loose change. ... -- A typographical system like LaTex with at good editor, the ideal would be something like WinEdt which also handles R. ...
    (borland.public.delphi.non-technical)