New environment for child process - how?

From: Charles Sullivan (
Date: 01/19/04

Date: Mon, 19 Jan 2004 08:23:29 GMT

My process needs to spawn multiple child processes, each with a different
environment. As I understand it, I can do the following:
  1. Allocate memory and create the new environment array envp[].
  2. fork()
  3. execve(child_filename, argv, envp)

But here's the question:
I'm passing the new environment pointer to execve(), which doesn't return
(and which may take a long time to finish execution). But somewhere along
the line I have to free the memory I allocated for the new environment
else I'll have a memory leak.

How do I handle this situation?

Thanks for your advice.

Charles Sullivan