Generally, memcpy
and memcmp
work strictly on bytes so they cannot differ.
One reading of the (C++11) standard seems to indicate it may be possible for an int
to differ from another (according to memcmp
) that you've just assigned it from, if integers are allowed to have padding bytes which have no effect on the value.
It would seem to be feasible as per your code with an int
and similarly-sized char
buffer:
int a = 0;
char b[sizeof(int)];
memcpy(b, &a, sizeof(int));
for the padding bytes (if any) in a
to change in such a way that the underlying value does not change. That could cause a memcmp
to fail.
That particular reading can be found in C++11 3.9.1 Fundamental types
:
For character types, all bits of the object representation participate
in the value representation. For unsigned character types, all possible bit patterns of the value representation represent numbers. These requirements do not hold for other types.
That allows for the possibility of padding bits within non-character types and there's nothing in the standard explicitly preventing those bits from changing at any time.
However, in that same section, it lumps the character and signed or unsigned integers into a "integral type" category and states that the:
representations of integral types shall define values by use of a pure binary numeration system. (footnote 49) [Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. —end example ]
Footnote 49 state:
A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position. (Adapted from the American National Dictionary for Information Processing Systems.)
That doesn't seem to leave the possibility open for padding bits in these types at all, because it very specifically calls out successive bits and powers of two, with the only exception specifically mentioned being the high bit (used for deciding sign for the three possible encodings) (a).
So I suspect that memcmp
will not be able to fail immediately following a memcpy
using the same memory blocks and size.
That's totally irrelevant in the question you link to, of course, since there's an intervening operation, delete
, which is free to change the underlying bit pattern. That situation is no different to:
int a = 0;
char b[sizeof(int)];
memcpy(b, &a, sizeof(int));
a = 42; // intervening operation
after which a memcmp
would be pretty much guaranteed to consider the two memory blocks as different.
(a) Annoyingly, there is one potential reading allowing for padding bits while still satisfying the "successive" bits and powers-of-two mentioned above - that's if the padding bits are at the low end of the underlying bit pattern (furthest from the sign). If that were allowed then, yes, memcmp
immediately after memcpy
could report a difference.