By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,373 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

Compiler warning when casting from a pointer to an integer

P: n/a
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?
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"dis" <di*@quietblue.co.uk> wrote in message
news:8d**************************@posting.google.c om...
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
Jul 19 '05 #2

P: n/a
On 16 Nov 2003 16:59:21 -0800, di*@quietblue.co.uk (dis) wrote in
comp.lang.c++:
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
Jul 19 '05 #3

P: n/a
"dis" <di*@quietblue.co.uk> wrote in message
news:8d**************************@posting.google.c om
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/de...it_windows.asp
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 19 '05 #4

P: n/a
dis
"John Carson" <do***********@datafast.net.au> wrote in message news:<3f******@usenet.per.paradox.net.au>...
"dis" <di*@quietblue.co.uk> wrote in message
news:8d**************************@posting.google.c om
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/de...it_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.
Jul 22 '05 #5

P: n/a
"dis" <di*@quietblue.co.uk> wrote in message
news:8d**************************@posting.google.c om

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=mi...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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.