473,756 Members | 3,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compiler warning when casting from a pointer to an integer

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
5 11227

"dis" <di*@quietblue. co.uk> wrote in message
news:8d******** *************** ***@posting.goo gle.com...
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
On 16 Nov 2003 16:59:21 -0800, di*@quietblue.c o.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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 19 '05 #3
"dis" <di*@quietblue. co.uk> wrote in message
news:8d******** *************** ***@posting.goo gle.com
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.publi c.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
dis
"John Carson" <do***********@ datafast.net.au > wrote in message news:<3f******@ usenet.per.para dox.net.au>...
"dis" <di*@quietblue. co.uk> wrote in message
news:8d******** *************** ***@posting.goo gle.com
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.publi c.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
"dis" <di*@quietblue. co.uk> wrote in message
news:8d******** *************** ***@posting.goo gle.com

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.microsof t.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
1739
by: Gregg Woodcock | last post by:
My compiler (m68k-gcc) does not allow this (simplified example; not my real code) and I don't see why not: { char *arrayCharPtr1 = {"a", "ab", NULL}; char *arrayCharPtr2 = {"A", "AB", NULL}; char *arrayCharPtrVar = ((someBoolean) ? arrayCharPtr1 : arrayCharPtr2); // <-error here! StrCopy(someString, arrayCharPtr); }
6
1338
by: Adam Warner | last post by:
Hi all, When adding variable length arrays to my program I created the elements as type struct o *. This is because /most/ of the time the VLAs contains pointers to struct o objects (these objects are as stringently aligned as any C type in the implementation). Occasionally a single element is actually a pointer to another VLA of pointers to struct o objects (and occasionally I'm storing an integer index (of type ptrdiff_t) as an...
16
5858
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program compiles fine. What is the warning shown ?
29
2525
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
30
1740
by: Philippe Bertrand | last post by:
Is this a bug in the C# compiler or CLR runtime? enum MyEnum { ZERO = 0, ONE = 1, TWO = 2 } class Foo { public Foo(string,object) { ... } public Foo(string,MyEnum) { ... } } Foo f = new Foo("", 0); // Uses Foo(string,MyEnum) constructor instead of Foo(string,object)
8
2010
by: Charles Sullivan | last post by:
I have a program written in C under Linux (gcc) which a user has ported to run under AT&T SysV R4. He sent me a copy of his makelog which displays a large number of compiler warnings similar to this: warning: semantics of ">>" change in ANSI C; use explicit cast The statement to which this applies is: xuc = ((uc & 0xF0 ) >4);
45
2276
by: alertjean | last post by:
Or may be I am stubborn or dumb ... of not putting in a * in the typecast. This is code I am worrying about long long b=1; int *address ; address=(int)&b; printf ("%x %x \n",*(address+1)*address); when I compile the code I get this warning from gcc (although it
28
2282
by: Dave Stafford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x) ({ if(x && *x) { free(*x); *x=NULL; } }) void main() { char *x = (char *) malloc(10); int *y = (int *) malloc(10);
2
1908
by: Chris Peters | last post by:
Hi, I'm using some Fortran 77 code together with C, and the compiler is giving me some strange warnings. Can anyone help explain what's going on? The code runs just fine. 1) Fortran code
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9273
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9841
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5303
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.