nounoursnoir
nounoursnoir

Reputation: 703

What, exactly, is memcmp supposed to return?

I would like to know what the function memcmp must return.

I've been searching on the Internet, and usually, memcmp definitions state something like the following:

The memcmp() function returns an integer greater than, equal to, or less than zero, accordingly as the object pointed to by s1 is greater than, equal to, or less than the object pointed to by s2.

It is never explicitly said what, exactly, is returned: is it the difference between two byte values, or is it -1, 0 or 1? I am confused:

Since I can't get a precise enough definition of the function memcmp, I ask this question here: what, exactly, is the function memcmp supposed to return? Is there an "official" source code somewhere? (I have seen a lot of source codes for memcmp but none gave me an answer: I then suppose that they are not the function that is written in the library string.h, at least not on my computer...)

Upvotes: 2

Views: 3354

Answers (4)

ad absurdum
ad absurdum

Reputation: 21361

The particular values returned by memcmp() are not specified by the Standard. The C11 Draft Standard does say, in §7.24.4 1:

The sign of a nonzero value returned by the comparison functions memcmp, strcmp, and strncmp is determined by the sign of the difference between the values of the first pair of characters (both interpreted as unsigned char) that differ in the objects being compared.

So only the sign of nonzero return values from the comparison functions should be taken as meaningful. The latitude given here allows each implementation to interpret these requirements as it sees fit.

Also, note that there is no "official source code"; the Standard is the document that C implementations must adhere to. Even reading the source code for the implementation that you are using to find the underlying method used to generate the memcmp() return values, use of these values in code is at best not portable, and is vulnerable to future changes in that implementation.

Upvotes: 12

pm100
pm100

Reputation: 50210

The reason for not specifying the exact results are

First the exact result doesnt matter. The caller just needs to know one of three results <, =, or >. The defined behavior works. Now the spec could have said return -1, 0, or 1. So why does it matter that this is not said. See second point

Second. By not specifying the exact result the implementer can write code that is very efficient. memcmp could be implemented by counting bits or doing some clever and. or. xor etc that does not naturally produce 1 or -1. So the spec is silent on the exact return value.

Upvotes: 5

alinsoar
alinsoar

Reputation: 15813

It does not specify what integer will return, it specifies that the result can be compared with 0.

Any values returned by an implementation is correct if it accomplish this test.

Upvotes: 2

cxw
cxw

Reputation: 17051

As @EugeneSh said, it is not defined. The POSIX specification says, in addition to the portion you quoted:

The sign of a non-zero return value shall be determined by the sign of the difference between the values of the first pair of bytes (both interpreted as type unsigned char) that differ in the objects being compared.

Therefore, only zero/nonzero and positive/negative are meaningful tests to apply to the return value from memcmp. Don't rely on the actual values, since they may differ between different C libraries (or maybe even processor architectures).

Source example

I found a mirror of the GNU C library (glibc) that someone has put on GitHub. The source for memcmp takes the difference between the two bytes (line 332), so the return value will generally not be only -1 or +1. However, a particular library may implement memcmp however makes the most sense for the target platform.

Upvotes: 2

Related Questions