reinterpret-cast

Using reinterpret_cast to read file into structure

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-20 04:58:25
问题 struct DATAs { char data1; short data2; short data3; float data4; int data5; short data6; unsigned short data7; short data8; char data9; }; void fixFile(char* filename) { std::ifstream InputFile; InputFile.open(filename, std::ios::binary); DATAs FileDatas; InputFile.read(reinterpret_cast<char*>(&FileDatas), sizeof(FileDatas)); } Why do I need to use "reinterpret_cast" for the reading instead of "InputFile.read(&FileDatas, sizeof(FileDatas))" ? 回答1: The type of the first argument to std:

casting member function pointer

感情迁移 提交于 2021-02-07 14:48:36
问题 I need to use a member function pointer that takes in an argument of base class that used in other code. Well, simply I want do to [something] like the example below. This code works fine, but I wonder if such cast is always safe? I cannot do dynamic or static cast here. #include <cstdio> class C { public: C () : c('c') {} virtual ~C() {} const char c; }; class D : public C { public: D () : d('d') {} virtual ~D() {} const char d; }; class A { public: A () {} virtual ~A() {} void f( C& c ) {

casting member function pointer

蓝咒 提交于 2021-02-07 14:47:31
问题 I need to use a member function pointer that takes in an argument of base class that used in other code. Well, simply I want do to [something] like the example below. This code works fine, but I wonder if such cast is always safe? I cannot do dynamic or static cast here. #include <cstdio> class C { public: C () : c('c') {} virtual ~C() {} const char c; }; class D : public C { public: D () : d('d') {} virtual ~D() {} const char d; }; class A { public: A () {} virtual ~A() {} void f( C& c ) {

Reinterpret Array of Bytes into Managed Struct Using Fixed Buffers

谁说胖子不能爱 提交于 2021-01-29 02:30:18
问题 I'm looking to reinterpret_cast an array of bytes into a C# struct. I've read several other answers to the problem, most have been about how to implement reinterpret cast. I've settled on a means to reinterpret cast, but I'm getting single characters instead of arrays of characters during my casting. For instance, I have the following object: public unsafe struct Establish503 { public static Establish503 ReinterpretCast(byte[] message) { GCHandle handle = GCHandle.Alloc(message, GCHandleType

How to cast char array to int at non-aligned position?

杀马特。学长 韩版系。学妹 提交于 2021-01-28 06:24:00
问题 Is there a way in C/C++ to cast a char array to an int at any position? I tried the following, bit it automatically aligns to the nearest 32 bits (on a 32 bit architecture) if I try to use pointer arithmetic with non-const offsets: unsigned char data[8]; data[0] = 0; data[1] = 1; ... data[7] = 7; int32_t p = 3; int32_t d1 = *((int*)(data+3)); // = 0x03040506 CORRECT int32_t d2 = *((int*)(data+p)); // = 0x00010203 WRONG Update: As stated in the comments the input comes in tuples of 3 and I

Can I convert a null pointer of int to a long type by reinterpret_cast

。_饼干妹妹 提交于 2021-01-27 02:54:08
问题 int *pt = 0; long i = reinterpret_cast<long>(pt); Is i guaranteed to be 0? Is this well defined or implementation-defined? I checked the c++ standard, but it only states that A pointer to a data object or to a function (but not a pointer to member) can be converted to any integer type large enough to contain it. In this case, pt doesn't point to any data object. Does the rule apply to this case? 回答1: No , i is not necessarily any value. The result is implementation-defined. † The

Why is reinterpret_cast not constexpr?

北慕城南 提交于 2020-07-20 07:48:10
问题 Consider the following snippet: static constexpr uint8_t a = 0; static constexpr const int8_t *b = reinterpret_cast<const int8_t *>(&a); This fails to compile with error: a reinterpret_cast is not a constant expression , because the C++ standard forbids using reinterpret_cast in constexpr . However compilation succeeds if I want to store the value b in PROGMEM (for AVR microcontrollers): static constexpr uint8_t a = 0; static const int8_t PROGMEM *const b = reinterpret_cast<const int8_t *>(&a

Using `reinterpret_cast` on an enum class - valid or undefined behavior?

北战南征 提交于 2020-06-17 06:47:51
问题 #include <iostream> #include <cassert> #include <type_traits> template<typename T> using Underlying = std::underlying_type_t<T>; enum class ETest : int { Zero = 0, One = 1, Two = 2 }; template<typename T> auto& castEnum(T& mX) noexcept { // `static_cast` does not compile // return static_cast<Underlying<T>&>(mX); return reinterpret_cast<Underlying<T>&>(mX); } int main() { auto x(ETest::Zero); castEnum(x) = 1; assert(x == ETest::One); return 0; } ideone Is this code guaranteed to always work?