3 ms·
You don't free the pointer returned by getenv(), because the environment variables are already in memory and getenv() is just giving you a pointer to one of the
by juped 4y ago
You don't free the pointer returned by getenv(), because the environment variables are already in memory and getenv() is just giving you a pointer to one of their values.
The most comfortable way to do a C API is to make the caller allocate space for return values (that aren't something simply copiable like int), and take a pointer to it as a parameter. The few standard library functions that malloc things are annoying, because you might not want to do that.
- c-smile 4y agogetenv() is just a sample. But even with it... it puts some limitation on potential API implementation and overall system performance. E.g. const char* user_password() cannot store the data anywhere, right? All that... > The most comfortable way to do a C API is to make the caller allocate space for return values That's even worse. How will caller know size of the buffer upfront? With the callback approach that is trivial - you get the size on call - no need to call the API function twice - for size of the buffer and then for real copy.
- saagarjha 4y agoYeah, it’s easy until someone calls setenv ;)