假设 STL 向量存储始终是连续的是否安全?

发布于 2024-07-08 14:21:28 字数 211 浏览 5 评论 0原文

如果您有一个已调整大小的 STL 向量,那么获取元素 0 的地址并假设向量的其余部分将跟随在内存中是否安全?

例如

vector<char> vc(100);
// do some stuff with vc
vc.resize(200);
char* p = &vc[0];
// do stuff with *p

If you have an STL vector which has been resized, is it safe to take the address of element 0 and assume the rest of the vector will follow in memory?

e.g.

vector<char> vc(100);
// do some stuff with vc
vc.resize(200);
char* p = &vc[0];
// do stuff with *p

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

Smile简单爱 2024-07-15 14:21:28

是的,这是一个有效的假设 (*)。

来自 C++03 标准 (23.2.4.1):

存储向量的元素
连续,这意味着如果 v 是
向量,其中 T 是某个
输入 bool 以外的类型,则它遵循
恒等式 &v[n] == &v[0] + n 为
所有 0 <= n < v.size()。

(*) ...但要注意向数组添加元素后重新分配的数组(使任何指针和迭代器无效)。

Yes, that is a valid assumption (*).

From the C++03 standard (23.2.4.1):

The elements of a vector are stored
contiguously, meaning that if v is a
vector where T is some
type other than bool, then it obeys
the identity &v[n] == &v[0] + n for
all 0 <= n < v.size().

(*) ... but watch out for the array being reallocated (invalidating any pointers and iterators) after adding elements to it.

双手揣兜 2024-07-15 14:21:28

C++03 标准添加了措辞以明确向量元素必须是连续的。

C++03 23.2.4 第 1 段包含以下 C++98 标准文档中的语言:

存储向量的元素
连续,这意味着如果 v
vector 其中 T
除了 bool 之外的某种类型,那么它
遵循恒等式 &v[n] == &v[0] +
n
对于所有 0 <= n v.size().


Herb Sutter 在他的一篇博客文章中谈到了这一变化,不要畏缩:向量保证是连续的

...连续性实际上是
矢量抽象。 这太重要了,
事实上,当它被发现时
C++98 标准没有
完全保证连续性,
C++03标准修改为
明确添加保证。

The C++03 standard added wording to make it clear that vector elements must be contiguous.

C++03 23.2.4 Paragraph 1 contains the following language which is not in the C++98 standard document:

The elements of a vector are stored
contiguously, meaning that if v is a
vector<T, Allocator> where T is
some type other than bool, then it
obeys the identity &v[n] == &v[0] +
n
for all 0 <= n < v.size().

Herb Sutter talks about this change in one of his blog entries, Cringe not: Vectors are guaranteed to be contiguous:

... contiguity is in fact part of the
vector abstraction. It’s so important,
in fact, that when it was discovered
that the C++98 standard didn’t
completely guarantee contiguity, the
C++03 standard was amended to
explicitly add the guarantee.

长不大的小祸害 2024-07-15 14:21:28

存储始终是连续的,但它可能会随着向量容量的变化而移动。

如果在容量更改操作之前元素零(或任何元素)上有指针、引用或迭代器,则它会失效并且必须重新分配。

Storage is always contiguous, but it may move as the vector's capacity is changed.

If you had a pointer, reference, or iterator on element zero (or any element) before a capacity-changing operation, it is invalidated and must be reassigned.

铁憨憨 2024-07-15 14:21:28

std::vector 保证项目存储在连续数组中,因此是数组的首选替代品,也可用于与平台相关的低级代码(如 Win32 API 调用)进行交互)。 要获取指向数组的指针,请使用:

&myVector.front();

std::vector guarantees that the items are stored in a contiguous array, and is therefore the preferred replacement of arrays and can also be used to interface with platform-dependent low-level code (like Win32 API calls). To get a pointer to the array use:

&myVector.front();
一笑百媚生 2024-07-15 14:21:28

是的。

它应该总是连续的

yes.

it should alway be contiguous

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文