[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: execve - what happens to dynamically allocated memory?



On Tue, Oct 30, 2001 at 09:39:35PM -0800, Aaron Brashears wrote:
> The manpage for execve states:
[snip]
> I'm wondering what happens to dynamically allocated memory. For
> example, if you have a code snippet such as:
[snip]

The dynamically allocated memory is deallocated. Mostly because the
allocation of memory via malloc() is done strictly within the C library,
and the only time it actually uses system resources is when it calls
sbrk() to grab more heap memory when it runs out. 

So the memory is only "allocated" from the point of view of the C library; 
as far as the system is concerned, the program data segment is just
occupying a single stretch of addresses (including any as-yet unallocated
dynamic memory) -- no different from another program that doesn't use
dynamically allocated memory. When sbrk() is called, the system just adds
more addresses to the segment; no problem there.

When execve() is called, the system removes everything in this data
segment and replaces it with the data segment from the new process.

Now, it would be a different story if the program started allocating
*system* resources, such as shared memory and semaphores... I'll leave
that for better-clued developers to answer :-)


T

-- 
Winners never quit, quitters never win. But those who never quit AND never
win are idiots.



Reply to: