Using environment variables without leaking memory?

Paul Brook paul at codesourcery.com
Mon Oct 23 02:13:51 UTC 2006


> How do I set (and reset) environment variables without leaking memory?  I
> _think_ if I use setenv() instead of putenv() it won't leak memory (freeing
> the old one), but if I previously used putenv() and then call setenv() to
> update that variable, does it free the old chunk of memory?  Or not?  (How
> does it know?  Presumably the environment variables inherited from a parent
> process weren't allocated with malloc()...)

When you create a new process the kernel makes a new copy of the environment. 
Usually this is put at the top of the new process' stack. The environment is 
not shared between multiple processes.

Paul



More information about the uClibc mailing list