Using environment variables without leaking memory?

Rob Landley rob at landley.net
Mon Oct 23 01:08:58 UTC 2006


Maybe this is the wrong place to ask, but the fundamental problem is that the 
libc API is badly designed.  I assume uClibc does this right, so you guys 
must know:

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()...)

I'm missing a concept here, I think.  I don't quite understand all the details 
of environment variables.  With putenv() you take a malloced chunk of memory 
and it becomes a live environment variable, but how does a child process 
inherit that sucker when exec a new process and free all the old memory?

Sorry about the off-topic question.  Could somebody point me where I should go 
read up on this?

Rob
-- 
"Perfection is reached, not when there is no longer anything to add, but
when there is no longer anything to take away." - Antoine de Saint-Exupery



More information about the uClibc mailing list