【发布时间】:2023-03-19 09:57:02
【问题描述】:
下面代码的正确写法是什么?
我有一个内存管理器,它为我提供char *,但我需要使用uint32_t 的数组。如何解决严格的别名规则?我知道对于单个对象,建议仅使用 memcpy() 复制内容,但该解决方案不适用于对象数组。
char* ptr = manager()->Allocate(1000 * sizeof(uint32_));
uint32_t* u32ptr = reinterpret_cast<uint32_t*>(ptr);
....
u32ptr[x] = y;
【问题讨论】:
-
您的经理应该返回
void*。它会解决你所有的问题(不包括) -
我早期的秃头问题是 10 年前。现在只是秃顶。
-
对不起,在写这纯粹是个玩笑之前我没有看到你的个人资料照片。没有冒犯?
-
哈哈哈 :) 无意冒犯 :)
标签: c++ c++11 pointers dynamic-memory-allocation reinterpret-cast