S
Stephen Sprunk
James Kuyper said:On 04/02/2014 02:31 AM, jay wrote: [...]I read about strlen() in C standard (n1570) in 6.24.6.3, it does not
have any error condition associated with it.
Correct, strlen() provides no way of reporting errors. There's only one
possible error: passing it a pointer to a block of memory that is not
terminated by a null character. strlen() will continue looking for
characters until it reaches the end of that block of memory - what it
does after that point depends upon how your system works; as far as the
C standard is concerned, the behavior is simply undefined.
There are other possible errors: Passing it a null pointer, and
passing it an invalid non-null pointer.
Implementations of strlen are not required to detect any of these
errors; they all have undefined behavior. (It would be easy to
detect a null pointer argument, but the overhead might be an issue,
and on most systems it's likely to cause a run-time fault anyway
-- and it's not clear what it should return for a null pointer
argument anyway.)
This exact scenario was my first exposure to portability problems and
the need to know what C actually guarantees vs. what a given
implementation happens to do: strlen(NULL) returned 0 on AIX but
segfaulted on Linux.
S