• Advertisement
Sign in to follow this  

[VC++] "Pointers are 64-bit on 64-bit platforms, and you will truncate the pointer value if you assign it to a 32-bit variable"

This topic is 2176 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here we are: http://msdn.microsof...y/3b2e7499.aspx

When you use Visual C++ to create applications to run on a 64-bit Windows operating system, you should be aware of the following issues:


An int and a long are 32-bit values on 64-bit Windows operating systems. For programs that you plan to compile for 64-bit platforms, you should be careful not to assign pointers to 32-bit variables. Pointers are 64-bit on 64-bit platforms, and you will truncate the pointer value if you assign it to a 32-bit variable.
[/quote]

OK, I know I'm not a genius... but is that sentence right?

Here's a simple code to test that (sorry for the end ):



#include <iostream>

int main( )
{
int i [ 4 ] = { 1, 2 ,3 ,4 };
long l [ 4 ] = { 11, 22 ,33 ,44 };
long long ll [ 44 ] = { 111, 222 ,333 ,444 };
void* p = NULL;

p = &i;
std::cout << "p = &i" << std::endl;
std::cout << "p = " << p << std::endl;
std::cout << "&p = " << &p << std::endl;
for( int i = 0; i < 4; i++ )
{
std::cout << "*( p + " << i << " ) = " << *( static_cast< int* >( p ) + i ) << std::endl;
}
std::cout << std::endl;

p = &l;
std::cout << "p = &l" << std::endl;
std::cout << "p = " << p << std::endl;
std::cout << "&p = " << &p << std::endl;
for( int i = 0; i < 4; i++ )
{
std::cout << "*( p + " << i << " ) = " << *( static_cast< long* >( p ) + i ) << std::endl;
}
std::cout << std::endl;

p = &ll;
std::cout << "p = &ll" << std::endl;
std::cout << "p = " << p << std::endl;
std::cout << "&p = " << &p << std::endl;
for( int i = 0; i < 4; i++ )
{
std::cout << "*( p + " << i << " ) = " << *( static_cast< long long* >( p ) + i ) << std::endl;
}
std::cout << std::endl;

char ch = '\n';
std::cin >> ch;

return( 0 );
}


Here is the output:

[attachment=7623:asd.PNG]

Everything seems to works fine...

so.. Where is the truncation?

Share this post


Link to post
Share on other sites
Advertisement

int* p = new int();
int q = (int)p; //truncates on any case where sizeof(q) != sizeof(p)
size_t r = (size_t)p; //does not truncate.

Share this post


Link to post
Share on other sites


int* p = new int();
int q = (int)p; //truncates on any case where sizeof(q) != sizeof(p)
size_t r = (size_t)p; //does not truncate.


thx u

now everything is clear... I belived there was some strange behaviours with the pointer arithmetic... but the reality is more trivial than what I thought :D

Share this post


Link to post
Share on other sites

[quote name='Washu' timestamp='1331331219' post='4920776']

int* p = new int();
int q = (int)p; //truncates on any case where sizeof(q) != sizeof(p)
size_t r = (size_t)p; //does not truncate.


thx u

now everything is clear... I belived there was some strange behaviours with the pointer arithmetic... but the reality is more trivial than what I thought :D
[/quote]

Use C++ casts though, not C casts like I did. Explicit C++ casts tell not just the compiler, but the reader what you're looking for:

int* p = new int();
int q = reinterpret_cast<int>(p); //truncates on any case where sizeof(q) != sizeof(p)
size_t r = reinterpret_cast<size_t>(p); //does not truncate.

Share this post


Link to post
Share on other sites
I don't think a static_cast is enough to do the trick. You will need a reinterpret_cast.

Share this post


Link to post
Share on other sites

I don't think a static_cast is enough to do the trick. You will need a reinterpret_cast.

You are correct.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement