Memory Ownership Issues
One of the difficulties involved when using dynamic memory is determining who is responsible for freeing a piece of allocated memory when nobody else is using it. This is known as memory ownership. There are many different solutions to this problem, each with their own tradeoffs and benefits, as witnessed by Java and Cocoa garbage collection, C++ RAII (resource acquisition is initialization), and Cocoa’s reference counting.
There are no uniform rules for memory ownership for Unix and C library calls, so you pretty much need to check the manpage for the calls in question (which is usually a good idea anyway). For instance:
getenv() returns a char *, but you do not need to free it since the environment variables are all ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access