Quote:
Originally Posted by
Praveen_218
John,
I was confused especially after reading the MAN page itself that mentioned of memory location for “unknown error numbers” (which I referred above) but didn't talk of the memory location of the address returned by this system call under normal circumstances.
The primary reason to ask this query is because of the fact that the MAN pages themselves not making me sure if the programmer needs to handle any kind of cleanup post calling strerror()? Just like the call to stat(), lstat(), fstat() which create objects of type struct stat on the heap itself (whose address is returned through the pointer struct stat *sb) and puts the onus of the cleanup on the programmer only.
Available examples too didn't give the clarity as they all called the function strerror() and used the address returned into their processing and were of just few lines only. Hence I was not getting sure of the error handling aspect of the few liner examples.
The code I was working was critical enough not to have scope for silly errors. Hence couldn't have overlooked the cleanup part if the allocation happens to be on the heap and couldn't have just called to free the address either if it belonged to a different memory region.
Hope you got the dilemma making me forward this query to the forum.
If you call strerror(errno), after getting an error indication from a function that sets errno, strerror() will probably return a pointer to a statically allocated string. If you call strerror(
random) with a value that is not a valid error number on your system, strerror() may return a pointer to a string like "Unknown error number xxx" where xxx is
random converted to a decimal digit string. The application calling strerror() doesn't need to free the memory to which the return value from strerror() points in either case. But, if you intend to call strerror() multiple times and print the results later, you need to allocate space for a buffer to hold the returned string, copy it to that buffer, and free the buffer when you are done with it.