I was looking at public-domain implementations on wikibooks.org. It implements memmove() as following explicitly stating that it is "not completely portable"! I was wondering as to why:
The code is as follows:
void *(memmove)(void *s1, const void *s2, size_t n)
{
char *p1 = s1;
const char *p2 = s2;
if (p2 < p1 && p1 < p2 + n) {
/* do a descending copy */
p2 += n;
p1 += n;
while (n-- != 0)
*--p1 = *--p2;
} else
while (n-- != 0)
*p1++ = *p2++;
return s1;
}
The specification of function memmove()
is that it can handle overlapping source and destination, but the specification does not say that memmove()
has to be called with pointers to the same memory block (“object” in the standard's parlance).
When p1
and p2
are pointers to different memory blocks, the condition p2 < p1
is undefined behavior. The C99 standard says (6.5.8:5):
When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object or incomplete types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.
I don't know if this is what the explanation refers to, but it is one definite source of non-portability.
A different implementation might use (uintptr_t)p2 < (uintptr_t)p1
. Then the comparison <
is a comparison between integers. The conversion to uintptr_t
gives implementation-defined results. The type uintptr_t
was introduced in C99 and is an unsigned integer type guaranteed to hold the representation of a pointer.
A completely portable implementation of memmove()
might use a third buffer to hold an intermediate copy, or use ==
comparison (which gives specified results in the context in which it would have to be used).
The explanation of the parentheses is here: What do the parentheses around a function name mean?
It's not portable because of the p2 < p1
and p1 < p2 + n
comparisons. The C standard only defines the behavior of pointer comparisons when the two pointers point into the same object. This code depends on them working reasonably even when you're copying between different objects.
In a practical sense, the code is fine. When the pointers don't point to the same object, it doesn't matter whether the copy is done ascending or descending, so the results of the comparisons are irrelevant. All that matters is that the code doesn't do something really horrible, like crash the process or send the nuclear launch codes. The C standard doesn't prohibit this, but it's not likely in any realistic implementation. Most implementations simply compare the raw addresses, and any weird implementations will just return an unpredictable value, but not have any side effects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With