2

I see this type of code a lot in a project I work on:

reinterpret_cast<long>(somePointer)

and I don't understand the point of this. It is used for user defined classes usually, that is, somePointer is pointer to an instance of a user defined class.

Thanks for any help

user2381422
  • 5,645
  • 14
  • 42
  • 56
  • see this--http://stackoverflow.com/questions/310451/should-i-use-static-cast-or-reinterpret-cast-when-casting-a-void-to-whatever –  Jul 26 '13 at 12:12
  • 2
    *"I see this type of code a lot in a project I work on:"* -- somebody's project is broken and full of hacks, more likely than not. – John Dibling Jul 26 '13 at 12:33
  • That's bad. Long may not be big enough for a pointer. – Neil Kirk Jul 26 '13 at 12:48

6 Answers6

7

It's used to convert the address of the pointer to its numeric representation and store it as a long.

It's typically used when you need to store a pointer in a particular context where an actual pointer isn't supported. For example, some APIs allow you to pass a numeric key which they will give you back in any callback from that API. You can create an object and cast it to a number and give it to the API. Then in the callback you cast the number back to a pointer with pointer=reinterpret_cast<type*>(number) and then access your data.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • 1
    @user2381422 At the risk of being pedantic, the size of a pointer could be larger than the size of a long, in which case the value could be truncated, leading to key collisions. – Come Raczy Jul 26 '13 at 12:33
  • And it would just explode when read back since the pointer would be invalid if half of it was gone – paulm Jul 26 '13 at 12:37
  • 3
    @ComeRaczy: Very true, as a matter of fact, if you ever compile that code for windows 64bits, or power 64bit architectures, it's going to break as `sizeof(long)==4, sizeof(void*)==8`. This is not just pedantry, it has practical repercusions in *current* code. – David Rodríguez - dribeas Jul 26 '13 at 12:41
  • You need to validate on your platform but it is very likely to work: `$ cat test_pointer_size.c #include int main(int argc, char *argv[]) { printf("sizeof(long): %u sizeof(void*): %u\n", sizeof(long), sizeof(void*)); return 0; } $ gcc test_pointer_size.c $ ./a.out sizeof(long): 8 sizeof(void*): 8 ` – lano1106 May 07 '21 at 16:12
0

A pointer is actually nothing more than an integer value, it just that the compiler interprets it as a memory address.

This means that you can store it in an other integer type, if that other integer type is big enough to hold the address (for example, you can't store a 64-bit address in a 16-bit integer variable).

But since C++ considers pointers and normal integer values as distinct and different types, you can't just do a normal cast, you have to ask the compiler to reinterpret the pointer as a long, which is what reinterpret_cast does.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0
reinterpret_cast<long>(somePointer)

This piece of code casts the address holded by somePointer to a long, it's better to use intptr_t or uintptr_t for signed/unsigned integral types in C++11 in this. They are capable of holding a value converted from a void pointer and then be converted back to that type with a value that compares equal to the original pointer.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

It is, as others have said, converting the pointer to an integer value. This is somewhat "dangerous", as a pointer may not actually fit in a long. There is a type in cstdint that is called intptr_t, which is intended for this purpose.

The reinterpret_cast<new_type>(value) really is a "Take the 'bits' of value and convert it so that it can be used as new_type" - and the compiler will just do what you ask of it, whether it makes much sense or not - so it's up to YOU as a programmer to make sure you only do this in a way that actually does something useful - if you happen to cut of 32 bits from a 64-bit pointer, and then try to make it into a pointer again later on, then it's YOUR fault if the pointer "isn't any good" after. reinterpret_cast should really only be used as a last resort, there are often other methods to do the same thing.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

reinterpret_cast<x>(exp)

reinterprets the underlying bit pattern, returning a value of type x

It instructs the compiler to treat the sequence of bits of an exp as if it had the type x

With reinterpret_cast you can cast a pointer to a different type, and then reinterpret_cast it back to the original type to get the original value.

P0W
  • 46,614
  • 9
  • 72
  • 119
0

Like Sean said, it's useful for manipulating pointers as if they were numbers.

Here's an example from the MSDN website that uses it to implement a basic hash function (note that this uses an unsigned int rather than a long, but the principle is the same)

unsigned short Hash( void *p ) {
   unsigned int val = reinterpret_cast<unsigned int>( p );
   return ( unsigned short )( val ^ (val >> 16));
}

This takes an arbitrary pointer, and produces an (effectively) unique hash value for that pointer - by treating the pointer as an unsigned int, XORing the integral value with a bit-shifted version of itself, and then truncating the result into an unsigned short.

However, like everyone else has said, this is very dangerous and should be avoided.

oboewan42
  • 88
  • 6