[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:13PM -0800, Aaron Brashears wrote:
> The manpage for execve states:
> 
>        execve() does not return on success, and the  text,  data,
>        bss,  and  stack of the calling process are overwritten by
>        that of the program loaded.  The program invoked  inherits
>        the  calling  process's PID, and any open file descriptors
>        that are not set to close on exec...
> 
> I'm wondering what happens to dynamically allocated memory. For
> example, if you have a code snippet such as:
> 
> child_argc = 5;
> char** child_argv;
> char buffer[MAX_STRING];
> child_argv[0]="./a.out";
> for( i = 1; i < child_argc; i++ )
> {
>   sprintf( buffer, "-%d", rand() );
>   child_argv[i] = malloc( strlen(buffer) );
>   strcpy( child_argv[i], buffer );
> }
> child_argv[i] = NULL;
> execvp( child_argv[0], child_argv );
> 
> 
> What happens to the dynamically allocated memory? FYI: I wrote the
> above code off the top of my head, so it hasn't been check for actual
> legal syntax.

The execvp system call works as expected. It does not clear memory
until it is ready to do so. Otherwise, it won't have the
command/arguments to properly load the new program.



Reply to: