reinterpret-cast

Proper way of casting pointer types

牧云@^-^@ 提交于 2019-12-17 17:39:15
问题 Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast<BYTE*>( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast ? I used to think that reinterpret_cast is OK for e.g. casting pointers to and from integer types (like e.g. DWORD_PTR ), but to cast from a void* to a BYTE* , isn't static_cast OK? Are there any (subtle?) differences in this particular case, or are they just

Proper way of casting pointer types

你说的曾经没有我的故事 提交于 2019-12-17 17:39:09
问题 Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast<BYTE*>( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast ? I used to think that reinterpret_cast is OK for e.g. casting pointers to and from integer types (like e.g. DWORD_PTR ), but to cast from a void* to a BYTE* , isn't static_cast OK? Are there any (subtle?) differences in this particular case, or are they just

Using reinterpret_cast to check inheritance at compile time

﹥>﹥吖頭↗ 提交于 2019-12-13 09:33:53
问题 Regarding this question: When to use reinterpret_cast? I found sth. like this: template<typename T> bool addModuleFactoryToViewingFactory(ViewingPackage::ViewingFactory* pViewingFactory) { static_cast<ModuleFactory*>(reinterpret_cast<T*>(0)); // Inheritance compile time check ... } Is this a good way to check whether T can be casted to ModuleFactory at compile time? I mean, to check if the programmer put valid stuff into the <> of addModuleFactoryToViewingFactory<T>(...) Is this okay, good or

C# reinterpret bool as byte/int (branch-free)

∥☆過路亽.° 提交于 2019-12-13 03:55:20
问题 Is it possible in C# to turn a bool into a byte or int (or any integral type, really) without branching ? In other words, this is not good enough: var myInt = myBool ? 1 : 0; We might say we want to reinterpret a bool as the underlying byte , preferably in as few instructions as possible. The purpose is to avoid branch prediction fails as seen here. 回答1: Here is a solution that takes more lines (and presumably more instructions) than I would like, but that actually solves the problem directly

Is it undefined behavior to reinterpret_cast an object of an unrelated type to an empty class

对着背影说爱祢 提交于 2019-12-13 01:07:35
问题 It it undefined behavior to cast an unrelated type to an empty base class? And then use that address to construct a derived type that inherits from that empty base? For example class Derived : public EmptyBase { public: template <typename T> Derived(T&& t) : EmptyBase{std::forward<T>(t)} {} using EmptyBase::print; }; and then is it undefined to do something like this static auto shim = 1; auto derived = Derived{*reinterpret_cast<EmptyBase*>(&shim)}; derived.print(); The standard guarantees

Is this a legitimate use of reinterpret_cast and if not how do I do this? [duplicate]

∥☆過路亽.° 提交于 2019-12-12 22:18:23
问题 This question already has answers here : Is casting std::pair<T1, T2> const& to std::pair<T1 const, T2> const& safe? (3 answers) Closed 4 years ago . This code demonstrates the problem I'm trying to solve: #include <map> class Point { public: float m_x; float m_y; }; typedef std::set<Point *> PointSet; typedef std::set<const Point * const> ConstPointSet; float GetMinimumRange(const ConstPointSet &pointSet) { float minimumRange(0.0f); // find the smallest distance between any pair of points in

Does casting to an unrelated reference type violate the strict aliasing rule?

岁酱吖の 提交于 2019-12-12 21:33:02
问题 The strict aliasing rule says If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined: — the dynamic type of the object, — a cv-qualified version of the dynamic type of the object, — a type similar (as defined in 4.4) to the dynamic type of the object, — a type that is the signed or unsigned type corresponding to the dynamic type of the object, — a type that is the signed or unsigned type corresponding

Is it unsafe to mix static and reinterpret cast when casting to and back from void*?

[亡魂溺海] 提交于 2019-12-12 16:27:11
问题 Simply: If i static_cast a type X* to void* , is it always safe to reinterpret_cast it back to X*? I am unable to produce any case where this fails for example: #include <iostream> struct a { int* m_array; }; int main() { bool fail = false; for(int i = 0; ++i < 5000;) { a* pA = new a; pA->m_array = new int [i+1]; // A new size of data everytime pA->m_array[i] = 10; void* pvA = static_cast<void*>(pA); pA = reinterpret_cast<a*>(pvA); if(pA->m_array[i] != 10) { fail = true; break; } delete []pA-

Can I reinterpret std::vector<char> as a std::vector<unsigned char> without copying?

痞子三分冷 提交于 2019-12-12 10:58:43
问题 I have a reference to std::vector<char> that I want to use as a parameter to a function which accepts std::vector<unsigned char> . Can I do this without copying? I have following function and it works; however I am not sure if a copy actually takes place - could someone help me understanding this? Is it possible to use std::move to avoid copy or is it already not being copied? static void showDataBlock(bool usefold, bool usecolor, std::vector<char> &chunkdata) { char* buf = chunkdata.data();

Why can I use static_cast With void* but not With char*

独自空忆成欢 提交于 2019-12-12 06:34:09
问题 I know that reinterpret_cast is primarily used going to or from a char* . But I was surprised to find that static_cast could do the same with a void* . For example: auto foo "hello world"s; auto temp = static_cast<void*>(&foo); auto bar = static_cast<string*>(temp); What do we gain from using reinterpret_cast and char* over static_cast and void* ? Is it something to do with the strict aliasing problem? 回答1: Generally speaking, static_cast will do cast any two types if one of them can be cast