Should I free/delete char* returned by getenv()?

SunnyShah picture SunnyShah · Nov 21, 2010 · Viewed 10.8k times · Source
 char * val;                                                                        
 val = getenv("ENV_VAR_NAME");

above is a code to get environment variable, will it cause memory leak if I dont free memory returned by getenv(char*) ? If no then please answer why?

Answer

icecrime picture icecrime · Nov 21, 2010

No you shouldn't. Standard 7.20.4.5 says :

The getenv function returns a pointer to a string associated with the matched list member. The string pointed to shall not be modified by the program, but may be overwritten by a subsequent call to the getenv function.

I believe deletion is covered by the text in bold.