 
                            ...
| Code Block | 
|---|
| main(int argc, char *argv[], char *envp[]) | 
Wiki Markup envp}}  now   references   an   incorrect   location.  
For example, POSIX says the following: \[[Open Group 04|AA. C References#Open Group 04]\]
Unanticipated results may occur if
setenv()changes the external variableenviron. In particular, if the optionalenvpargument 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).
| Wiki Markup | 
|---|
| Microsoft notes the following about {{getenv()}}: \[[MSDN|AA. C References#MSDN]\] | 
getenvand_putenvuse the copy of the environment pointed to by the global variable_environto access the environment.getenvoperates only on the data structures accessible to the runtime library and not on the environment "segment" created for the process by the operating system. Consequently, programs that use theenvpargument tomainorwmainmay retrieve invalid information.
| Wiki Markup | 
|---|
| The Visual C++ reference notes the following about {{envp}} \[[MSDN|AA. C References#MSDN]\] | 
The environment block passed to main and wmain is a "frozen" copy of the current environment. If you subsequently change the environment via a call to putenv or _wputenv, the current environment (as returned by getenv/_wgetenv and the _environ/ _wenviron variable) will change, but the block pointed to by envp will not change.
When example, when compiled with GCC version 3.4.6 and run on a 32-bit Intel GNU/Linux machine, the following code:
...
Non-Compliant Code Example (POSIX)
| Wiki Markup | 
|---|
| After a call to the POSIX {{setenv()}} function, or other function that modifies the environment, the {{envp}} pointer may no longer reference the environment. POSIX states that \[[Open Group 04|AA. C References#Open Group 04]\] | 
Unanticipated results may occur if
setenv()changes the external variableenviron. In particular, if the optionalenvpargument 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).
| Code Block | ||
|---|---|---|
| 
 | ||
| 
int main(int argc, char const *argv[], char const *envp[]) {
   size_t i;
   if (setenv("MY_NEW_VAR", "new_value", 1) != 0) {
     /* Handle Error */
   }
   if (envp != NULL) {
      for (i = 0; envp[i] != NULL; i++) {
         puts(envp[i]);
      }
   }
   return 0;
}
 | 
...
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. 
| Wiki Markup | 
|---|
| According to the Visual C++ reference \[[MSDN|AA. C References#MSDN]\] | 
The environment block passed to
mainandwmainis a "frozen" copy of the current environment. If you subsequently change the environment via a call toputenvor_wputenv, the current environment (as returned bygetenv/_wgetenvand the_environ/_wenvironvariable) will change, but the block pointed to byenvpwill not change.
| Code Block | ||
|---|---|---|
| 
 | ||
| 
int main(int argc, char const *argv[], char const *envp[]) {
   size_t i;
   if (_putenv_s("MY_NEW_VAR", "new_value", 1) != 0) {
     /* Handle Error */
   }
   if (envp != NULL) {
      for (i = 0; envp[i] != NULL; i++) {
         puts(envp[i]);
      }
   }
   return 0;
}
 | 
...