[英]Is a vector of arrays contiguous?
If I create如果我创建
std::vector<std::array<double, 2>> points;
std::vector<double> points2;
I know that points2 will be a contiguous chunk of memory holding doubles in the heap.我知道 points2 将是 memory 在堆中持有双打的连续块。 I think that points will be a contiguous chunk of memory of double* to the stack?我认为这些点将是 memory 的连续块,双 * 到堆栈? But will those array be contiguous in the stack?但是这些数组在堆栈中会是连续的吗? Let's say that I am storing pairs of doubles to represents some points.假设我正在存储成对的双打来代表一些点。
points2 is in memory like this: [x0 y0 x1 y1 x2 y2...] What about points? points2 在 memory 中是这样的: [x0 y0 x1 y1 x2 y2...] 点数呢? What is the best way to store pair of doubles in this case?在这种情况下,存储双打的最佳方式是什么? Thanks for any tip.感谢您的任何提示。
Is a vector of arrays contiguous? arrays 的向量是否连续?
No, it is not.不它不是。 std::array
may contain padding or even additional members at the end. std::array
最后可能包含填充甚至附加成员。 More details, eg, here:更多细节,例如,这里:
But I believe this is very unlikely to happen and you can simply check such situations by comparing 2 * sizeof(double)
with sizeof(std::array<double, 2>)
.但我相信这不太可能发生,您可以通过比较2 * sizeof(double)
和sizeof(std::array<double, 2>)
来简单地检查这种情况。
Assuming that sizeof(std::array<double, 2>)
is an integer multiple of alignof(std::array<double, 2>)
, then they should be stored contiguously with no padding.假设sizeof(std::array<double, 2>)
是alignof(std::array<double, 2>)
<double, 2>) 的 integer 倍数,那么它们应该连续存储而没有填充。
And it's pretty likely that this will always be the case.而且很可能永远都是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.