views:

42

answers:

2

Hi. With these variables:

NSInteger dataStart;
uint64_t dataSize[1];
const unsigned char *beginning;
NSInteger bytesEnd;

...at these values:

dataStart = 499
dataSize[0] = 427
beginning = 9060864
bytesEnd = 9061793

...the following code:

NSLog(@"dataStart = %d, dataSize[0] = %d, beginning = %d, bytesEnd = %d",
        dataStart, dataSize[0], (NSInteger)beginning, bytesEnd);

...sends this to the console:

dataStart = 499, dataSize[0] = 427, beginning = 0, bytesEnd = 9060864

In other words, an extra zero has been inserted after the array, bumping the other variables along. It does this consistently. I'm using xcode 3.2.3. What is happening here?

[Edit for emphasis: It's not just the old favorite of printing a zero where a value should be because a cast is wrong. It's inserting an extra zero, then printing the correct value of beginning where it should print bytesEnd, and not printing bytesEnd.]

Thanks,

+2  A: 

Just a guess, but are you compiling in 64-bit mode? The beginning pointer is 64 bits in that case, but NSInteger is 32-bits, so you're only printing the high-order 32-bit bits of the pointer which are zero.

I guess this is a contrived example because no one in their right mind would cast like this. Right?

Frederik Slijkerman
Mike Howard
Of course it's moving the other variables along: NSLog assumes that the value is 32 bits, but it is 64-bits, so it also occupies the address for the next value. If you're compiling in 64-bits mode, you shouldn't cast a pointer to NSInteger anyway.
Frederik Slijkerman
Mike Howard
-1 The answer is incorrect. Jeff Laing's answer below is almost right but wrong in 32 bit mode. datasize[0] is 64 bits wide and takes 64 bits on the stack, but with the %d you are telling NSLog it is only 32 bits wide. By the way, in 64 bit mode, NSInteger is 64 bits, so the %d is wrong for NSIntegers too.
JeremyP
@JeremyP: you're right
Frederik Slijkerman
+1  A: 

You have the wrong format selector. %d is not sufficient to print a uint64_t which is what datasize[0] is. Use %ld.

Jeff Laing
%ld is wrong for 64 bit ints in 32 bit mode.
JeremyP