在 C++ 结构内声明的数组 A[1] 创建多个实例

creating multiple instances of an array a[1] declared inside a structure in c++

本文关键字:创建 实例 结构 C++ 声明 数组      更新时间:2023-10-16

假设我有两个结构如下:

struct address{
int x;
int y;
} addr;
struct details{
int count;
int size;
addr addre[1];// Instances of count number of addresses
} detail;                 

如何创建一个变量,比如det,它有多个由计数定义的addre实例?

这是通过在对象末尾分配动态大小的容器(具有固定大小(来减少内存分配数量和提高引用局部性的常见技巧。

不过,C++,使用该额外的member[1]会导致一些麻烦 - 该成员会自动初始化,而其余元素则不会。最好完全避免声明该成员,而是为元素提供访问器/迭代器。然后手动初始化并销毁所有成员。例如:

struct address {
int x;
int y;
};
struct details {
int count;
int size;
address* addr_begin() { return reinterpret_cast<address*>(this + 1); }
address* addr_end() { return addr_begin() + count; }
static void* operator new(size_t sizeof_details, int count) {
return ::operator new(sizeof_details + count * sizeof(address));
}
static void operator delete(void* p) {
::operator delete(p);
}
static std::unique_ptr<details> create(int count, int size) {
return std::unique_ptr<details>(new(count) details(count, size));
}
~details() {
std::for_each(addr_begin(), addr_end(), [](address& a) { a.~address(); });
}
private:
details(int count, int size)
: count(count)
, size(size)
{
std::uninitialized_fill(addr_begin(), addr_end(), address{});
}
};
int main() {
auto det = details::create(10, 10);
}

如果无法更改结构,则:

#include <new>
#include <algorithm>
struct address {
int x;
int y;
};
struct details {
int count;
int size;
address addre[1];
};
details* create_details(int count, int size) {
void* mem = ::operator new(sizeof(details) + (count - 1) * sizeof(address));
auto* p = new (mem) details{count, size};
std::uninitialized_fill(p->addre + 1, p->addre + count, address{});
return p;
}
void destroy_details(details* p) {
std::for_each(p->addre + 1, p->addre + p->count, [](address& a) { a.~address(); });
p->~details();
::operator delete(p);
}
int main() {
auto* p = create_details(10, 10);
destroy_details(p);
}

如果我正确回答了你的问题(我不确定,因为它很清楚(,你有一个结构

struct address {
int x;
int y;
};

并且您要定义另一个details,它包含addressdetails.count个实例的集合。你基本上有两个选择。

details.count在编译时已知

在这种情况下,最好的选择是将count定义为非类型模板参数并使用std::array

template <std::size_t COUNT>
struct details {
static constexpr std::size_t count = COUNT;
std::array<address, COUNT> addresses;
};
// ...
details<42> det;
// det.addresses[0] to det.addresses[41] are valid (but uninitialized) addresses

details.count在编译时未知

在这种情况下,最好的选择是使用std::vector并为施工提供count,甚至在施工后向details.addresses添加addresses:

struct details {
std::vector<address> addresses;
};
// ...
details det;
det.addresses.emplace_back(address{0, 0}); // adds and initializesdet.addresses[0]

被 YSC 击败了几秒钟,只是给我留下了一些补充:

如果大小和容量是您额外需要的,您甚至可以直接使用向量:

using Details = std::vector<address>;

由于您显然需要大小和容量,因此对 std::array 执行相同的操作可能不符合您的需求,因此您可能会继续使用聚合......