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
25
votes
3 answers

Is it a strict aliasing violation to alias a struct as its first member?

Sample code: struct S { int x; }; int func() { S s{2}; return (int &)s; // Equivalent to *reinterpret_cast(&s) } I believe this is common and considered acceptable. The standard does guarantee that there is no initial padding…
M.M
  • 138,810
  • 21
  • 208
  • 365
24
votes
3 answers

Examples using reinterpret_cast that do not trigger UB

Reading https://en.cppreference.com/w/cpp/language/reinterpret_cast I wonder what are use-cases of reinterpret_cast that are not UB and are used in practice? The above description contains many cases where it is legal to convert a pointer to some…
wimalopaan
  • 4,838
  • 1
  • 21
  • 39
24
votes
7 answers

Why can't I reinterpret_cast uint to int?

Here's what I want to do: const int64_t randomIntNumber = reinterpret_cast (randomUintNumber); Where randomUintNumber is of type uint64_t. The error is (MSVC 2010): error C2440: 'reinterpret_cast' : cannot convert from 'const uint64_t' …
Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
22
votes
3 answers

C++ unions vs. reinterpret_cast

It appears from other StackOverflow questions and reading §9.5.1 of the ISO/IEC draft C++ standard standard that the use of unions to do a literal reinterpret_cast of data is undefined behavior. Consider the code below. The goal is to take the…
kgraney
  • 1,975
  • 16
  • 20
21
votes
5 answers

Is reinterpret_cast mostly useless?

I've read various previous questions about the use of reinterpret_cast, and I've also read the relevant wording in the C++ standard. Essentially, what it comes down to is that the result of a pointer-to-pointer reinterpret_cast operation can't…
Channel72
  • 24,139
  • 32
  • 108
  • 180
20
votes
1 answer

reinterpret_cast, char*, and undefined behavior

What are the cases where reinterpret_casting a char* (or char[N]) is undefined behavior, and when is it defined behavior? What is the rule of thumb I should be using to answer this question? As we learned from this question, the following is…
Barry
  • 286,269
  • 29
  • 621
  • 977
20
votes
4 answers

Is %p specifier only for valid pointers?

Suppose on my platform sizeof(int)==sizeof(void*) and I have this code: printf( "%p", rand() ); Will this be undefined behavior because of passing a value that is not a valid pointer in place of %p?
sharptooth
  • 167,383
  • 100
  • 513
  • 979
18
votes
2 answers

Why is reinterpret_cast not constexpr?

Consider the following snippet: static constexpr uint8_t a = 0; static constexpr const int8_t *b = reinterpret_cast(&a); This fails to compile with error: a reinterpret_cast is not a constant expression, because the C++ standard…
Conor Taylor
  • 2,998
  • 7
  • 37
  • 69
18
votes
3 answers

Is it safe to reinterpret_cast an enum class variable to a reference of the underlying type?

I've seen reinterpret_cast used to apply incrementation to enum classes, and I'd like to know if this usage is acceptable in standard C++. enum class Foo : int8_t { Bar1, Bar2, Bar3, Bar4, First = Bar1, Last = Bar4 }; for…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
17
votes
2 answers

Why does this C-style cast not consider static_cast followed by const_cast?

Consider: float const& f = 5.9e-44f; int const i = (int&) f; Per expr.cast/4 this should be considered as, in order: a const_­cast, a static_­cast, a static_­cast followed by a const_­cast, a reinterpret_­cast, or a reinterpret_­cast followed by…
ecatmur
  • 152,476
  • 27
  • 293
  • 366
17
votes
3 answers

Way to get unsigned char into a std::string without reinterpret_cast?

I have an unsigned char array that I need in a std::string, but my current way uses reinterpret_cast which I would like to avoid. Is there a cleaner way to do this? unsigned char my_txt[] = { 0x52, 0x5f, 0x73, 0x68, 0x7e, 0x29, 0x33, 0x74, 0x74,…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
17
votes
2 answers

Once again: strict aliasing rule and char*

The more I read, the more confused I get. The last question from the related ones is closest to my question, but I got confused with all words about object lifetime and especially - is it OK to only read or not. To get straight to the point.…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
17
votes
3 answers

Is casting std::pair const& to std::pair const& safe?

Is it safe (in theory or in practice) to reinterpret_cast a std::pair const & into a std::pair const &, assuming that the programmer hasn't intentionally done something weird like specializing std::pair?
user541686
  • 205,094
  • 128
  • 528
  • 886
16
votes
2 answers

Is there a (semantic) difference between the return value of placement new and the casted value of its operand?

Is there a (semantic) difference between the return value of placement new and the casted value of its operand? struct Foo { ... }; char buffer[...]; Foo *a = new(buffer) Foo; Foo *b = reinterpret_cast(buffer); Does a and b differ in some…
geza
  • 28,403
  • 6
  • 61
  • 135
15
votes
1 answer

Does accessing the first field of a struct via a C cast violate strict aliasing?

Does this code violate strict aliasing? struct {int x;} a; *(int*)&a = 3 More abstractly, is it legal to cast between different types as long as the primitive read/write operations are type correct?
Geoffrey Irving
  • 6,483
  • 4
  • 32
  • 40
1
2
3
38 39