reinterpret-cast

Does encapsulated char array used as object breaks strict aliasing rule

风格不统一 提交于 2020-01-04 03:03:21
问题 Do the following class break the strict aliasing rule: template<typename T> class store { char m_data[sizeof(T)]; bool m_init; public: store() : m_init(false) {} store(const T &t) : init(true) { new(m_data) T(t); } ~store() { if(m_init) { get()->~T(); } } store &operator=(const store &s) { if(m_init) { get()->~T(); } if(s.m_init) { new(m_data) T(*s.get()); } m_init = s.m_init; } T *get() { if (m_init) { return reinterpret_cast<T *>(m_data); } else { return NULL; } } } My reading of a standard

reinterpret_cast to function pointer

丶灬走出姿态 提交于 2020-01-03 14:16:33
问题 There is the code that I've written for experiments with reinterpret_cast<T> #include <iostream> #include <cstdlib> using std::cout; using std::endl; int foo() { cout << "foo" << endl; return 0; } void (*bar)(); int main() { bar = reinterpret_cast<void (*)()>(foo); //Convertion a function type to a pointer to function type bar(); //displays foo. Is it UB? } First of all why such reinterpret_cast convertion permitted? I thought such conversion is ill-formed. 回答1: The standard (C++11 §5.2.10/6)

Does casting a char array to another type violate strict-aliasing rules?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 08:27:08
问题 Consider these two functions: int f1() { alignas(int) char buf[sizeof(int)] = {}; return *reinterpret_cast<int*>(buf); } int f2() { alignas(int) char buf[sizeof(int)] = {}; char* ptr = buf; return *reinterpret_cast<int*>(ptr); } GCC warns that the first violates strict-aliasing rules. But the second is OK. Clang accepts both without complaint. Is the warning legitimate? 回答1: The warning is legitimate. f2 is not OK (it is undefined behaviour), it just doesn't provoke the warning. I suspect the

reinterpret_cast casts away qualifiers

血红的双手。 提交于 2019-12-31 17:48:14
问题 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<Dialog *>(data); dialog->setValue((data_sent * 100) / data_total); } the reinterpret_cast seems not allowed and say reinterpret_cast from 'const void *) to Dialog * casts away qualifiers Any idea 回答1: As Nick Strupat stated in comment, reinterpret_cast can't cast away cv-qualifiers So you can use

Why doesn't this reinterpret_cast compile?

邮差的信 提交于 2019-12-28 04:59:07
问题 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<double>(x); When I try to compile the program, it gives me an error saying invalid cast from type 'float' to type 'double What's going on? I thought reinterpret_cast was the rogue cast that you could use to convert apples to submarines, why won't this simple cast compile? 回答1: By assigning y to the value returned by the cast you're not really

What does reinterpret_cast<char *>(&st) and (-1)*static_cast<int> mean?

扶醉桌前 提交于 2019-12-26 03:13:42
问题 The code here is being used for creating a Student Report card project. While trying to understand we can not figure out the use of and functions of the below code: File.read(reinterpret_cast<char *> (&st), sizeof(student)); int pos=(-1)*static_cast<int>(sizeof(st)); File.read(reinterpret_cast<char *> (&st), sizeof(student)); if(st.retrollno()==n) { st.showdata(); cout<<"\n\nPlease Enter The New Details of student"<<endl; st.getdata(); int pos=(-1)*static_cast<int>(sizeof(st)); File.seekp(pos

C++ unions vs. reinterpret_cast

醉酒当歌 提交于 2019-12-25 03:11:42
问题 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 integer value of 0xffff and literally interpret it as a series of bits in IEEE 754 floating point. (Binary convert shows visually how this is done.) #include <iostream> using namespace std; union unionType { int myInt; float myFloat; }; int main() { int

Is there a good way to convert from unsigned char* to char*?

落花浮王杯 提交于 2019-12-24 06:59:11
问题 I've been reading a lot those days about reinterpret_cast<> and how on should use it (and avoid it on most cases). While I understand that using reinterpret_cast<> to cast from, say unsigned char* to char* is implementation defined (and thus non-portable ) it seems to be no other way for efficiently convert one to the other. Lets say I use a library that deals with unsigned char* to process some computations. Internaly, I already use char* to store my data (And I can't change it because it

Calling derived class's methods from pointer to base class via reinterpret_casting the method pointer. Is this UB?

不羁的心 提交于 2019-12-24 06:34:52
问题 With a pointer to an object of a derived type assigned to a pointer of its base class, I've found that you can reinterpet_cast a method from the derived class to a pointer of the base class, even if the base class doesn't have any such function (virtual, hidden, or otherwise). And it can be dereferenced and called from there and it "just works". But I'd like to make sure it's not UB. Is this UB? Is it portable? Compilable Example: #include <cstdio> struct A { /* no foo method */ }; struct B :

A “hack” to get float template parameter working compiles but segfaulted on both g++ and clang

若如初见. 提交于 2019-12-22 08:14:54
问题 I know why I can't use float as template parameter and how to set a static const float member of template class thanks to a numerator/denominator couple. But I was trying another "hack" based on reinterpret_cast to "emule" float template parameters from its IEEE754 hexadecimal writing. Here is the small piece of code : #include <iostream> #include <cstdint> template <uint32_t T> struct MyStruct { static const float value; }; template <uint32_t T> const float MyStruct<T>::value = *reinterpret