Jérôme M. Berger wrote:
> Walter Bright wrote:
>> Jérôme M. Berger wrote:
>>> Actually, that problem already occurs in C. I've had problems when
>>> porting code from x86 to x86_64 because some unsigned operations
>>> don't behave the same way on both...
>> How so? I thought most 64 bit C compilers were specifically designed to
>> avoid this problem.
>
> I can't isolate it to a minimal test case, but at my job, we make
> an image processing library. Since negative image dimensions don't
> make sense, we decided to define width and height as "unsigned int".
> Now, we have code that works fine on 32-bit platforms (x86 and arm)
> but segfaults on x86_64. Simply adding an (int) cast in front of the
> image dimensions in a couple of places fixes the issue (tested with
> various versions of gcc on linux and windows).
>
Gotcha! See the attached test case. I will post the explanation for
the issue as a reply to give everyone a chance to try and spot the
error...
Jerome
--
mailto:[email protected]
http://jeberger.free.fr
Jabber: [email protected]
#include <assert.h>
#include <stdio.h>
int main (int argc, char** argv) {
char* data = argv[0]; /* Just to get a valid pointer */
unsigned int offset = 3;
printf ("Original: %p\n", data);
data += offset;
printf ("+3 : %p\n", data);
data += -offset;
printf ("-3 : %p\n", data);
assert (data == argv[0]); /* Works on 32-bit systems, fails on 64-bit */
return 0;
}
signature.asc
Description: OpenPGP digital signature
