Kenneth Brody wrote:
Skarmander wrote:
>Simon Biber wrote:
Note: Brian snipped the declaration of foo and bar.
int foo = 0;
int bar = 200000000;
Brian (Default User) wrote:
Christian Bau wrote:
memcpy (&foo, &bar, sizeof (foo)) must have exactly the same effect
as a simple assignment foo = bar.
Not exactly. The assignment may be done on an element by element basis,
which would not copy the padding bytes.
For the valid data itself, the two structs will be same of course, but
a bitwise comparison would not necessarily be the same.
That is:
memcmp(&foo, &bar, sizeof foo)
need not evaluate to 0 following foo = bar.
What if, as in this case, foo and bar are both ints? Must memcmp
evaluate to zero following foo = bar?
>No. ints can have padding bits. Assignment need not make these padding bits
>equal, since all that is required is that the "value" of 'bar' is copied to
>'foo'. Values may have multiple representations.
>
But, on systems that have padding bits, would copying them have any effect?
Does overwriting any padding bytes in structs cause any "bad" behavior?
That's a completely different matter. No, it would not, since padding bytes
in structs are never used for anything (other than meeting alignment
requirements) and can never cause trap representations like padding bits in
integers can. And even in integers, if "foo = bar" copies the padding bits
of 'bar' to 'foo' as well, that's not just perfectly legal, it's what you'd
expect most implementations to do.
The question was whether assignment and memcpy() did the same thing. They
don't. memcpy() gives you stronger guarantees than assignment.
S.