Hello everyone,


For the following code, on x86 build, the output is

000000C8
000000C8

and on x64 build, the output is

CCCCCCCC000000C8
00000000000000C8

I think the reason is, for x64, both void* and void** are 64bit, for for x86
void* and void** are 32 bit, and for int, always 32bit for both x86 and x64.

This line of code "void* b2 = *b1", dereference b1 and makes it into 64-bit
on x64, which concatenate value of a 0x000000C8 and previous DWORD
0xcccccccc, so forms the result 0xCCCCCCCC000000C8.

But on x86, when dereference b1 and makes it into 32-bit on x86, and
recovers to original value 0x000000C8.

In my understanding correct?

[Code]
#include <iostream>

using namespace std;

int main()
{

int a = 200;

void** b1 = (void**)&a;

void* b2 = *b1;

int b3 = (int)*b1;

cout << (void*)b2 << endl;

cout << (void*)b3 << endl;

return 0;
}
[/Code]


thanks in advance,
George

Re: different result on x86 and x64 by Jochen

Jochen
Thu Oct 09 01:56:24 CDT 2008

Hi George!

> void** b1 = (void**)&a;
> void* b2 = *b1;

If you have a bug in your code, then it is expected that you get problems...

int != void*
int => 32 bit
void* => (depends on target)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Re: different result on x86 and x64 by George

George
Thu Oct 09 02:08:00 CDT 2008

Hi Jochen,


I am just using short code to simulate my big program to reproduce this
issue. I agree with your words and it is why my small code looks ugly. :-)

Anyway, what is your answer to my original question?


regards,
George

Re: different result on x86 and x64 by Jochen

Jochen
Thu Oct 09 02:27:39 CDT 2008

Hi George!

> Anyway, what is your answer to my original question?

You cast a 32 bit word into an 64-bit pointer. Therefor the upper 32
bits are not initialized (0xcc).
So your understanding is correct.
Never use int when you store pointers!

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Re: different result on x86 and x64 by George

George
Thu Oct 09 02:43:02 CDT 2008

Thanks Jochen,


Question answered.


regards,
George

Re: different result on x86 and x64 by karpov2007

karpov2007
Thu Oct 09 05:50:40 CDT 2008

The Viva64 code analyzer find next errors:

int main()
{
int a = 200;
void** b1 = (void**)&a; // V114: Dangerous explicit type pointer
conversion.
void* b2 = *b1;
int b3 = (int)*b1; //V202: Explicit type conversion. Type casting
from memsize to 32-bit.
cout << (void*)b2 << endl;
cout << (void*)b3 << endl; //V201: Explicit type conversion. Type
casting to memsize.
return 0;
}

p.s.
Viva64 - product provides detection of errors typical of 64-bit
Windows applications.
http://www.viva64.com/viva64-tool/

Re: different result on x86 and x64 by George

George
Thu Oct 09 06:16:00 CDT 2008

Thanks karpov2007,


Looks like a good and smart tool, VC does not report this issue. I am using
VSTS 2008.

Anyway, do you agree with my analysis in my original post? :-)


regards,
George

Re: different result on x86 and x64 by karpov2007

karpov2007
Thu Oct 09 08:56:23 CDT 2008

> Anyway, do you agree with my analysis in my original post? :-)

Yes, I am agree. It is widespread enough error. It it is linked to
conversion of type of arrays.
For example the similar situation is described in article "20 issues
of porting C ++ code on the 64-bit platform" (Item 8. Changing an
array type).
http://www.viva64.com/content/articles/64-bit-development/?f=20_issues_of_porting_C++_code_on_the_64-bit_platform.html&lang=en&content=64-bit-development#IDA0FD1E

Re: different result on x86 and x64 by George

George
Fri Oct 10 02:07:00 CDT 2008

Thanks karpov2007,


I like your recommended your article. :-)


regards,
George