title: Is it safe to use getenv() in static initializers, that is, before main()?
body: <p>I looked in <a href="http://www.google.com/search?q=advanced+programming+in+the+unix+environment&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a" rel="noreferrer">Stevens</a>, and in the <a href="https://rads.stackoverflow.com/amzn/click/com/0937175730" rel="noreferrer" rel="nofollow noreferrer">Posix Programmer's Guide</a>, and the best I can find is</p>
  
  <blockquote>
    <p>An array of strings called the <em>enviroment</em> is made available when the process begins.
    This array is pointed to by the external variable <code>environ</code>, which is defined as:</p>
    
    <p><code>extern char **environ;</code></p>
  </blockquote>
  
  <p>It's that <em>environ</em> variable that has me hesitating. I want to say</p>
  
  <p>-The calling process/shell has already allocated the block of null terminated strings</p>
  
  <p>-the 'external' variable <code>environ</code> is used as the entry point by <em>getenv()</em>.</p>
  
  <p>-<em>ipso facto</em> feel free to call <em>getenv()</em> within a static initializer.</p>
  
  <p>But I can't find any guarantee that the 'static initialization' of <em>environ</em> precedes all the other static initialization code. Am I overthinking this?</p>
  
  <h1>Update</h1>
  
  <p>On <em>my</em> platform (AMD Opteron, Redhat 4, GCC 3.2.3), setting <strong>LD_DEBUG</strong> shows that <em>environ</em> gets set <em>before</em> my static initializers are called. This is a nice thing to know; thanks, @codelogic. But it is not necessarily the result I'd get on all platforms.</p>
  
  <p>Also, while I agree intuitively with @ChrisW on the behavior of the C/C++ runtime library, this is just my intuition based on experience. So anyone who can pipe up with a quote from someplace authoritative guaranteeing that <em>environ</em> is there before static initializers are called, bonus points!</p>