Questions tagged [reinterpret-cast]

A C++ operator that simply allows the conversion between types by reinterpreting the underlying bit pattern. In general use, this amounts a pointer to be converted into any other pointer type and it can also allow an integral type to be converted into any pointer type and vice versa.

A reinterpret_cast directs the compiler to "view" or treat the memory as if it were the new type (being cast to).

From cppreference.com:

Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.

There are limitations on what reinterpret_cast can do whilst remaining valid, in particular type aliasing can become a problem.

581 questions
603
votes
11 answers

When to use reinterpret_cast?

I am little confused with the applicability of reinterpret_cast vs static_cast. From what I have read the general rules are to use static cast when the types can be interpreted at compile time hence the word static. This is the cast the C++ compiler…
HeretoLearn
  • 7,124
  • 6
  • 24
  • 22
257
votes
9 answers

Should I use static_cast or reinterpret_cast when casting a void* to whatever

Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other?
Andy
  • 3,004
  • 2
  • 22
  • 12
83
votes
11 answers

Why doesn't this reinterpret_cast compile?

I understand that reinterpret_cast is dangerous, I'm just doing this to test it. I have the following code: int x = 0; double y = reinterpret_cast(x); When I try to compile the program, it gives me an error saying invalid cast from type…
Vlad the Impala
  • 15,572
  • 16
  • 81
  • 124
69
votes
3 answers

Proper way of casting pointer types

Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast? I used…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
64
votes
3 answers

Why can't I static_cast between char * and unsigned char *?

Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule?
Nick
  • 2,821
  • 5
  • 30
  • 35
49
votes
7 answers

reinterpret_cast cast cost

My understanding is that C++ reinterpret_cast and C pointer cast is a just a compile-time functionality and that it has no performance cost at all. Is this true?
fulmicoton
  • 15,502
  • 9
  • 54
  • 74
46
votes
2 answers

reinterpret_cast casts away qualifiers

I add an issue on reinterpreting a variable and I don't know why.. int ProgressBar(const uint64_t data_sent, const uint64_t data_total, void const *const data) { Dialog *dialog = reinterpret_cast (data); dialog->setValue((data_sent…
Seb
  • 2,929
  • 4
  • 30
  • 73
45
votes
3 answers

Getting around the reinterpret cast limitation with constexpr

In c++11, a constexpr expression cannot contain reinterpret casts. So for instance, if one wanted to manipulate the bits in a floating point number, say to find the mantissa of the number: constexpr unsigned int mantissa(float x) { return…
nbubis
  • 2,304
  • 5
  • 31
  • 46
44
votes
7 answers

Why do we have reinterpret_cast in C++ when two chained static_cast can do its job?

Say I want to cast A* to char* and vice-versa, we have two choices (I mean, many of us think we've two choices, because both seems to work! Hence the confusion!): struct A { int age; char name[128]; }; A a; char *buffer =…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
41
votes
1 answer

How does qobject_cast work?

I just found the following code in Qt and I'm a bit confused what's happening here. Especially as to what reinterpret_cast(0) does? template inline T qobject_cast(const QObject *object) { // this will cause a compilation error if T…
ronag
  • 49,529
  • 25
  • 126
  • 221
38
votes
5 answers

Is the std::array bit compatible with the old C array?

Is the underlying bit representation for an std::array v and a T u[N] the same? In other words, is it safe to copy N*sizeof(T) bytes from one to the other? (Either through reinterpret_cast or memcpy.) Edit: For clarification, the emphasis is on…
shinjin
  • 2,858
  • 5
  • 29
  • 44
37
votes
3 answers

casting via void* instead of using reinterpret_cast

I'm reading a book and I found that reinterpret_cast should not be used directly, but rather casting to void* in combination with static_cast: T1 * p1=... void *pv=p1; T2 * p2= static_cast(pv); Instead of: T1 * p1=... T2 * p2=…
sinek
  • 2,458
  • 3
  • 33
  • 55
35
votes
2 answers

Why can reinterpret_cast not convert an int to int?

My compiler is the latest VC++ 2013 RC. void f() { int n1 = 0; int n2 = reinterpret_cast(n1); // error C2440 } error C2440: 'reinterpret_cast' : cannot convert from 'int' to 'int' Why can reinterpret_cast not be used in such an…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
28
votes
2 answers

Is it undefined behavior to `reinterpret_cast` a `T*` to `T(*)[N]`?

Consider the following scenario: std::array a; auto p = reinterpret_cast(a.data()); (*p)[0] = 42; Is this undefined behavior? I think it is. a.data() returns a int*, which is not the same as int(*)[8] The type aliasing rules on…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
27
votes
6 answers

Is C++ considered weakly typed? Why?

I've always considered C++ to be one of the most strongly typed languages out there. So I was quite shocked to see Table 3 of this paper state that C++ is weakly typed. Apparently, C and C++ are considered weakly typed since, due to type-casting,…
user541686
  • 205,094
  • 128
  • 528
  • 886
1
2 3
38 39