How does one maintain memory with the putenv system call? How does one maintain memory with the putenv system call? unix unix

How does one maintain memory with the putenv system call?


Don't use putenv() if you are worried about the memory leaking.

That's why POSIX provides setenv() and unsetenv() as well - those take control of the memory and manage it.


Quoting the putenv() manual page (URL above):

The putenv() function shall use the string argument to set environment variable values. The string argument should point to a string of the form "name= value". The putenv() function shall make the value of the environment variable name equal to value by altering an existing variable or creating a new one. In either case, the string pointed to by string shall become part of the environment, so altering the string shall change the environment. The space used by string is no longer used once a new string which defines name is passed to putenv().


No, you don't need to do that manually. Memory for your environment is freed by the OS (as part of your process memory) when your process exits.