reinterpret-cast 相关问题

一种C ++运算符,它通过重新解释基础位模式简单地允许类型之间的转换。在一般使用中,这相当于指向要转换为任何其他指针类型的指针,并且它还可以允许将整数类型转换为任何指针类型,反之亦然。

reinterpret_cast 何时违法?

我写了一段代码,积累了不必要的内存空间,并在需要时将其归还,将其组织为堆栈(客户的要求很奇怪)。 void* 堆栈 = nullptr; 模板 我写了一段代码,积累了不必要的内存空间,并在需要时将其归还,将其组织为堆栈(客户的要求很奇怪)。 void* stack = nullptr; template <typename P> void push(P* p) { // reinterpret memory space of *p as a storage for the address of the top element of the stack; *reinterpret_cast<void**>(p) = stack; // now *p is the top element, so update stack to point to *p; stack = p; } template <typename P> P* pop() { if (stack == nullptr) throw; // get the address of the top element P* p = static_cast<P*>(stack); // now stack should point to the next element after *p, which address is stored in *p's memory space stack = *reinterpret_cast<void**>(stack); return p; } stack是指向顶部元素的指针。当新的内存空间为 pushed 时,其前 8 个字节用于存储前一个元素的地址,stack 被更新。一个简单的栈链。 问题 当我试图实现这种行为时,编译器经常这样说(我知道某些内存操作是非法的): 对强制转换的赋值是非法的,不支持左值强制转换 我想知道我的实施现在是否合法? 示例 #include <iostream> #include <bitset> constexpr size_t bytes_8 = 8 * 8; constexpr size_t bytes_20 = 8 * 20; constexpr size_t bytes_50 = 8 * 50; // we assume that std::bitset holds its data contigiously in the beginning struct A { std::bitset<bytes_8> bits; }; struct B { std::bitset<bytes_20> bits; }; struct C { std::bitset<bytes_50> bits; }; int main() { // Imagine they were allocated on heap: A a; B b; C c; std::cout << "Addresses" << '\n' << "a: " << &a << "\t" << "b: " << &b << "\t" << "c: " << &c << std::endl << std::endl << std::endl; push<A>(&a); std::cout << "---------- push<A>(&a) ----------\n" << "'a' is the top element (and the only one), so 'stack' points to it,\n" << "meanwhile 'a' stores the address of the previous element (none)\n\n" << "stack: " << stack << "\ta.bits: " << std::hex << a.bits.to_ullong() << std::endl << std::endl; push<B>(&b); std::cout << "---------- push<B>(&b) ----------\n" << "now 'b' is the top element, so 'stack' updates to point to it,\n" << "'b' stores the address of the previous element (a)\n\n" << "stack: " << stack << "\tb.bits: " << std::hex << b.bits.to_ullong() << std::endl << std::endl; push<C>(&c); std::cout << "---------- push<C>(&c) ----------\n" << "finally 'c' is the top element and 'stack' points to it,\n" << "'c' stores the address of the previous element (b)\n\n" << "stack: " << stack << "\tc.bits: " << std::hex << c.bits.to_ullong() << std::endl << std::endl << std::endl; auto p1 = pop<C>(); std::cout << "---------- pop<C>() ----------\n" << "'b' is the top element and the next one after it is 'a'\n\n" << "ret: " << p1 << "\nstack: " << stack << "\tb.bits: " << std::hex << b.bits.to_ullong() << std::endl << std::endl; auto p2 = pop<B>(); std::cout << "---------- pop<B>() ----------\n" << "'a' is the top element and the last one in the stack\n\n" << "ret: " << p2 << "\nstack: " << stack << "\ta.bits: " << std::hex << a.bits.to_ullong() << std::endl << std::endl; auto p3 = pop<A>(); std::cout << "---------- pop<A>() ----------\n" << "the stack is empty\n\n" << "ret: " << p3 << "\nstack: " << "0x" << stack << std::endl << std::endl; return 0; } 输出: Addresses a: 0x7fffffffdc20 b: 0x7fffffffdc40 c: 0x7fffffffdc60 ---------- push<A>(&a) ---------- 'a' is the top element (and the only one), so 'stack' points to it, meanwhile 'a' stores the address of the previous element (none) stack: 0x7fffffffdc20 a.bits: 0 ---------- push<B>(&b) ---------- now 'b' is the top element, so 'stack' updates to point to it, 'b' stores the address of the previous element (a) stack: 0x7fffffffdc40 b.bits: 7fffffffdc20 ---------- push<C>(&c) ---------- finally 'c' is the top element and 'stack' points to it, 'c' stores the address of the previous element (b) stack: 0x7fffffffdc60 c.bits: 7fffffffdc40 ---------- pop<C>() ---------- 'b' is the top element and the next one after it is 'a' ret: 0x7fffffffdc60 stack: 0x7fffffffdc40 b.bits: 7fffffffdc20 ---------- pop<B>() ---------- 'a' is the top element and the last one in the stack ret: 0x7fffffffdc40 stack: 0x7fffffffdc20 a.bits: 0 ---------- pop<A>() ---------- the stack is empty ret: 0x7fffffffdc20 stack: 0x0 即使您设法删除未定义的行为,您的实现也不可能工作。 将元素推入堆栈会覆盖其中的一部分。这是一个有损操作。 sizeof void* 对象表示开头的 *p 字节已经消失,永远无法恢复。即使您不再使用该对象,它的析构函数仍然需要运行,并且通常需要该数据。

回答 1 投票 0

能否以安全的方式优化多个连续无符号整数的比较?

在 GCC 13.2 和 Clang 18.0.1 中使用 -O3,为操作符生成的程序集 < of a tuple of four std::uint16_t objects is less than impressive (pun intended). Two other implementations using

回答 1 投票 0

我可以将placement new 与类数组一起使用吗?

考虑以下代码: 结构块{ alignas(int) 无符号字符数据[sizeof(int)]; }; int main() { 块 buff[sizeof(double) / sizeof(int)]; ::new(&buff) 双(); 双 d = *std::

回答 1 投票 0

重新解释指针的指针转换

我有两个类,一个父类和一个子类(从父类继承)我想知道是否可以使用reinterpret_cast或som将子**转换/转换为父**。

回答 1 投票 0

在给定正确的静态断言的情况下,`reinterpret_cast` 是否会从派生 -> 内存 -> 基安全?

我正在编写 C++14 代码,需要尽快反序列化对象。 序列化位于内存中,并且与反序列化发生在同一进程中。 考虑一下: 结构体基础 { ...

回答 1 投票 0

相当于C#中C++的reinterpret_cast

我想知道C#中C++的reinterpret_cast相当于什么!? 这是我的样本: 类基类 { 受保护的 int 计数器 = 0; } Foo 类:基础 { 公共整数计数器 { 得到{ ret...

回答 8 投票 0

从指针转换为 c 数组引用?

是否可以将分配的内存转换为对 c 数组的引用而不调用未定义的行为? 我在堆中有 4 个元素,希望将其视为 2x2 c 数组(传递给函数......

回答 1 投票 0

MessageBox 描述只有 1 个字母

std::string Window::Exception::TranslateErrorCode(HRESULT hr) noexcept { char* pMsgBuf = nullptr; DWORD nMsgLen = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | 格式消息分配缓冲区

回答 1 投票 0

使用一些模板化方法理解reinterpret_cast错误

我正在尝试使用一些 C++ ARM 编译器来编译以下代码。 voidpairs_converter(const mem_t& mem, uint16_t* data) {/* 一些东西 */} 模板 排队 无效

回答 1 投票 0

通过不带参数的指针调用函数[重复]

我想实现一个返回默认值的通用函数,然后通过函数指针使用它来替换具有不同原型的其他函数。 例如: int 替换...

回答 1 投票 0

std::as_writable_bytes何时触发未定义的行为

我以为我终于理解了reinterpret_cast和严格别名,然后我遇到了这个例子,稍微修改了https://en.cppreference.co上的“无效标量”示例...

回答 1 投票 0

在 C++14 中放置 new + reinterpret_cast:格式良好?

考虑以下 C++14 中的示例: alignas(T) 无符号字符数据[sizeof(T)]; 新(数据)T(); T* p = reinterpret_cast(数据); p->某事(); // 布? 这段代码合法吗?或者是

回答 1 投票 0

reinterpret_cast<const> 放弃 const 限定符? [重复]

在下面的情况下,我尝试将指向结构的指针转换为指向另一个结构的指针(该结构在幕后具有相同的内存布局)。我尝试以正确的方式执行此操作,但是

回答 1 投票 0

用于指针和引用转换的 `reinterpret_cast` 是否完全被新 C++ 标准的功能所取代?

reinterpret_cast 被很多人认为是危险的,并且在不调用 UB 的情况下很难正确使用它。它也有各种不同的用法,无法传达

回答 1 投票 0

将 3D 向量分配给 4d 向量或如何使用 OpenCV<double, 4> 将 cv::Vec<double, 3> 转换为 cv::Vec

是否有更好或更有效的方法将 cv::Vec 转换为 cv::Vec。 该示例显示了我当前使用reinterpret_cast 的解决方案。我想指出的是...

回答 1 投票 0

How to view-cast / reinterpret-cast in pythran / numpy?

我正在尝试在 pythran 中进行 numpy view-casting(我相信在 C/C++ 领域将被称为 reinterpret-casting): 以下愚蠢的虚构示例采用无符号 8 字节整数数组,

回答 1 投票 0

我们能否将 `Eigen::Vector<T, D + 1>& x` `reinterpret_cast` 为 `Eigen::Vector<T, D>&` 以从 `x` 中提取第一个 `D` 分量?

我需要计算 D 维函数的函数值和梯度,并将这些值相加。我认为将梯度部分简单地存储在 ...

回答 2 投票 0

在指针和引用上使用“reinterpret_cast”时编译器警告的差异[关闭]

假设我有两个结构 结构B { 诠释 n; }; 结构C { 诠释 k; }; 和 B b = {}; C& c = reinterpret_cast(b); //不好,编译器(gcc 8.5 with -O2 -Wall)不开心 C * c1 =

回答 1 投票 0

为什么 interpret_cast 运算符在 cpp 中被认为是危险的?有更好的方法吗?

我正在研究cpp中文件中的随机访问。我遇到了 reinterpret_cast 运算符。它允许您将指针从一种类型转换为另一种类型,即使这些类型之间没有任何关联。

回答 0 投票 0

在下面的代码中我不想使用 reinterpret_cast 任何人都可以建议我一个好的解决方案而不是 reinterpret_cast 吗?

在下面的代码中我不想使用 reinterpret_cast 任何人都可以建议我一个好的解决方案而不是 reinterpret_cast 吗? #include #include 使用

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.