To quote the man page of getenv()
:
As typically implemented, getenv() returns a pointer to a string
within the environment list. The caller must take care not to modify
this string, since that would change the environment of the process.
The implementation of getenv() is not required to be reentrant.
The string pointed to by the return value of getenv() may be
statically allocated, and can be modified by a subsequent call to
getenv(), putenv(3), setenv(3), or unsetenv(3).
https://man7.org/linux/man-pages/man3/getenv.3.html
So in the case of getenv()
you don't need to deallocate it yourself.
As the other answer mentioned, if YOU call malloc
, you are required to use free
. If you call library functions which do allocations, you call library freeing functions. If you are not sure, read their docs.