What is the memory layout of vector of arrays?
can anybody explaine the memory layout of
std::vector<std::array<int, 5>> vec(2)
does it provide contiguous memory block of a 2D array
with 2 rows of 5 elements?
To my understanding, the vector of vectors
std::vector<std::vector<int>> vec(2, std::vector<int>(5))
provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.
Will it be the same for the vector of arrays?
c++ c++11 language-lawyer stdvector stdarray
add a comment |
can anybody explaine the memory layout of
std::vector<std::array<int, 5>> vec(2)
does it provide contiguous memory block of a 2D array
with 2 rows of 5 elements?
To my understanding, the vector of vectors
std::vector<std::vector<int>> vec(2, std::vector<int>(5))
provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.
Will it be the same for the vector of arrays?
c++ c++11 language-lawyer stdvector stdarray
Given the answers, if you want this, usestd::vector<int> vec(5*2)
and do 2D indexing yourself inside the flat 1D array. Maybe write a wrapper class for 2D indexing on top of a flat container, with either a templated or runtime-variable row length. You'd also want to expose a flat view so algorithms that just need to do something to every element without caring about 2D position can do that with one big loop, more efficiently.
– Peter Cordes
5 hours ago
add a comment |
can anybody explaine the memory layout of
std::vector<std::array<int, 5>> vec(2)
does it provide contiguous memory block of a 2D array
with 2 rows of 5 elements?
To my understanding, the vector of vectors
std::vector<std::vector<int>> vec(2, std::vector<int>(5))
provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.
Will it be the same for the vector of arrays?
c++ c++11 language-lawyer stdvector stdarray
can anybody explaine the memory layout of
std::vector<std::array<int, 5>> vec(2)
does it provide contiguous memory block of a 2D array
with 2 rows of 5 elements?
To my understanding, the vector of vectors
std::vector<std::vector<int>> vec(2, std::vector<int>(5))
provide the memory layout of two contiguous arrays of length 5 elements in different locations in memory.
Will it be the same for the vector of arrays?
c++ c++11 language-lawyer stdvector stdarray
c++ c++11 language-lawyer stdvector stdarray
edited 18 hours ago
Bathsheba
176k27251373
176k27251373
asked 18 hours ago
ConstConst
197111
197111
Given the answers, if you want this, usestd::vector<int> vec(5*2)
and do 2D indexing yourself inside the flat 1D array. Maybe write a wrapper class for 2D indexing on top of a flat container, with either a templated or runtime-variable row length. You'd also want to expose a flat view so algorithms that just need to do something to every element without caring about 2D position can do that with one big loop, more efficiently.
– Peter Cordes
5 hours ago
add a comment |
Given the answers, if you want this, usestd::vector<int> vec(5*2)
and do 2D indexing yourself inside the flat 1D array. Maybe write a wrapper class for 2D indexing on top of a flat container, with either a templated or runtime-variable row length. You'd also want to expose a flat view so algorithms that just need to do something to every element without caring about 2D position can do that with one big loop, more efficiently.
– Peter Cordes
5 hours ago
Given the answers, if you want this, use
std::vector<int> vec(5*2)
and do 2D indexing yourself inside the flat 1D array. Maybe write a wrapper class for 2D indexing on top of a flat container, with either a templated or runtime-variable row length. You'd also want to expose a flat view so algorithms that just need to do something to every element without caring about 2D position can do that with one big loop, more efficiently.– Peter Cordes
5 hours ago
Given the answers, if you want this, use
std::vector<int> vec(5*2)
and do 2D indexing yourself inside the flat 1D array. Maybe write a wrapper class for 2D indexing on top of a flat container, with either a templated or runtime-variable row length. You'd also want to expose a flat view so algorithms that just need to do something to every element without caring about 2D position can do that with one big loop, more efficiently.– Peter Cordes
5 hours ago
add a comment |
4 Answers
4
active
oldest
votes
Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5>
literally contains five int
s in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".
However, the std::array
object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.
An int
+----+
| |
+----+
A vector of 2 x int
+----+----+----+-----+ +----+----+
| housekeeping | ptr | | 1 | 2 |
+----+----+----+-----+ +----+----+
| ^
-----------
An std::array<int, 5>
+----+----+----+----+----+----------->
| 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+----+----+----------->
A vector of 2 x std::array<int, 5>
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| ^
-----------
And, even if it were, due to aliasing rules, whether you'd be able to use a single int*
to navigate all 10 numbers would potentially be a different matter!
All in all, a vector of ten int
s would be clearer, completely packed, and possibly safer to use.
In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).
9
According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.
– Daniel Langr
18 hours ago
1
IOW, while the memory allocated has to be contiguous, the array elements need not be.
– MSalters
17 hours ago
1
Ooh this answer just gets posher and posher. Upped to 13.
– Bathsheba
16 hours ago
2
@DanielLangr That was a great discussion. Thanks for adding here...
– Const
14 hours ago
2
Notestatic_assert(sizeof(std::array<int,t>)==sizeof(int)*5)
mitigates any padding (and passes in every version of every major compiler that has supportedstd::array
). It does not mitigate against aliasing issues.
– Yakk - Adam Nevraumont
8 hours ago
|
show 4 more comments
The big difference between std::vector
and std::array
is that std::vector
contains a pointer to the memory it wraps, while std::array
contains the actual array in itself.
That means a vector of vectors is like a jagged array.
For a vector of arrays, the std::array
objects will be placed contiguously but separate from the vector object. Note that the std::array
object themselves may be larger than the array they contain, and if so then the data will not be contiguous.
The last bit also means that an array (plain C-style or std::array
) of std::array
may also not keep the data contiguously. The std::array
objects in the array will be contiguous, but not the data.
The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.
4
It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.
– Daniel Langr
18 hours ago
2
@DanielLangr Thanks for reminding me. Rephrased that part.
– Some programmer dude
18 hours ago
add a comment |
The C++ standard does not guarantee that std::array
doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.
Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.
The above also applies to a std::array<std::array>
.
add a comment |
static_assert(sizeof(std::array<int,5>)==5*sizeof(int));
the above mitigates against having any padding on the end of a std::array
. No major compiler will cause the above to fail to this date, and I'd bet won't in the future.
If and only if the above fails, then std::vector<std::array<int,5>> v(2)
will have a "gap" between the std::array
s.
This doesn't help as much as you'd like; a pointer generated as follows:
int* ptr = &v[0][0];
only has a domain of validity up to ptr+5
, and dereferencing ptr+5
is undefined behavior.
This is due to aliasing rules; you aren't allowed to "walk" past the end of one object into another, even if you know it is there, unless you first round-trip to certain types (like char*
) where less restricted pointer arithmetic is permitted.
That rule, in turn, exists to allow compilers to reason about what data is being accessed through which pointer, without having to prove that arbitrary pointer arithmetic will let you reach outside objects.
So:
struct bob {
int x,y,z;
};
bob b {1,2,3};
int* py = &b.y;
no matter what you do with py
as an int*
, you cannot legally modify x
or z
with it.
*py = 77;
py[-1]=3;
std::cout << b.x;
the complier can optimize the std::cout
line to simply print 1
, because the py[-1]=3
may attempt to modify b.x
, but doing so through that means is undefined behavior.
The same kind of restrictions prevent you from going from the first array in your std::vector
to the second (ie, beyond ptr+4
).
Creating ptr+5
is legal, but only as a one-past-the-end pointer. Comparing ptr+5 == &v[1][0]
is also not specified in result, even though their binary values are absolutely going to be identical in every compiler on every major hardware system.
If you want to go futher down the rabbit hole, it isn't even possible to implement std::vector<int>
within C++ itself due to these restrictions on pointer aliasing. Last I checked (which was before c++17, but I didn't see a resolution in C++17) the standard committee was working on solving this, but I don't know the state of any such effort. (This is less of a problem than you might think, because nothing requires that std::vector<int>
be implemented in standard-compliant C++; it must simply have standard-defined behavior. It can use compiler-specific extensions internally.)
Nice answer; upped. Note also the somewhat related issue that you can’t write malloc in standard C.
– Bathsheba
6 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54197195%2fwhat-is-the-memory-layout-of-vector-of-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5>
literally contains five int
s in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".
However, the std::array
object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.
An int
+----+
| |
+----+
A vector of 2 x int
+----+----+----+-----+ +----+----+
| housekeeping | ptr | | 1 | 2 |
+----+----+----+-----+ +----+----+
| ^
-----------
An std::array<int, 5>
+----+----+----+----+----+----------->
| 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+----+----+----------->
A vector of 2 x std::array<int, 5>
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| ^
-----------
And, even if it were, due to aliasing rules, whether you'd be able to use a single int*
to navigate all 10 numbers would potentially be a different matter!
All in all, a vector of ten int
s would be clearer, completely packed, and possibly safer to use.
In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).
9
According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.
– Daniel Langr
18 hours ago
1
IOW, while the memory allocated has to be contiguous, the array elements need not be.
– MSalters
17 hours ago
1
Ooh this answer just gets posher and posher. Upped to 13.
– Bathsheba
16 hours ago
2
@DanielLangr That was a great discussion. Thanks for adding here...
– Const
14 hours ago
2
Notestatic_assert(sizeof(std::array<int,t>)==sizeof(int)*5)
mitigates any padding (and passes in every version of every major compiler that has supportedstd::array
). It does not mitigate against aliasing issues.
– Yakk - Adam Nevraumont
8 hours ago
|
show 4 more comments
Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5>
literally contains five int
s in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".
However, the std::array
object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.
An int
+----+
| |
+----+
A vector of 2 x int
+----+----+----+-----+ +----+----+
| housekeeping | ptr | | 1 | 2 |
+----+----+----+-----+ +----+----+
| ^
-----------
An std::array<int, 5>
+----+----+----+----+----+----------->
| 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+----+----+----------->
A vector of 2 x std::array<int, 5>
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| ^
-----------
And, even if it were, due to aliasing rules, whether you'd be able to use a single int*
to navigate all 10 numbers would potentially be a different matter!
All in all, a vector of ten int
s would be clearer, completely packed, and possibly safer to use.
In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).
9
According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.
– Daniel Langr
18 hours ago
1
IOW, while the memory allocated has to be contiguous, the array elements need not be.
– MSalters
17 hours ago
1
Ooh this answer just gets posher and posher. Upped to 13.
– Bathsheba
16 hours ago
2
@DanielLangr That was a great discussion. Thanks for adding here...
– Const
14 hours ago
2
Notestatic_assert(sizeof(std::array<int,t>)==sizeof(int)*5)
mitigates any padding (and passes in every version of every major compiler that has supportedstd::array
). It does not mitigate against aliasing issues.
– Yakk - Adam Nevraumont
8 hours ago
|
show 4 more comments
Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5>
literally contains five int
s in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".
However, the std::array
object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.
An int
+----+
| |
+----+
A vector of 2 x int
+----+----+----+-----+ +----+----+
| housekeeping | ptr | | 1 | 2 |
+----+----+----+-----+ +----+----+
| ^
-----------
An std::array<int, 5>
+----+----+----+----+----+----------->
| 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+----+----+----------->
A vector of 2 x std::array<int, 5>
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| ^
-----------
And, even if it were, due to aliasing rules, whether you'd be able to use a single int*
to navigate all 10 numbers would potentially be a different matter!
All in all, a vector of ten int
s would be clearer, completely packed, and possibly safer to use.
In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).
Arrays do not have any indirection, but just store their data "directly". That is, a std::array<int, 5>
literally contains five int
s in a row, flat. And, like vectors, they do not put padding between their elements, so they're "internally contiguous".
However, the std::array
object itself may be larger than the set of its elements! It is permitted to have trailing "stuff" like padding. So, although likely, it is not necessarily true that your data will all be contiguous in the first case.
An int
+----+
| |
+----+
A vector of 2 x int
+----+----+----+-----+ +----+----+
| housekeeping | ptr | | 1 | 2 |
+----+----+----+-----+ +----+----+
| ^
-----------
An std::array<int, 5>
+----+----+----+----+----+----------->
| 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+----+----+----------->
A vector of 2 x std::array<int, 5>
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| housekeeping | ptr | | 1 | 2 | 3 | 4 | 5 | possible cruft/padding.... | 1 | 2 | 3 | 4 | 5 | possible cruft/padding....
+----+----+----+-----+ +----+----+----+----+----+----------------------------+----+----+----+----+----+----------->
| ^
-----------
And, even if it were, due to aliasing rules, whether you'd be able to use a single int*
to navigate all 10 numbers would potentially be a different matter!
All in all, a vector of ten int
s would be clearer, completely packed, and possibly safer to use.
In the case of a vector of vectors, a vector is really just a pointer plus some housekeeping, hence the indirection (as you say).
edited 16 hours ago
answered 18 hours ago
Lightness Races in OrbitLightness Races in Orbit
286k51466788
286k51466788
9
According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.
– Daniel Langr
18 hours ago
1
IOW, while the memory allocated has to be contiguous, the array elements need not be.
– MSalters
17 hours ago
1
Ooh this answer just gets posher and posher. Upped to 13.
– Bathsheba
16 hours ago
2
@DanielLangr That was a great discussion. Thanks for adding here...
– Const
14 hours ago
2
Notestatic_assert(sizeof(std::array<int,t>)==sizeof(int)*5)
mitigates any padding (and passes in every version of every major compiler that has supportedstd::array
). It does not mitigate against aliasing issues.
– Yakk - Adam Nevraumont
8 hours ago
|
show 4 more comments
9
According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.
– Daniel Langr
18 hours ago
1
IOW, while the memory allocated has to be contiguous, the array elements need not be.
– MSalters
17 hours ago
1
Ooh this answer just gets posher and posher. Upped to 13.
– Bathsheba
16 hours ago
2
@DanielLangr That was a great discussion. Thanks for adding here...
– Const
14 hours ago
2
Notestatic_assert(sizeof(std::array<int,t>)==sizeof(int)*5)
mitigates any padding (and passes in every version of every major compiler that has supportedstd::array
). It does not mitigate against aliasing issues.
– Yakk - Adam Nevraumont
8 hours ago
9
9
According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.
– Daniel Langr
18 hours ago
According to the answers here data don't have to be contiguous: Is the data in nested std::arrays guaranteed to be contiguous?. There are some discussion about this topic. Another discussions: Does std::array of std::array have contiguous memory? and Is the size of std::array defined by standard.
– Daniel Langr
18 hours ago
1
1
IOW, while the memory allocated has to be contiguous, the array elements need not be.
– MSalters
17 hours ago
IOW, while the memory allocated has to be contiguous, the array elements need not be.
– MSalters
17 hours ago
1
1
Ooh this answer just gets posher and posher. Upped to 13.
– Bathsheba
16 hours ago
Ooh this answer just gets posher and posher. Upped to 13.
– Bathsheba
16 hours ago
2
2
@DanielLangr That was a great discussion. Thanks for adding here...
– Const
14 hours ago
@DanielLangr That was a great discussion. Thanks for adding here...
– Const
14 hours ago
2
2
Note
static_assert(sizeof(std::array<int,t>)==sizeof(int)*5)
mitigates any padding (and passes in every version of every major compiler that has supported std::array
). It does not mitigate against aliasing issues.– Yakk - Adam Nevraumont
8 hours ago
Note
static_assert(sizeof(std::array<int,t>)==sizeof(int)*5)
mitigates any padding (and passes in every version of every major compiler that has supported std::array
). It does not mitigate against aliasing issues.– Yakk - Adam Nevraumont
8 hours ago
|
show 4 more comments
The big difference between std::vector
and std::array
is that std::vector
contains a pointer to the memory it wraps, while std::array
contains the actual array in itself.
That means a vector of vectors is like a jagged array.
For a vector of arrays, the std::array
objects will be placed contiguously but separate from the vector object. Note that the std::array
object themselves may be larger than the array they contain, and if so then the data will not be contiguous.
The last bit also means that an array (plain C-style or std::array
) of std::array
may also not keep the data contiguously. The std::array
objects in the array will be contiguous, but not the data.
The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.
4
It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.
– Daniel Langr
18 hours ago
2
@DanielLangr Thanks for reminding me. Rephrased that part.
– Some programmer dude
18 hours ago
add a comment |
The big difference between std::vector
and std::array
is that std::vector
contains a pointer to the memory it wraps, while std::array
contains the actual array in itself.
That means a vector of vectors is like a jagged array.
For a vector of arrays, the std::array
objects will be placed contiguously but separate from the vector object. Note that the std::array
object themselves may be larger than the array they contain, and if so then the data will not be contiguous.
The last bit also means that an array (plain C-style or std::array
) of std::array
may also not keep the data contiguously. The std::array
objects in the array will be contiguous, but not the data.
The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.
4
It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.
– Daniel Langr
18 hours ago
2
@DanielLangr Thanks for reminding me. Rephrased that part.
– Some programmer dude
18 hours ago
add a comment |
The big difference between std::vector
and std::array
is that std::vector
contains a pointer to the memory it wraps, while std::array
contains the actual array in itself.
That means a vector of vectors is like a jagged array.
For a vector of arrays, the std::array
objects will be placed contiguously but separate from the vector object. Note that the std::array
object themselves may be larger than the array they contain, and if so then the data will not be contiguous.
The last bit also means that an array (plain C-style or std::array
) of std::array
may also not keep the data contiguously. The std::array
objects in the array will be contiguous, but not the data.
The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.
The big difference between std::vector
and std::array
is that std::vector
contains a pointer to the memory it wraps, while std::array
contains the actual array in itself.
That means a vector of vectors is like a jagged array.
For a vector of arrays, the std::array
objects will be placed contiguously but separate from the vector object. Note that the std::array
object themselves may be larger than the array they contain, and if so then the data will not be contiguous.
The last bit also means that an array (plain C-style or std::array
) of std::array
may also not keep the data contiguously. The std::array
objects in the array will be contiguous, but not the data.
The only way to guarantee contiguous data for a "multi-dimensional" array is nested plain C-style arrays.
edited 18 hours ago
answered 18 hours ago
Some programmer dudeSome programmer dude
296k24250412
296k24250412
4
It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.
– Daniel Langr
18 hours ago
2
@DanielLangr Thanks for reminding me. Rephrased that part.
– Some programmer dude
18 hours ago
add a comment |
4
It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.
– Daniel Langr
18 hours ago
2
@DanielLangr Thanks for reminding me. Rephrased that part.
– Some programmer dude
18 hours ago
4
4
It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.
– Daniel Langr
18 hours ago
It also means that a vector of arrays is similar to an array of arrays, in that the data is all contiguous... I dare to disagree. Please, see my comment under LightnessRacesinOrbit's answer.
– Daniel Langr
18 hours ago
2
2
@DanielLangr Thanks for reminding me. Rephrased that part.
– Some programmer dude
18 hours ago
@DanielLangr Thanks for reminding me. Rephrased that part.
– Some programmer dude
18 hours ago
add a comment |
The C++ standard does not guarantee that std::array
doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.
Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.
The above also applies to a std::array<std::array>
.
add a comment |
The C++ standard does not guarantee that std::array
doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.
Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.
The above also applies to a std::array<std::array>
.
add a comment |
The C++ standard does not guarantee that std::array
doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.
Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.
The above also applies to a std::array<std::array>
.
The C++ standard does not guarantee that std::array
doesn't contain any payload at the end of the array, so alas you cannot assume that the first element of a subsequent array is just after the last element of a prior array.
Even if that were the case, the behaviour on attempting to reach any element in a array by pointer arithmetic on a pointer to an element in a different array is undefined. This is because pointer arithmetic is only valid within arrays.
The above also applies to a std::array<std::array>
.
answered 18 hours ago
BathshebaBathsheba
176k27251373
176k27251373
add a comment |
add a comment |
static_assert(sizeof(std::array<int,5>)==5*sizeof(int));
the above mitigates against having any padding on the end of a std::array
. No major compiler will cause the above to fail to this date, and I'd bet won't in the future.
If and only if the above fails, then std::vector<std::array<int,5>> v(2)
will have a "gap" between the std::array
s.
This doesn't help as much as you'd like; a pointer generated as follows:
int* ptr = &v[0][0];
only has a domain of validity up to ptr+5
, and dereferencing ptr+5
is undefined behavior.
This is due to aliasing rules; you aren't allowed to "walk" past the end of one object into another, even if you know it is there, unless you first round-trip to certain types (like char*
) where less restricted pointer arithmetic is permitted.
That rule, in turn, exists to allow compilers to reason about what data is being accessed through which pointer, without having to prove that arbitrary pointer arithmetic will let you reach outside objects.
So:
struct bob {
int x,y,z;
};
bob b {1,2,3};
int* py = &b.y;
no matter what you do with py
as an int*
, you cannot legally modify x
or z
with it.
*py = 77;
py[-1]=3;
std::cout << b.x;
the complier can optimize the std::cout
line to simply print 1
, because the py[-1]=3
may attempt to modify b.x
, but doing so through that means is undefined behavior.
The same kind of restrictions prevent you from going from the first array in your std::vector
to the second (ie, beyond ptr+4
).
Creating ptr+5
is legal, but only as a one-past-the-end pointer. Comparing ptr+5 == &v[1][0]
is also not specified in result, even though their binary values are absolutely going to be identical in every compiler on every major hardware system.
If you want to go futher down the rabbit hole, it isn't even possible to implement std::vector<int>
within C++ itself due to these restrictions on pointer aliasing. Last I checked (which was before c++17, but I didn't see a resolution in C++17) the standard committee was working on solving this, but I don't know the state of any such effort. (This is less of a problem than you might think, because nothing requires that std::vector<int>
be implemented in standard-compliant C++; it must simply have standard-defined behavior. It can use compiler-specific extensions internally.)
Nice answer; upped. Note also the somewhat related issue that you can’t write malloc in standard C.
– Bathsheba
6 hours ago
add a comment |
static_assert(sizeof(std::array<int,5>)==5*sizeof(int));
the above mitigates against having any padding on the end of a std::array
. No major compiler will cause the above to fail to this date, and I'd bet won't in the future.
If and only if the above fails, then std::vector<std::array<int,5>> v(2)
will have a "gap" between the std::array
s.
This doesn't help as much as you'd like; a pointer generated as follows:
int* ptr = &v[0][0];
only has a domain of validity up to ptr+5
, and dereferencing ptr+5
is undefined behavior.
This is due to aliasing rules; you aren't allowed to "walk" past the end of one object into another, even if you know it is there, unless you first round-trip to certain types (like char*
) where less restricted pointer arithmetic is permitted.
That rule, in turn, exists to allow compilers to reason about what data is being accessed through which pointer, without having to prove that arbitrary pointer arithmetic will let you reach outside objects.
So:
struct bob {
int x,y,z;
};
bob b {1,2,3};
int* py = &b.y;
no matter what you do with py
as an int*
, you cannot legally modify x
or z
with it.
*py = 77;
py[-1]=3;
std::cout << b.x;
the complier can optimize the std::cout
line to simply print 1
, because the py[-1]=3
may attempt to modify b.x
, but doing so through that means is undefined behavior.
The same kind of restrictions prevent you from going from the first array in your std::vector
to the second (ie, beyond ptr+4
).
Creating ptr+5
is legal, but only as a one-past-the-end pointer. Comparing ptr+5 == &v[1][0]
is also not specified in result, even though their binary values are absolutely going to be identical in every compiler on every major hardware system.
If you want to go futher down the rabbit hole, it isn't even possible to implement std::vector<int>
within C++ itself due to these restrictions on pointer aliasing. Last I checked (which was before c++17, but I didn't see a resolution in C++17) the standard committee was working on solving this, but I don't know the state of any such effort. (This is less of a problem than you might think, because nothing requires that std::vector<int>
be implemented in standard-compliant C++; it must simply have standard-defined behavior. It can use compiler-specific extensions internally.)
Nice answer; upped. Note also the somewhat related issue that you can’t write malloc in standard C.
– Bathsheba
6 hours ago
add a comment |
static_assert(sizeof(std::array<int,5>)==5*sizeof(int));
the above mitigates against having any padding on the end of a std::array
. No major compiler will cause the above to fail to this date, and I'd bet won't in the future.
If and only if the above fails, then std::vector<std::array<int,5>> v(2)
will have a "gap" between the std::array
s.
This doesn't help as much as you'd like; a pointer generated as follows:
int* ptr = &v[0][0];
only has a domain of validity up to ptr+5
, and dereferencing ptr+5
is undefined behavior.
This is due to aliasing rules; you aren't allowed to "walk" past the end of one object into another, even if you know it is there, unless you first round-trip to certain types (like char*
) where less restricted pointer arithmetic is permitted.
That rule, in turn, exists to allow compilers to reason about what data is being accessed through which pointer, without having to prove that arbitrary pointer arithmetic will let you reach outside objects.
So:
struct bob {
int x,y,z;
};
bob b {1,2,3};
int* py = &b.y;
no matter what you do with py
as an int*
, you cannot legally modify x
or z
with it.
*py = 77;
py[-1]=3;
std::cout << b.x;
the complier can optimize the std::cout
line to simply print 1
, because the py[-1]=3
may attempt to modify b.x
, but doing so through that means is undefined behavior.
The same kind of restrictions prevent you from going from the first array in your std::vector
to the second (ie, beyond ptr+4
).
Creating ptr+5
is legal, but only as a one-past-the-end pointer. Comparing ptr+5 == &v[1][0]
is also not specified in result, even though their binary values are absolutely going to be identical in every compiler on every major hardware system.
If you want to go futher down the rabbit hole, it isn't even possible to implement std::vector<int>
within C++ itself due to these restrictions on pointer aliasing. Last I checked (which was before c++17, but I didn't see a resolution in C++17) the standard committee was working on solving this, but I don't know the state of any such effort. (This is less of a problem than you might think, because nothing requires that std::vector<int>
be implemented in standard-compliant C++; it must simply have standard-defined behavior. It can use compiler-specific extensions internally.)
static_assert(sizeof(std::array<int,5>)==5*sizeof(int));
the above mitigates against having any padding on the end of a std::array
. No major compiler will cause the above to fail to this date, and I'd bet won't in the future.
If and only if the above fails, then std::vector<std::array<int,5>> v(2)
will have a "gap" between the std::array
s.
This doesn't help as much as you'd like; a pointer generated as follows:
int* ptr = &v[0][0];
only has a domain of validity up to ptr+5
, and dereferencing ptr+5
is undefined behavior.
This is due to aliasing rules; you aren't allowed to "walk" past the end of one object into another, even if you know it is there, unless you first round-trip to certain types (like char*
) where less restricted pointer arithmetic is permitted.
That rule, in turn, exists to allow compilers to reason about what data is being accessed through which pointer, without having to prove that arbitrary pointer arithmetic will let you reach outside objects.
So:
struct bob {
int x,y,z;
};
bob b {1,2,3};
int* py = &b.y;
no matter what you do with py
as an int*
, you cannot legally modify x
or z
with it.
*py = 77;
py[-1]=3;
std::cout << b.x;
the complier can optimize the std::cout
line to simply print 1
, because the py[-1]=3
may attempt to modify b.x
, but doing so through that means is undefined behavior.
The same kind of restrictions prevent you from going from the first array in your std::vector
to the second (ie, beyond ptr+4
).
Creating ptr+5
is legal, but only as a one-past-the-end pointer. Comparing ptr+5 == &v[1][0]
is also not specified in result, even though their binary values are absolutely going to be identical in every compiler on every major hardware system.
If you want to go futher down the rabbit hole, it isn't even possible to implement std::vector<int>
within C++ itself due to these restrictions on pointer aliasing. Last I checked (which was before c++17, but I didn't see a resolution in C++17) the standard committee was working on solving this, but I don't know the state of any such effort. (This is less of a problem than you might think, because nothing requires that std::vector<int>
be implemented in standard-compliant C++; it must simply have standard-defined behavior. It can use compiler-specific extensions internally.)
answered 8 hours ago
Yakk - Adam NevraumontYakk - Adam Nevraumont
183k19190375
183k19190375
Nice answer; upped. Note also the somewhat related issue that you can’t write malloc in standard C.
– Bathsheba
6 hours ago
add a comment |
Nice answer; upped. Note also the somewhat related issue that you can’t write malloc in standard C.
– Bathsheba
6 hours ago
Nice answer; upped. Note also the somewhat related issue that you can’t write malloc in standard C.
– Bathsheba
6 hours ago
Nice answer; upped. Note also the somewhat related issue that you can’t write malloc in standard C.
– Bathsheba
6 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54197195%2fwhat-is-the-memory-layout-of-vector-of-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Given the answers, if you want this, use
std::vector<int> vec(5*2)
and do 2D indexing yourself inside the flat 1D array. Maybe write a wrapper class for 2D indexing on top of a flat container, with either a templated or runtime-variable row length. You'd also want to expose a flat view so algorithms that just need to do something to every element without caring about 2D position can do that with one big loop, more efficiently.– Peter Cordes
5 hours ago