reinterpret-cast

reinterpret_cast, char*, and undefined behavior

岁酱吖の 提交于 2019-11-30 08:41:14
What are the cases where reinterpret_cast ing 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 undefined behavior: alignas(int) char data[sizeof(int)]; int *myInt = new (data) int; // OK *myInt = 34; // OK int i = *reinterpret_cast<int*>(data); // <== UB! have to use std::launder But at what point can we do a reinterpret_cast on a char array and have it NOT be undefined behavior? Here are a few simple examples: No new , just reinterpret_cast :

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

早过忘川 提交于 2019-11-30 03:01:02
Consider the following scenario: std::array<int, 8> a; auto p = reinterpret_cast<int(*)[8]>(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 cppreference seem to suggest that the reinterpret_cast is not valid As a programmer, I know that the memory location pointed by a.data() is an array of 8 int objects Is there any rule I am missing that makes this reinterpret_cast valid? An array object and its first element are not pointer-interconvertible * , so the result of the reinterpret_cast

Problem casting STL complex<double> to fftw_complex

空扰寡人 提交于 2019-11-30 00:31:50
The FFTW manual says that its fftw_complex type is bit compatible to std::complex<double> class in STL. But that doesn't work for me: #include <complex> #include <fftw3.h> int main() { std::complex<double> x(1,0); fftw_complex fx; fx = reinterpret_cast<fftw_complex>(x); } This gives me an error: error: invalid cast from type ‘std::complex<double>’ to type ‘double [2]’ What am I doing wrong? Re-write your code as follows: #include <complex> #include <fftw3.h> int main() { std::complex<double> x(1,0); fftw_complex fx; memcpy( &fx, &x, sizeof( fftw_complex ) ); } Every compiler I've used will

Does reinterpret_cast lead to undefined behavior?

丶灬走出姿态 提交于 2019-11-29 19:37:45
问题 I have a class template A which contains a container of pointers ( T* ): template <typename T> class A { public: // ... private: std::vector<T*> data; }; and a bunch of functions like: void f(const A<const T>&); void g(const A<const T>&); Is it OK to call these functions via a cast from A<const T> to A<T> ? A<double> a; ... auto& ac = reinterpret_cast<const A<const double>&>(a); f(ac); I'm pretty sure that this code has undefined behaviour. Is it dangerous to use such conversions in real life

Can Aliasing Problems be Avoided with const Variables

人盡茶涼 提交于 2019-11-29 14:56:38
问题 My company uses a messaging server which gets a message into a const char* and then casts it to the message type. I've become concerned about this after asking this question. I'm not aware of any bad behavior in the messaging server. Is it possible that const variables do not incur aliasing problems? For example say that foo is defined in MessageServer in one of these ways: As a parameter: void MessageServer(const char* foo) Or as const variable at the top of MessageServer : const char* foo =

Getting around the reinterpret cast limitation with constexpr

旧街凉风 提交于 2019-11-29 13:21:58
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 ((*(unsigned int*)&x << 9) >> 9); }; The above code would fail to be constexpr . In theory, I can't see how a reinterpret cast in this or similar cases can be any different from arithmetic operators, but the complier (and the standard) don't allow it. Is there any clever way of getting around this limitation? I can't see how a reinterpret cast in this or

Proper casting for fstream read and write member functions

痞子三分冷 提交于 2019-11-29 12:39:06
Although there was a lot of lines written on the topic of reinterpret_cast, and how bad it is, I'm still puzzled with best way to avoid it, especially when dealing with functions like read and write from fstream. So, here is my dilemma... Let's say we have an integer array that we want to fill with some data from a file. std::ifstream iFile( ... ); // presume that the type of this array is not a matter of choice int *a = new int[ 100 ]; We can read with a few different casts: iFile.read( (char *)a, sizeof( int ) * 100 ); iFile.read( reinterpret_cast< char * >( a ), sizeof( int ) * 100 ); iFile

Make interchangeable class types via pointer casting only, without having to allocate any new objects?

元气小坏坏 提交于 2019-11-29 05:10:00
UPDATE : I do appreciate "don't want that, want this instead" suggestions. They are useful, especially when provided in context of the motivating scenario . Still...regardless of goodness/badness, I've become curious to find a hard-and-fast "yes that can be done legally in C++11" vs "no it is not possible to do something like that" . I want to "alias" an object pointer as another type, for the sole purpose of adding some helper methods. The alias cannot add data members to the underlying class (in fact, the more I can prevent that from happening the better!) All aliases are equally applicable

Can reinterpret_cast (or any cast) convert xvalues to lvalues?

三世轮回 提交于 2019-11-29 04:19:47
Is the following code legal (by C++11 and/or C++14 standard(s))? #include <iostream> #include <utility> using namespace std; void foo(int &a) { cout << a << endl; } int main() { foo(reinterpret_cast<int &>(move(5))); } If yes, is it undefined behavior? If it's not undefined behavior, can I even mutate a inside foo without it becoming UB? It compiles on clang 3.5, not on gcc 4.9. GCC error: ➤ g++-4.9 -std=c++1y sample.cpp -o sample sample.cpp: In function 'int main()': sample.cpp:11:40: error: invalid cast of an rvalue expression of type 'std::remove_reference<int>::type {aka int}' to type 'int

Once again: strict aliasing rule and char*

浪尽此生 提交于 2019-11-29 01:18:12
问题 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. Correct me if I'm wrong. This is fine, gcc does not give warning and I'm trying to "read type T ( uint32_t ) via char* ": uint32_t num = 0x01020304; char* buff = reinterpret_cast< char* >( &num ); But this is "bad" (also gives a warning) and I'm trying