结构体的无符号字符别名
Unsigned char alias for struct
假设我有这个函数:
template<class T>
uint8_t* toBytes(T&& obj)
{
uint8_t* array = new uint8_t[sizeof(T)];
for (int x = 0; x < sizeof(T); x++)
{
array[x] = reinterpret_cast<uint8_t*>(&obj)[x];
}
return array;
}
我相当确定这是定义的行为(只要不期望内存看起来像任何特定的东西…我认为).
现在假设我有另一个函数:
template<class T>
T* toType(uint8_t* array)
{
return reinterpret_cast<T*>(array);
}
是否定义了以下内容?
class A { /* Members of A */ };
A a;
uint8_t array = toBytes(a);
A* anotherA = toType<A>(array);
由于对齐问题,我认为它未定义。new uint8_t[sizeof(T)];
不一定返回适合T
对齐的内存
相关文章:
- C++字符*缓冲区的大小
- HEX值到wchar_t字符(UTF-8)的转换
- 部分定义/别名模板模板参数
- 为什么 Serial.println(<char[]>);返回随机字符?
- 我的字符计数代码计算错误.为什么
- 字符串-C++后显示的随机字符
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 如何在C++中从字符串中分割字符
- 为什么msgrcv()将垃圾字符馈送到缓冲区
- 指向指向字符数组的指针数组的指针
- 如何在C++20中创建模板别名的推导指南
- 如何用转义符替换字符串中的所有特殊字符
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 比较字符数组
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 别名漏洞是否适用于签名字符?
- C++20 中的严格别名规则是否允许标准 c++ unicode 字符和下划线类型之间"reinterpret
- 如何(优雅地)根据字符别名分配引用成员
- 结构体的无符号字符别名