Does strlen include the null




















It causes undefined behavior which means anything can happen, most likely your program will crash. If you're lucky, it won't cause a crash :. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.

Ask Question. Asked 12 years, 10 months ago. Active 1 year, 3 months ago. Viewed 34k times. Improve this question. Ask yourself: How should strlen know where your string ends, if it's not null-terminated? Technically speaking, there is no such thing as a C string that is not NUL-terminated, because the NUL-Terminator is defined to be part of the C string : — fredoverflow.

Haev you considered consulting the documentation? If you know the buffer size, you might want to use memchr instead, which returns null pointer if no null terminator is found, or strnlen to cap the max size of string. Add a comment. Active Oldest Votes. Improve this answer. Hosam Aly Hosam Aly What does "do nothing" mean to you?

No, strlen expected a pointer to a string as an argument. And a null pointer does not point to a string. SM Ryan. Is it that strlen function can't handle NULL pointers? No, it doesn't. You can define your own string length function which does. When I'm developing I use the debug library. Gordon Burditt. Example: the first argument of strtok. Barry Schwarz. Yes but you can't call it strlen if it has external linkage since that name is reserved.

Remove del for email. Anything your program does at this point is not unexpected or perhaps it should be phrased that at this point you are no longer entitled to have any expectations.

Yes but. Even if some compiler included a strlen function in its run time library that did handle NULL pointers, the standard still states the behavior is undefined. To be slightly more accurate, strlen expects a pointer to a string.

Does NULL point to a string. I disagree. It should be recommended. If not, then what you are saying is that it's better to rely on the results of undefined behavior of strlen NULL and all it's perverse consequences than to rely on the "crash"? Best regards -- jay. That would be the best option. Dik T. How about strcmp NULL, ""?

One can never test every possible condition in complex real-world software, and the consequences of your validation being incomplete are too severe to risk a crash no matter how sure you are you got it right. Deliberately choosing to kill people is likely to bankrupt you, if not put you in jail. Pedantism is for those who don't have to face the consequences, or those whose work doesn't matter. Now, say there's a bug that causes the agents' phones to crash randomly due to trapping on strlen NULL.

It's one thing for that to be an accident, in which case the phone vendor is likely to only face a few million dollars per victim in fines and civil suits, but if it came out that the crash was deliberate when there was the option of handling the error without hanging up on someone who was being raped or assaulted, well, let's just say juries wouldn't be very sympathetic to those responsible.

And this isn't such a hypothetical example. I've seen it actually happen to two different vendors, though NDAs prevent me from saying who or when. How about strcmp NULL, "" thats a good one. My intuition would say that they would compare unequal. Now I see why it was decided to not let the string functions have defined behaviour when being passed NULL.

My intuition would be that -- assuming that the call doesn't segfault -- it'd compare equal. That's the more logical thing to me, since novice programmers don't see a difference between NULL and "", and certain things get a lot simpler if you require that to be true. My guess is that, like many other things in C, the behavior prior to C89 was divergent, so ANSI decided it was better left undefined to avoid forcing implementors to change their code.

The latter is necessarily false; the former is allowed by the C standard since the behavior is undefined. Silencing compiler warnings or errors with explicit casts can get you into real, terrible, trouble. Richard Bos. Database developers burst out in tears. FYI, there's a world of difference between "there's no valid data for this item yet " and "the valid data for this item is blank". Presently, one must test both arguments against NULL anyways before calling strcmp because failing to do so may cause a crash.

What I suggest reduces the amount of work for most folks and makes it no worse for the rest. Richard Tobin. This is only needed if there is some possibility that one of them might be null, which is almost never the case in my code. And if I were writing a library function that passed one of its arguments to strcmp , then I would only test it if the function was supposed to accept null; if not then I would leave it to strcmp to crash.

This is a quality-of-implementation issue. Charles Richmond. This discussion thread is closed Start new discussion. Null Argument shouldnt be? Viewed 54k times. Improve this question. So what string did you enter, and what was the result? Roger Rowland is correct. Add a comment. Active Oldest Votes. For example: You put in apple. Improve this answer.

Arxo Clay Arxo Clay 6 6 silver badges 12 12 bronze badges. But the compiler warns that gets is deprecated and dangerous. Of course this question is for five years ago, just saying. Semaphore 63 1 1 silver badge 11 11 bronze badges.

As per the man page use: man fgets on linux terminal, fgets reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Sign up or log in Sign up using Google. Sign up using Facebook.



0コメント

  • 1000 / 1000