Large pointer differences -- is this a bug
Steve Linton
sal@dcs.st-and.ac.uk
Fri Oct 19 07:39:00 GMT 2001
In a C program on x86 architecture, I have a large (>2GB) array of 4-byte
objects. Suppose I have two pointers s pointing near the start of the array
and e pointing near the end. Now suppose I compute (e-s). gcc (all versions I
have tried up to 3.01) produces code that does the subtraction and then does a
signed right shift 2, producing a value near -0.5G, instead of the value I
would like, of +0.5G. Since the correct value can be expressed in a ptrdiff_t
(an int) I feel it should be, but I can't see a clear statement in ANSI. gdb,
evaluating it interactively gives me the result I expect.
So, is this a bug?
I also started wondering what behaviour should I expect if my array contained
1 byte objects? Then ptrdiff_t is not actually a big enough type to contain
the full range of possible answers (-3G to +3G on Linux).
Steve Linton
More information about the Gcc-help
mailing list