Take the 2-minute tour ×

Why is strlen() not checking for NULL?

if I do strlen(NULL), it seg faults.

Trying to understand the rationale behind it (if any).

share|improve this question
add comment

6 Answers

up vote 15 down vote accepted

The rational behind it is simple -- how can you check the length of something that does not exist?

Also, unlike "managed languages" there is no expectations the run time system will handle invalid data or data structures correctly. (This type of issue is exactly why more "modern" languages are more popular for non-computation or less performant requiring applications).

A standard template in c would look like this

 int someStrLen;

 if (someStr != NULL)  // or if (someStr)
    someStrLen = strlen(someStr);
    // handle error.
share|improve this answer
Thanks Hogan. Does that mean, the caller should check for NULL before passing the object to strlen()? something like: if (object) len =strlen(object) else len=-1 –  hari Apr 26 '11 at 20:41
"Managed"... That's right. Imagine every function begin very paranoid and checking for every possible mistake. Printf storing meta-information for every argument in the list, every math operation checking for overflow etc. That's managed. –  user405725 Apr 26 '11 at 20:42
@hari - yes, that means you need to check that your pointer is valid before calling strlen. –  John Bode Apr 26 '11 at 20:55
@hari - yes exactly, I've updated the answer with a template. –  Hogan Apr 26 '11 at 20:58
@R I guess we are not in agreement about what "standard template" means. Maybe you would prefer "useful pattern"? If you feel better with this term, I'm fine with it. –  Hogan Feb 5 '12 at 18:44
show 3 more comments

The portion of the language standard that defines the string handling library states that, unless specified otherwise for the specific function, any pointer arguments must have valid values.

The philosphy behind the design of the C standard library is that the programmer is ultimately in the best position to know whether a run-time check really needs to be performed. Back in the days when your total system memory was measured in kilobytes, the overhead of performing an unnecessary runtime check could be pretty painful. So the C standard library doesn't bother doing any of those checks; it assumes that the programmer has already done it if it's really necessary. If you know you will never pass a bad pointer value to strlen (such as, you're passing in a string literal, or a locally allocated array), then there's no need to clutter up the resulting binary with an unnecessary check against NULL.

share|improve this answer
+1 for "If you know you will never pass a bad pointer value". –  Blagovest Buyukliev Apr 26 '11 at 21:32
add comment
size_t strlen ( const char * str );


Strlen takes a pointer to a character array as a parameter, null is not a valid argument to this function.

share|improve this answer
add comment

The standard does not require it, so implementations just avoid a test and potentially an expensive jump.

share|improve this answer
add comment

A little macro to help your grief:

#define strlens(s) (s==NULL?0:strlen(s))
share|improve this answer
add comment

Three significant reasons:

  • The standard library and the C language are designed assuming that the programmer knows what he is doing, so a null pointer isn't treated as an edge case, but rather as a programmer's mistake that results in undefined behaviour;

  • It incurs runtime overhead - calling strlen thousands of times and always doing str != NULL is not reasonable unless the programmer is treated as a sissy;

  • It adds up to the code size - it could only be a few instructions, but if you adopt this principle and do it everywhere it can inflate your code significantly.

share|improve this answer
Some standard C functions do check for NULL inputs, so the first reason is bogus. The third reason is also bogus because putting a few extra checks in the library adds less to code size (on a typical, non-embedded platform) than all the checks inserted in client code. –  larsmans Apr 26 '11 at 21:02
@larsmans: reason one wasn't an ultimate statement but rather an attempt to describe the prevailing mindset in C programming; reason three makes sense when you are sure that the pointer can't be NULL in the client code and such a check acts more like an assert statement. –  Blagovest Buyukliev Apr 26 '11 at 21:21
@larsmans: oh, but most functions that check for NULL are on "newer" parts of the standard (e.g: mb*, wc*), aren't they? –  ninjalj Apr 26 '11 at 21:25
@ninjalj: And checking for NULL is actually the biggest flaw in the wc/mb interfaces. A common need with these functions is to process a single byte/character at a time, and performing multiple useless null pointer checks on each call can easily double the time spent in them. –  R.. Feb 5 '12 at 1:22
@R..: sure, I was just pointing out that the existence of those functions doesn't really constitute a counter-example of Blagovest's first point. –  ninjalj Feb 14 '12 at 23:52
add comment

Your Answer


