Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

64-bit puzzle

The computer industry is changing, and 64-bit technology is the next, inevitable step.
Look at sample.

int main(int, const char **) {
int a = -2;
unsigned b = 1;
int array[5] = { 1, 2, 3, 4, 5 };
int *ptr = array + 3;
ptr = ptr + (a + b); //Invalid pointer value on 64-bit platform
printf("%i\n", *ptr); //Access violation on 64-bit platform
return 0;
}
Do you know why this code does not work on 64-bit system? [ad removed by moderator]
[500 byte] By [Karpov2007] at [2007-11-11 10:19:13]
# 1 Re: 64-bit puzzle
First off, we do not allow the posting of commercial products and sites here, so I removed that part from your original message. However, this is an interesting discussion seed so I didn't remove it altogether.

Your program exhibits undefined behavior even on 32 bit systems I'm afraid, so I wouldn't even dream of porting it to a 64 bit env as is. The problem is of course the addition of a+b, which returns an unsigned result (which obviously can't be negative).
The result is 4294967295. When added to a 32 bit ptr, the rollover causes the pointer to retain a valid value, although it's a very bad way of doing pointer arithmetic.
With a 64 pointer, the result is of course a pointer that is 4294967295 integers away from ptr, which is outside the address space of the program.
Danny at 2007-11-11 20:59:08 >
# 2 Re: 64-bit puzzle
Unfortunately the similar code really exists in many appendices. It is not only my ideas. I recommend as an example:

"Kang Su Gatlin - On the 64-bit Whiteboard" (http://channel9.msdn.com/ShowPost.aspx?PostID=53939)
Now we get into some 64-bit code on the whiteboard and learn some things you need to think about when you move to 64-bit systems. Kang Su Gatlin is a program manager on the Visual C++ team and recently spoke as part of the Route 64 Training Tour.

"Peculiarities of the Development of 64-bit Applications" (http://www.viva64.com/articles.php)
Evgeniy Ryzhkov.

"Porting Linux applications to 64-bit systems" (http://www-128.ibm.com/developerworks/library/l-port64.html)
Harsha S. Adiga
Karpov2007 at 2007-11-11 21:00:02 >