Compiler warning when casting from a pointer to an integer

D

dis

I've been going through my code and clearing away some of the compiler
warnings that i'm generating and i've come across areas where i cast
pointers to integer values. The Visual Studio compiler generates this
warning ...
"warning C4311: 'type cast' : pointer truncation from 'void *' to
'DWORD'"

The warning is generated as the pointer is apparently 64 bits long
(even though a quick sizeof() reports it to only be 4 bytes in length)
and is being truncated, i understand that much. Looking around the
MSDN revealed that the pointer is considered a 32 bit offset from a 32
bit base pointer, and i assume thats where the full 64 bits has come
from.

What i don't understand is why sizeof() reports the pointer as 32 bits
while it is considered to be 64 bits in lenght when casting. Also
i've got no idea how to change the code so that the warning will go
away, anyone?
 
T

Tanguy Fautré

dis said:
I've been going through my code and clearing away some of the compiler
warnings that i'm generating and i've come across areas where i cast
pointers to integer values. The Visual Studio compiler generates this
warning ...
"warning C4311: 'type cast' : pointer truncation from 'void *' to
'DWORD'"

The warning is generated as the pointer is apparently 64 bits long
(even though a quick sizeof() reports it to only be 4 bytes in length)
and is being truncated, i understand that much. Looking around the
MSDN revealed that the pointer is considered a 32 bit offset from a 32
bit base pointer, and i assume thats where the full 64 bits has come
from.

What i don't understand is why sizeof() reports the pointer as 32 bits
while it is considered to be 64 bits in lenght when casting. Also
i've got no idea how to change the code so that the warning will go
away, anyone?

- C++ related answer:
Visual Studio warns you against a dangerous casting. How can you be sure
that a pointer will always be 32 bits in size?
I suspect some other compilers on other platforms will also give similar
warnings.

- VC++ related answer
Now, part of this issue (which isn't an issue IMHO) is that Visual C++ 7.0
and higher have by default an option that checks such portability issues
(for the moment, only for 64 bits platform, which explains your warning).
see <Project property pages> / General Properties / C/C++ / General / Detect
64 bits Portability Issues

Regards,

Tanguy
 
J

Jack Klein

I've been going through my code and clearing away some of the compiler
warnings that i'm generating and i've come across areas where i cast
pointers to integer values. The Visual Studio compiler generates this
warning ...
"warning C4311: 'type cast' : pointer truncation from 'void *' to
'DWORD'"

The warning is generated as the pointer is apparently 64 bits long
(even though a quick sizeof() reports it to only be 4 bytes in length)
and is being truncated, i understand that much. Looking around the
MSDN revealed that the pointer is considered a 32 bit offset from a 32
bit base pointer, and i assume thats where the full 64 bits has come
from.

What i don't understand is why sizeof() reports the pointer as 32 bits
while it is considered to be 64 bits in lenght when casting. Also
i've got no idea how to change the code so that the warning will go
away, anyone?

How can you expect anyone to make suggestions about fixing your code
when you haven't include it? Post a small sample, just copy and past
it into your newsreader, no attachments, and make sure it includes the
definitions of the types involved.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

John Carson

dis said:
I've been going through my code and clearing away some of the compiler
warnings that i'm generating and i've come across areas where i cast
pointers to integer values. The Visual Studio compiler generates this
warning ...
"warning C4311: 'type cast' : pointer truncation from 'void *' to
'DWORD'"

The warning is generated as the pointer is apparently 64 bits long
(even though a quick sizeof() reports it to only be 4 bytes in length)
and is being truncated, i understand that much. Looking around the
MSDN revealed that the pointer is considered a 32 bit offset from a 32
bit base pointer, and i assume thats where the full 64 bits has come
from.

What i don't understand is why sizeof() reports the pointer as 32 bits
while it is considered to be 64 bits in lenght when casting. Also
i've got no idea how to change the code so that the warning will go
away, anyone?

This is a Microsoft specific issue. In future, I suggest you post on

microsoft.public.vc.language

....but anyway.

A pointer on the Win32 platform is 32 bits. sizeof is reporting correctly.
However, in anticipation of the possibility that you may port your code to a
64 bit platform, the compiler warns you of any code that would give
problems. The pointer is not actually being truncated; the error message is
wrong in that respect. Rather, it would be truncated if you compiled for a
64 bit platform.

You can change a compiler option that turns off all such warnings. Some
warnings are completely bogus (even if you do port to 64 bits) and this is
the only way to turn them off.

To turn off all correct warnings and to make your code "64 bit safe", when
you make assignments between a pointer and an integer, you need to use a
"pointer-sized" integer. In Microsoft's case, this means an integer type
that is 32 bits on a 32 bit platform and 64 bits on a 64 bit platform. See

http://msdn.microsoft.com/library/d...64/win64/getting_ready_for_64_bit_windows.asp
 
D

dis

John Carson said:
This is a Microsoft specific issue. In future, I suggest you post on

microsoft.public.vc.language

...but anyway.

A pointer on the Win32 platform is 32 bits. sizeof is reporting correctly.
However, in anticipation of the possibility that you may port your code to a
64 bit platform, the compiler warns you of any code that would give
problems. The pointer is not actually being truncated; the error message is
wrong in that respect. Rather, it would be truncated if you compiled for a
64 bit platform.

You can change a compiler option that turns off all such warnings. Some
warnings are completely bogus (even if you do port to 64 bits) and this is
the only way to turn them off.

To turn off all correct warnings and to make your code "64 bit safe", when
you make assignments between a pointer and an integer, you need to use a
"pointer-sized" integer. In Microsoft's case, this means an integer type
that is 32 bits on a 32 bit platform and 64 bits on a 64 bit platform. See

http://msdn.microsoft.com/library/d...64/win64/getting_ready_for_64_bit_windows.asp

Cheers guys, i *thought* i'd disabled the 64bit compiler setting,
looks like i forgot to remove it in a couple of projects under debug
mode. Its all sorted now though, thanks.

I would have posted on a MS/VisualStudio group but i'm working through
google and can't get access. Guess i could have posted via microsoft
but in my experience their boards are pretty useless.
 
J

John Carson

dis said:
Cheers guys, i *thought* i'd disabled the 64bit compiler setting,
looks like i forgot to remove it in a couple of projects under debug
mode. Its all sorted now though, thanks.

I would have posted on a MS/VisualStudio group but i'm working through
google and can't get access. Guess i could have posted via microsoft
but in my experience their boards are pretty useless.

You can get Microsoft groups via Google, e.g.,

http://groups.google.com/groups?q=microsoft.public.vc.language&hl=en

Further, Microsoft's newsgroups are available on their news server

msnews.microsoft.com

to which anyone can connect for free using Outlook Express or similar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top