Re: Doubts about free()



On Oct 31, 3:12 am, pushpakul...@xxxxxxxxx wrote:

In the above piece of code during execution malloc results in
allocating space physically in the RAM.

Yes, if and only if the system library and operating system think
that's the right thing to do. All that is guaranteed is that address
space local to this process be reserved.

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.

Your use of the term "memory" is ambiguous. It could mean physical
memory, virtual memory, or some confused combination of the two. Since
all that had to be reserved was address space local to the process,
that's all that has to be recovered, and it is on every implementation
I know of.

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.

If you're talking about physical memory, which is probably the only
thing that's scarce, modern operating systems never give that to a
process without retaining the ability to recover it if needed. The
exception would be specific calls to lock memory. Physical memory
mapped to meet the demands of 'malloc' is never locked into the
process. It can always be recovered if physical memory can better be
used elsewhere.

 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.

If you mean system wide virtual memory, probably. If you mean physical
memory, definitely not.

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.

It's almost impossible to answer that, because it's not clear what you
mean by "memory". In normal circumstances, only physical memory would
be scarce. Physical memory can easily be reclaimed by the OS.

DS
.



Relevant Pages

  • Re: xmalloc string functions
    ... Failed malloc() is. ... minor annoyance to a critical failure. ... there is something to recover from. ... does g_list_appendallocate memory? ...
    (comp.lang.c)
  • Re: malloc and free
    ... In the past I have always been casting the malloc. ... >When I use malloc for the allocation of a small piece of memory, ... >of the allocated block the pointer is pointing to. ... operating system of the C run-time library is an implementation issue. ...
    (comp.lang.c)
  • Re: If Macs have no spyware....
    ... >had made a complete code review of its operating system and removed all ... and writing new data into those memory locations would ... >but when the data exists on the stack, it can cause very large problems. ... >location that needs to be written in place of the correct execution ...
    (comp.sys.mac.advocacy)
  • Re: If Macs have no spyware....
    ... First you yammer about being a Mac advocate, then bad mouth me for dumping XP in favor of a Mac. ... Supposedly Microsoft had made a complete code review of its operating system and removed all the buffers which could overflow. ... the fundamental problem is that the basic architecture of Windows has two fatal flaws in its memory management and while these remain in the software the ad hoc patches will never be enough to make Windows a secure operating system. ... These problems are bad enough when dealing with data in the one routine but when the data exists on the stack, it can cause very large problems. ...
    (comp.sys.mac.advocacy)
  • Re: [Lit.] Buffer overruns
    ... > floating point support or a memory expansion option. ... had virtual memory support grafted on. ... > where the modified instruction was fetched from. ... vis-a-vis the official coporate strategic operating system TSS/360. ...
    (sci.crypt)