What is the difference between
*(unsigned*)d = *(unsigned*)s;
d+=4; s+=4;
*(unsigned*)d = *(unsigned*)s;
d+=4; s+=4;
and
*(unsigned long*)d = *(unsigned long*)s;
d+=8; s+=8;
on 64bit systems?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Provided that nothing unpleasant happens in respect of padding bits or strict aliasing rules, and assuming the sizes of the types are as you expect, and provided that the memory regions don’t overlap, and are correctly aligned, then they each copy 8 bytes from one place to another.
Of course, aside from the practical effect there may be a difference in performance and/or code size.
If you’re seeing something break, then look at the actual code emitted, that might tell you what has gone wrong. Unless you have a lot of optimization switched on, and maybe even with optimization, I don’t immediately see why those wouldn’t be equivalent with AMD64, Ubuntu, and gcc.
Things I’ve mentioned that could go wrong:
unsignedanunsigned longto have padding bits, and if so then there could be bit patterns which are trap representations of one or both, which could explode as soon as you dereference.sanddare the result of casting pointers-to-double touint8_t*, and you look at the resulting double, then in one or both cases you might not see the effects of the change because you have an illegal type-pun.sizeof(long) == 4then the two aren’t equivalent.longis 32 bits on 64bit Windows systems, just not 64bit Linux ones.d == s + 4, then the two code snippets have different effect. Because of this, you won’t see the first optimized to become the second unless the compiler knows thatdandspoint to entirely different places (and that’s what C99restrictis for).sordis correctly aligned forintbut notlongthen there’s a difference. (Edit: apparently you can enable or disable hardware exceptions for unaligned access on x86-64).