gpt4 book ai didi

c++ - 写入类的最后一个字节

转载 作者:太空狗 更新时间:2023-10-29 19:53:00 26 4
gpt4 key购买 nike

给定具有标准布局成员的标准布局类,例如:

struct foo {
int n;
int m;
unsigned char garbage;
};

根据标准,写入结构的最后一个字节而不写入 nm 的内存区域是否总是安全的(但可能写入垃圾)?例如,

foo f;
*(static_cast<unsigned char *>(static_cast<void *>(&f)) + (sizeof(foo) - 1u)) = 0u;

在花了一些时间阅读 C++11 标准后,在我看来答案可能是肯定的。

从 9.2/15 开始:

Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

因此,garbage 成员的地址高于其他两个成员(它们本身是连续存储的,因为它们具有标准布局),因此结构的最后一个字节必须属于 garbage 或成为最终填充的一部分。

这个推理正确吗?我在这里干预了 f 对象的生命周期吗?写入填充字节有问题吗?

编辑

在回复评论时,我在这里试图实现的目标与我正在编写的类似变体的类有关。

如果以直接的方式进行(即,在变体类中放置一个 int 成员来记录正在存储的类型),填充将使类比需要的大将近 50%。

我想做的是确保我要存储在变体中的每个类类型的每个最后一个字节都是可写的,所以我可以将存储标志合并到原始存储(对齐的原始字符数组)中我我在变体中使用。在我的具体案例中,这消除了大部分浪费的空间。

编辑 2

作为实际示例,考虑将这两个类存储在典型 64 位机器上的变体中:

// Small dynamic vector class storing 8-bit integers.
class first {
std::int8_t *m_ptr;
unsigned short m_size_capacity; // Size and capacity packed into a single ushort.
};

// Vector class with static storage.
class second {
std::int8_t m_data[15];
std::uint8_t m_size;
};

class variant
{
char m_data[...] // Properly sized and aligned for first and second.
bool m_flag; // Flag to signal which class is being stored.
};

这两个类的大小在我的机器上是 16,变体类中需要的额外成员使大小变为 24。如果我现在在末尾添加垃圾字节:

// Small dynamic vector class storing 8-bit integers.
class first {
std::int8_t *m_ptr;
unsigned short m_size_capacity; // Size and capacity packed into a single ushort.
unsigned char m_garbage;
};

// Vector class with static storage.
class second {
std::int8_t m_data[14]; // Note I lost a vector element here.
std::uint8_t m_size;
unsigned char m_garbage;
};

两个类的大小仍然是 16,但如果现在我可以自由使用每个类的最后一个字节,我可以去掉变体中的标志成员,最终大小仍然是 16。

最佳答案

相反,您应该将标签放在前面,然后是其他小成员。

// Small dynamic vector class storing 8-bit integers.
struct first
{
unsigned char m_tag;
std::uint8_t m_size;
std::uint8_t m_capacity;
std::int8_t *m_ptr;
};

// Vector class with static storage.
struct second
{
unsigned char m_tag;
std::uint8_t m_size;
std::int8_t m_data[14];
};

然后,语言规则允许您将它们放入一个 union 并使用其中任何一个访问 m_tag,即使它不是 union ,因为初始布局是相同的(成员的公共(public)初始序列的特殊规则)。

union tight_vector
{
first dynamic;
second small_opt;
};

tight_vector v;
if (v.dynamic.m_size < 4) throw std::exception("Not enough data");
if (v.dynamic.m_tag == DYNAMIC) { /* use v.dynamic */ }
else { /* use v.small_opt */ }

有问题的规则是 9.2/18:

If a standard-layout union contains two or more standard-layout structs that share a common initial sequence, and if the standard-layout union object currently contains one of these standard-layout structs, it is permitted to inspect the common initial part of any of them. Two standard-layout structs share a common initial sequence if corresponding members have layout-compatible types and either neither member is a bit-field or both are bit-fields with the same width for a sequence of one or more initial members.

关于c++ - 写入类的最后一个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18564497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com