Small query regarding function "char * strerror(int errnum)"

As this function returns the address of the string corressponding to the errno value provided to it. Can someone please let me know where, in the memory, it could be (on freeBSD).

The MAN page tells under the BUG section that "For unknown error numbers, the strerror() function will return its result in a static buffer which may be overwritten by subsequent calls."

But it doesn't explictly mentions of anything, under normal conditions, of the storage location of the string that whether its the 'heap' area or some system array which holds the 'string object' whose address is returned by the function strerror().

Additionally what is sys_errlist in freeBSD and how to access it directly? Is it the array/table which holds the strings corressponding to the errno whose address is returned by the function strerror() (the MAN pages didn't tell this explictly); if yes then I have the following further questions:

1) How it is instantialized? Is it unique for every process? When it is instantialized/initialized ?
2) Can it be edited from a program (If not under RO data section) ?

Where exactly the string is stored is implementation-defined and shouldn't matter to you as an application developer.

sys_errlist is in stdio.h (see 'man sys_errlist'). It is initialized before your main() function is called. I don't know whether it's "editable" or not - frankly I cannot think of any reason that this would be anything other than a terrible, terrible idea.

What exactly are you trying to achieve here - what's the end-goal for this line of enquiry?

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 function 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 those few liner examples (sometimes they might just overlook error handling part just to give us the feel of the function).

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 to forward this query to the forum.

They do no such thing. Pointers do not work that way! You cannot "return an address" through a pointer you pass into a function, any more than you can return a number through a number you pass into a function.

You do not need to clean up after strerror -- or stat, lstat, or fstat, either.

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.

The man page tells you that the result may be in a static buffer. It tells you this to let you know two things:

  1. You do not need to free the result returned from this function. A static buffer (as in when you use "static" within a function) exists throughout the lifetime of the program and does not need to be released by anyone.
  2. You should not rely on the result being available over multiple calls to strerror(), as in:
    text char *err1 = strerror(errno); some_call(); char *err2 = strerror(errno); printf("Got errors %s and %s\n", err1, err2);

    This is because it's possible that the result of both calls to strerror() will be a pointer to the same (static) buffer.

Some advice: When asking questions on forums, ask what you really want to ask, as opposed to second-guessing what you want. When you ask where the result of strerror() will be and if you can edit it, my first instinct is to just say "no, don't do any of that forget about it!", but if you just ask whether you should free() the result and why the man page mentions a static buffer, you'll get a much better response.