"Ivan Novick" <iv***********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
On Jan 30, 12:29 am, "Allen" <che...@naritech.cnwrote:
>I do like this:
int address =
reinterpret_cast<int>(reinterpret_cast<int*>(cons t_cast<char*>((ptr))))
;
But the compiler gives a warning, warning C4311: "reinterpret_cast":
from int* to int pointer cut off.- Hide quoted text -
Yes, like the other poster said, the sizeof int on your system may not
be big enough to hold a pointer value. reinterpret_cast is definitely
the right way to cast it. Instead of putting the result into an int,
you can use intptr_t which is guaranteed to be a typedef to an integer
type big enough to hold a pointer on your system. intptr_t is not
part of the C++ standard, but I believe it is part of the C standard,
and is available in g++, and may even be in the queue to be added to
the C++ standard in the future.
Ivan
http://www.0x4849.net
-----code-----------------------------------------------
#include <cstddef>
template<intstruct intselector;
template<struct intselector<1>
{ typedef unsigned char type; };
template<struct intselector<2>
{ typedef unsigned short type; };
template<struct intselector<3>
{ typedef unsigned int type; };
template<struct intselector<4>
{ typedef unsigned long type; };
// and if your platform supports it (non-standard):
//template<struct intselector<5>
// { typedef unsigned long long type; };
template<size_t Sstruct inttype
{
typedef typename intselector
<
S <= sizeof(unsigned char)
? 1
: S <= sizeof(unsigned short)
? 2
: S <= sizeof(unsigned int)
? 3
: S <= sizeof(unsigned long)
? 4
: 5
>::type type;
};
typedef inttype<sizeof(void*)>::type intptr_t;
-----end code-------------------------------------------
Now you have an intptr_t on every platform ;)
- Sylvester