-20

Well this question is about C and C++ as strcmp is present in both of them.

I came across this link: C library function - strcmp().

Over here it was explained the return values of strcmp. I know that every function, how much ever safe it is, can fail. Thus, I knew that even strcmp can fail at some time.

Also, I came across this question which also explained the return values of strcmp. After searching a lot, I could not find a website which explained how to check if strcmp could fail.

I first had a thought that it would return -1, but it turned out that it returns numbers < 0 if the first string is smaller. So can someone tell me how to check if strcmp has failed.

EDIT: well, I do not understand the point of strcmp not failing. There are many ways in which a function fails. For example, in one comment, it was written that if a stack doesn't extend, it might cause a stack overlow. No program in any language is absolutely safe!

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
  • 3
    How may it fail? If 2 strings are given as parameters, isn't there only 3 options about a comparison between them? – Aycan Yaşıt Feb 13 '16 at 07:31
  • how do you imagine a situation that it fails? Except when a null pointer is passed, otherwise it's just a comparison that returns how the strings compares – phuclv Feb 13 '16 at 07:31
  • It will fail if the strings aren't null terminated? – Czipperz Feb 13 '16 at 07:33
  • 3
    The only ways `strcmp()` can fail are if you pass one or two null pointers, or if the 'strings' are not properly null terminated (but they need to be similar chunks of data because `strcmp()` stops as soon as there's a difference). However, that invokes undefined behaviour so anything can happen and it is valid. – Jonathan Leffler Feb 13 '16 at 07:34

4 Answers4

14

There is no defined situation where strcmp can fail.

You can trigger undefined behavior by passing an invalid argument, such as:

  • a null pointer
  • an uninitialized pointer
  • a pointer to memory that's been freed, or to a stack location from a function that's since been exited, or similar
  • a perfectly valid pointer, but with no null byte between the location that the pointer points to and the end of the allocated memory range that it points into

but in such cases strcmp makes absolutely no guarantees about its behavior — it's allowed to just start wiping your hard drive, or sending spam from your e-mail account, or whathaveyou — so it doesn't need to explicitly indicate the error. (And indeed, the only kind of invalid argument that strcmp really could detect and handle, in a typical C or C++ implementation, is the null-pointer case, which you could just as easily check before calling strcmp. So an error indicator would not be useful.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
7

If you look up strcmp in some documentation, you will find something like

The behavior is undefined if lhs or rhs are not pointers to null-terminated strings.

This means that it can crash, or cause small red daemons to fly out of your nose, or whatever. Since undefined behavior can be that what you innocently thought would happen (nothing wrong, as you see it), happens, there's no sure fire way to recognize it. Although you'll recognize a crash.

So, you as a programmer can't check for undefined behavior after the fact.

But the compiler can add such checks for you, for many kinds of undefined behavior including this, because it's in full charge of that behavior. Whether it will do so depends on the compiler. Also, you can add checks in e.g. a wrapper function, that minimizes the chance of undefined behavior, although with non-terminated strings there's no practical way of checking that I know of that won't itself possibly invoke undefined behavior.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

Possible failure is undefined behaviour, and may happen if you give wrong arguments
(not pointers to null-terminated strings).

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
0

strcmp returns 0 if and only if the both strings are properly zero-terminated, and have the exact same characters.

If they both are not exactly the same, a non-zero value is returned. If the return value is negative, it means that the first string comes first in lexicographical ordering; if positive, it means that the second string comes first in lexicographical ordering.

There is no "failure", except possible undefined behaviour, if invalid input is provided - then, anything could happen, including a program crash, or compiler generating invalid code.