When we do setenv()
or putenv()
, the book 'Advanced Programming in the UNIX Environment' says that when we modifying value of environment lists, it's complicated.
Basically, environment lists are stored at the top of stack.
However, the book says, in name=value
structure, if we change the old value of a name to a new value, which is larger than old value, we must malloc()
to obtain room for the new string, copy the new string to this area, and then replace the old pointer in the environment list for corresponding name with the pointer to this allocated area (it must be heap area, because we use malloc()
).
But, basically environment lists are stored at the top of the stack. So, new value has to be located at here, not heap area (area of malloc()
). So, why this value is at heap area, not top of the stack?
There is the char** environ
global variable, and this double pointer points array of pointers which points at name=value
strings. I thought this name=value
strings are one of the data which is pointed by environ
's array of pointer. This is why I can't understand that array of pointer points heap area (which stores new value in above situation).