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
-2
votes
1 answer

Serialize arbitrary trivially cpyable non array "stuff" into buffer (reinterpret_cast) goes wrong

The title is self-explanatory: I want to serialize an arbitrary large amount of trivially copyable non array "stuff" into a buffer (for academic reasons). The basic idea is to reinterpret_cast the address of what I want to serialize as an unsigned…
R. Absil
  • 173
  • 6
-2
votes
1 answer

can i cast a template instance with arg uint to same template instance with arg int - is it compliant - to which standard?

Plz check following c++ code: (nothing special, should be compliant to c++ 2nd edition from 1991) class C { // also defines all the methods called in template method below with the obvious name and args. public:…
seebee
  • 1
  • 3
-2
votes
1 answer

Incorrect char array length when using reinterpret_cast to store data with struct object

I'm trying to read .txt data (formatted with spaces) into a struct object. To simplify the question, say the .txt contains one record of a student's first name, last name, campus code, and id. Here is the struct: struct Student { char f_name[10]; //…
Madmint
  • 5,976
  • 1
  • 11
  • 17
-2
votes
1 answer

C++ reinterpret_cast core in Linux, but run succsee in MAC

In some case,I want malloc a block memory, example: first part of memory will contains strings, second part memory will contains long type data. so, i try the demo code below. the code below can run success in MAC, but core dump in Linux. valgrind…
liewen
  • 3
  • 2
-2
votes
2 answers

Is c_str() or reinterpret_cast better for working with binary files?

I need to work with binary files in a program and I've seen reinterpret_cast used, as well as c_str(). Here is a code snippet using c_str(): fstream aFile; string sample = "hello this is a line of code"; aFile.open("newFile.bin", ios::out |…
V B
  • 45
  • 1
  • 2
-2
votes
2 answers

Why reinterpret_cast fails while memcpy works?

I'm writing some socket code and based on some params I'm using either IPv4 or IPv6. For that I have a code like this: struct sockaddr final_addr; ... struct sockaddr_in6 addr6; ... memcpy(&final_addr, &addr6, size); ... bind(fd, &final_addr,…
freakish
  • 54,167
  • 9
  • 132
  • 169
-2
votes
2 answers

reinterpret_cast or C Style type casting

Should be prefer reinterpret_cast over C style casting. Please explain. Which one should be preferred if one has to choose between reinterpret_cast and c style casting
-2
votes
3 answers

Converting hexadecimal number in uint64_t format to double in C++

Say, I have a profiler method that returns a uint64_t value in hexadecimal format. I want to display this value in decimal format. Is there a simple way to do this without using standard c++ libraries? the expected output is in the range of…
Naveen
  • 458
  • 1
  • 10
  • 29
-2
votes
2 answers

Construct parent from forward declared sibling without a reinterpret_cast

I'm trying to call a parent constructor, with a given pointer to sibling object: class Base{ public: Base(const Base&) =default; }; #include "daughter.h" // <-- problem! I'll come to this in a second. class Son: public Base{ public: …
OJFord
  • 10,522
  • 8
  • 64
  • 98
-2
votes
2 answers

Returning int, int* and int& from a function

I just wanted to clarify something, imagine we have the function signature: 1) int* X(){} 2) int Y(){} 3) int& Z(){} I am trying to work out the exhaustive possibilities of types of values I can return for the above. The below show possible…
user997112
  • 29,025
  • 43
  • 182
  • 361
-2
votes
2 answers

std::copy_n and reinterpret_cast

uint data1; ushort data2; ushort data3; uchar data4[8]; std::uint8_t buff[16]; std::uint8_t* out = buff; out = std::copy_n(reinterpret_cast(&quid.data1), 4, out); out =…
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
-3
votes
1 answer

Accessing private members with reinterpret_cast

I know how to access std::string in class S, but how can i get int i in this class? What should i do? I understand i should shift the pointer, but in what way? #include #include class S { std::string s = "abcd"; int i =…
-3
votes
1 answer

Placement of an item in memory

Help me please. I allocate memory as follows (T is template type) T * ptr = reinterpret_cast(operator new (sizeof(T)); And after that I want to put an element into this memory; Am I write if I do it in this way? new (p) T(elem); (elem has…
GThompson
  • 25
  • 4
-3
votes
1 answer

Why g++ giver: "error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"

I'm doing an UDP connection, between a microcontroller and a computer. The framework I'm using is c++ based and has a function to send an UDP packet with the following prototype: bool UdpConnection::send(const char *data, int length) int length is…
Oshio
  • 133
  • 4
  • 17
-3
votes
3 answers

How do I reinterpret_cast between any two types?

I would like to re-declare the type of a given variable, but unfortunately reinterpret_cast<> does not help here. This line: reinterpret_cast>(std::string("Hello")); results in the following compiler error: invalid cast from…
Jonn Dove
  • 477
  • 2
  • 11
1 2 3
38
39