type cast and pointer

R

ra

Hi ng,
I have a pointer p:

typep *p;

I try to cast this pointer with a DWORD

DWORD d = (DWORD) p;

My compiler (V.S. dot Net 2005) return a warning....

I need a DWORD or INT or FLOAT .....

How are the best way to cast a pointer?


Thansk!
 
I

Ivan Vecerina

ra said:
Hi ng,
I have a pointer p:

typep *p;

I try to cast this pointer with a DWORD

DWORD d = (DWORD) p;

My compiler (V.S. dot Net 2005) return a warning....

DWORD is a Windows-specific typedef, your question does not belong in this
forum about standard C++.
And you should provide the exact text of the warning you are confronted
with.

The point of the warning is probably that, for a pointer-to-integer cast to
be reversible (i.e. can be casted back to the same pointer value), you need
to make sure that you use an integer type that has sufficient accuracy.
Actually, it is not guaranteed that such an integer type exists.
But on your platform, someting like INT_PTR might be a typedef to the
integral type you are looking for.
I need a DWORD or INT or FLOAT .....

How are the best way to cast a pointer?

You may also want to read about C++-style type casts: reinterpret_cast,
static_cast, etc.


Ivan
 
J

John Harrison

ra said:
Hi ng,
I have a pointer p:

typep *p;

I try to cast this pointer with a DWORD

DWORD d = (DWORD) p;

Well the obvious question is, why? Newbies often seem to have a fatal
attraction towards doing obscure and dangerous things in C++.
My compiler (V.S. dot Net 2005) return a warning....

Not surprising when you do dangerous things.
I need a DWORD or INT or FLOAT .....

Need for what? This is hard to understand because you haven't provided
the context.
How are the best way to cast a pointer?

I don't think you need to cast a pointer. I think you need to explain
what you are trying to do and then I can (hopefully) explain how to do
it without you having to cast any pointers (or at least how to cast them
without warning messages).

john
 
P

persenaama

You can do this:

DWORD d = reinterpret_cast<DWORD&>(p);

But two words of warning:

- this invokes undefined behaviour, it works for Visual C++ .net 2005
and 2003 and the behaviour IS defined on that compiler and platform
assuming you are targeting 32-bit IA32. Regarding C++, the behaviour,
however, is not defined.

You might be better off doing two things:

First, use type other than DWORD so that your code keeps working on
64-bit Windows binaries. I approach similiar problem differently but
not going into details as that is off-topic. But just think of some
other type than DWORD (which works fine as of now on tools and platform
you currently targeting-- assuming 32-bit Windows).

Second, you might want to consider doing pointer aritchmetic instead of
casting the bits to represent integer value instead of memory address.

Something akin to this:

typep* p; // initialized somewhere..

char* base = 0;
ptrdiff_t ip = reinterpret_cast<char*>(p) - base;

In this case, ip would pretty much be the linear address stored into
integer-- it's better to use this than reinterpret_cast the bits and
assume it works, because it doesn't, I can give example of a fairly
common compiler where the former approach will produce catastrophic
failure if you want to dwell that path.

Thank you for your time and good luck!
 
R

Ron Natalie

ra said:
Hi ng,
I have a pointer p:

typep *p;

I try to cast this pointer with a DWORD

DWORD d = (DWORD) p;

My compiler (V.S. dot Net 2005) return a warning....

I need a DWORD or INT or FLOAT .....

DWORD is not necessarily big enough to hold a pointer (it
isn't on WIN64). You need a DWORD_PTR, which despite the
stupid name isn't a pointer to a DWORD, but another integral
type bigenough to hold either a PTR (casted) or a DWORD.
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top