Some environments provide environment pointers that are valid when main()
is called, but may be invalided by operations that modify the environment.
According to C99 \ [[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Wiki Markup
In a hosted environment, the main function receives a third argument, {{ Wiki Markup char
\*envp
\[
\]
}}, that points to a null-terminated array of pointers to {{char
}}, each of which points to a string that provides information about the environment for this execution of the program.
Consequently, under a hosted environment it is possible to access the environment through a modified form of main()
:
...
Noncompliant Code Example (POSIX)
After a call to the POSIX {{ Wiki Markup setenv()
}} function, or another function that modifies the environment, the {{envp
}} pointer may no longer reference the environment. POSIX states that \ [[Open Group 2004|AA. Bibliography#Open Group 04]\]
Unanticipated results may occur if
setenv()
changes the external variableenviron
. In particular, if the optionalenvp
argument tomain()
is present, it is not changed, and as a result may point to an obsolete copy of the environment (as may any other copy ofenviron
).
...
After a call to the Windows _putenv_s()
function, or other function that modifies the environment, the envp
pointer may no longer reference the environment.
According to the Visual C++ reference \ [[MSDN|AA. Bibliography#MSDN]\] Wiki Markup
The environment block passed to
main
andwmain
is a "frozen" copy of the current environment. If you subsequently change the environment via a call toputenv
or_wputenv
, the current environment (as returned bygetenv
/_wgetenv
and the_environ
/_wenviron
variable) will change, but the block pointed to byenvp
will not change.
...
ISO/IEC 9899:1999 Section J.5.1, "Environment Arguments"
Bibliography
\[[MSDN|AA. Bibliography#MSDN] \] [{{ Wiki Markup getenv,
_wgetenv
}}|http://msdn.microsoft.com/en-us/library/tehxacec.aspx], [{{_environ,
_wenviron
}}|http://msdn.microsoft.com/en-us/library/stxk41x1.aspx], [{{_putenv_s,
_wputenv_s
}}|http://msdn.microsoft.com/en-us/library/eyw7eyfw.aspx]
\[[Open Group 2004|AA. Bibliography#Open Group 04]\] [{{setenv()}}|http://www.opengroup.org/onlinepubs/009695399/functions/setenv.html]
[Open Group 2004] setenv()
...
10. Environment (ENV) ENV32-C. All atexit handlers must return normally