reinterpret-cast

access violation casting to void* and back

痞子三分冷 提交于 2019-12-12 05:39:43
问题 I get an access violation reading location when I try the following. What am I doing wrong? uint64_t hInt = 2901924954136; void* hPoint = reinterpret_cast<void*>(hInt); uint64_t hIntBack = *static_cast<uint64_t*>(hPoint); //get access violation here 回答1: I am guessing you meant to store the address of hInt in hPoint , not the value of hInt . uint64_t hInt = 2901924954136; void* hPoint = reinterpret_cast<void*>(&hInt); // ^ addressof operator 回答2: What you do is the following: cast an integer

Is reinterpret_cast and c-style cast compatible (by C++ standard)?

末鹿安然 提交于 2019-12-12 05:15:08
问题 The C++ standards mentions that reinterpret_cast is implementation defined, and doesn't give any guarantees except that casting back (using reinterpret_cast ) to original type will result in original value passed to first. C-style casting of at least some types behaves much the same way - casting back and forth results with the same value - Currently I am working with enumerations and int s, but there are some other examples as well. While C++ standard gives those definitions for both cast

Print an address of function in C++, g++/clang++ vs vc++ , who is right?

帅比萌擦擦* 提交于 2019-12-11 08:54:53
问题 Consider following simple program: #include <iostream> void foo() { } int main() { std::cout<<static_cast<void*>(foo); } It compiles fine on VC++ but g++ & clang++ gives compilation errors. See live demo here ( VC++ ) See live demo here ( clang++ ) See live demo here ( g++ ) Diagnostics given by g++ & clang++ : source_file.cpp: In function ‘int main()’: source_file.cpp:4:38: error: invalid static_cast from type ‘void()’ to type ‘void*’ std::cout<<static_cast<void*>(foo); ^ So, the question is

Multiple inheritance and the this pointer

筅森魡賤 提交于 2019-12-10 18:33:40
问题 Suppose I have this struct: struct vector_data { double x, y; double& operator[](size_t index) { return * (static_cast<double*>(static_cast<void*>(this)) + index); } }; The operator[] should work as expected, because vector_data is a POD type. The expected behaviour is that vector_data[0] returns x, and vector_data[1] returns y. Now suppose I have a second struct: struct more_data { double evil_data; // There could be more here, data or functions }; And derive from both like this: struct

What’s the best way to cast a function pointer from one type to another?

久未见 提交于 2019-12-10 16:29:29
问题 I’ve searched Stack Overflow for an answer, but I get nothing specific to this problem: only general cases about use of various types of cast operators. So, the case in point is when retrieving a function address with the Windows GetProcAddress() API call, which returns a function pointer of type FARPROC , with: typedef INT_PTR (__stdcall *FARPROC)(); . The trouble is, the actual function sought rarely (if ever) has this actual signature, as shown in the MRCE code, below. In this code, I have

reinterpret_cast to the same type

て烟熏妆下的殇ゞ 提交于 2019-12-10 16:01:44
问题 Consider following program: struct A{}; int main() { A a; A b = a; A c = reinterpret_cast<A>(a); } The compiler(g++14) throws an error about invalid cast from type 'A' to type 'A' . Why is casting to the same type invalid? 回答1: It is not allowed, because the standard says so. There is a rather limited set of allowed conversion that you can do with reinterpret_cast . See eg cppreference. For example the first point listed there is: 1) An expression of integral, enumeration, pointer, or pointer

reinterpret_cast std::function* to and from void*

半腔热情 提交于 2019-12-10 14:16:40
问题 I'm suffering a segfault in a plugin when I call a std::function in it passed from the main executable, via converting it's address to/from void* . I can reproduce the problem in a few self-contained lines: #include <iostream> #include <functional> int main() { using func_t = std::function<const std::string& ()>; auto hn_getter = func_t{[]() { return "Hello"; }}; auto ptr = reinterpret_cast<void*>(&hn_getter); auto getter = reinterpret_cast<func_t*>(ptr); std::cout << (*getter)() << std::endl

Strict pointer aliasing: any solution for a specific problem?

别来无恙 提交于 2019-12-10 13:00:58
问题 I have a problem caused by breaking strict pointer aliasing rule. I have a type T that comes from a template and some integral type Int of the same size (as with sizeof ). My code essentially does the following: T x = some_other_t; if (*reinterpret_cast <Int*> (&x) == 0) ... Because T is some arbitary (other than the size restriction) type that could have a constructor, I cannot make a union of T and Int . (This is allowed only in C++0x only and isn't even supported by GCC yet). Is there any

Convert from QByteArray to array of double

半腔热情 提交于 2019-12-10 09:19:25
问题 I have an array of double : QVector<double> Y(count); I need to pack it to QByteArray to send via Ethernet. So I did it. It was not too hard: QByteArray line; line.clear(); line.append(QByteArray::fromRawData(reinterpret_cast<const char*>(Y.data()), count*sizeof(double))); I try use this code to unpack the data from QByteArray recv : QVector<double> data((line.size())/sizeof(double)); QByteArray dou(sizeof(double),0x0); for(int i = 0; i<data.count(); i++){ dou = recv.mid(i*sizeof(double)

reinterpret_cast bug or UB? [duplicate]

别等时光非礼了梦想. 提交于 2019-12-09 14:19:56
问题 This question already has answers here : Observing weird behavior with 'auto' and std::minmax (1 answer) structured bindings with std::minmax and rvalues (2 answers) Closed last year . Consider following code: #include <cstdint> #include <algorithm> std::uintptr_t minPointer(void *first, void *second) { const auto pair = std::minmax( reinterpret_cast<std::uintptr_t>(first), reinterpret_cast<std::uintptr_t>(second) ); return pair.first; } and the assembly generated by GCC8 with -O3 on https:/