title: Questions about putenv() and setenv()
body: <p>I have been thinking a little about environment variables and have a few questions/observations.</p>
  
  <ul>
  <li><p><code>putenv(char *string);</code></p>
  
  <p>This call seems fatally flawed.  Because it doesn't copy the passed string you can't call it with a local and there is no guarantee a heap allocated string won't be overwritten or accidentally deleted.  Furthermore (though I haven't tested it), since one use of environment variables is to pass values to child's environment this seems useless if the child calls one of the <code>exec*()</code> functions.  Am I wrong in that?</p></li>
  <li><p>The Linux man page indicates that glibc 2.0-2.1.1 abandoned the above behavior and began copying the string but this led to a memory leak that was fixed in glibc 2.1.2.  It's not clear to me what this memory leak was or how it was fixed.</p></li>
  <li><p><code>setenv()</code> copies the string but I don't know exactly how that works.  Space for the environment is allocated when the process loads but it is fixed.  Is there some (arbitrary?) convention at work here?  For example, allocating more slots in the env string pointer array than currently used and moving the null terminating pointer down as needed?  Is the memory for the new (copied) string allocated in the address space of the environment itself and if it is too big to fit you just get ENOMEM? </p></li>
  <li><p>Considering the above issues, is there any reason to prefer <code>putenv()</code> over <code>setenv()</code>?</p></li>
  </ul>