Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

in unix, i want to modify enviroment variables.
if size of new value is larger than the old one, the room for the new variable is allocated by malloc.
however, is memory for enviroment variables above the stack? (not heap memory) where to allocate the room for new variable?

share|improve this question
up vote 1 down vote accepted

The memory for the initial environment variables may be above the stack, but that doesn't necessarily mean that new ones need to be allocated there too.

How to allocate strings used to set environment variables depends on what function you use to set it:

  • putenv() doesn't copy the string ("name=value") passed to it, so it needs to be in a place that doesn't get deallocated. A malloc() that doesn't get free'd is good here, for instance, but string literals work too (if you know what name+value you want to set at compile time).
  • setenv() copies the strings ("name", "value") it's passed, so they can be anywhere you like. They can be literals, allocated with malloc(), alloca(), C++'s new char[], or they can just be a global or stack-allocated arrays. And when setenv() returns you can dispose of or reuse that memory however you like. (Note the third parameter (int overwrite) if you decide to use this one)

May I suggest you read up on those functions?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.