2

The suggested duplicates do not directly answer this particular question. One of them just addresses the contiguity of the elements of vector without talking about array while the other discusses the use of array instead of float[N] without talking about contiguitiness (is that a word?).

Does a vector<std::array<float,10>> of size()=5 result in 50 contiguous floats in memory, such that a pointer to the vector[0] element points to the start of this stretch of memory?

John Dvorak
  • 26,799
  • 13
  • 69
  • 83
johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • 2
    well, there would be no way to fill that vector with anything, so that questions point is kind of moot. – PlasmaHH May 14 '13 at 10:32
  • 1
    It's not a duplicate - the other question is about scalars being placed in contiguous regions; the OP asks about a vector of arrays. – Sergey Kalinichenko May 14 '13 at 10:34
  • @dasblinkenlight [The second duplicate](http://stackoverflow.com/questions/4612273/correct-way-to-work-with-vector-of-arrays) addresses the problem of putting arrays in vectors. One should use `std::array` in this case. The floats are continuous, which is discussed in [the first duplicate](http://stackoverflow.com/questions/849168/are-stdvector-elements-guaranteed-to-be-contiguous). – leemes May 14 '13 at 10:35
  • @leemes That's a much better duplicate, thanks for finding it! – Sergey Kalinichenko May 14 '13 at 10:36
  • @dasblinkenlight He didn't find it, I did :) – trojanfoe May 14 '13 at 10:38
  • 1
    @dasblinkenlight Well, the first duplicate discusses if the memory is continuous, which is the question. The problem that a vector of arrays is not really possible is another question ;) But of course, the answer that it is continuous only helps if one already has chosen the correct array type, namely `std::array` over raw arrays. – leemes May 14 '13 at 10:38
  • 1
    @trojanfoe True. So I `std::forward("thanks")`. ;) – leemes May 14 '13 at 10:39
  • @leemes I have edited the question, which still applies and doesn't seem to be answered in the "duplicates" – johnbakers May 14 '13 at 10:42
  • 1
    @Fellowshee: It is answered there. Just combine them. 1) your construct is not possible 2) vectors memory is contiguous. – PlasmaHH May 14 '13 at 10:46
  • @PlasmaHH. Wrong. I can make an inference that my assumption is correct, but since a vector of another container is not guaranteed to be contiguous, and none of those answers addresses my exact vector, then my question is valid, though I agree it is similar. – johnbakers May 14 '13 at 10:50
  • @Fellowshee: I would be intrested to see a full compiling code sample using your construct. – PlasmaHH May 14 '13 at 10:54
  • "[The suggested duplicates do not directly answer this particular question.]" -- please tell us why they don't – John Dvorak May 14 '13 at 11:02
  • @JanDvorak: Because one of them just addresses the contiguity of the elements of `vector` without talking about `array` while the other discusses the use of `array` instead of `float[N]` without talking about _contiguitiness_ (is that a word?). – rodrigo May 14 '13 at 11:18
  • @rodrigo thanks, and I believe the word is _contiguity_ maybe? cheers – johnbakers May 14 '13 at 11:21
  • 1
    So the actual question to be answered in addition to the supposed duplicates is, *is a `std::array` guaranteed to store 10 contiguous `float`s and only those (without any additional padding or alignment issues) and thus layout compatible to a `float[10]`?*, which *rodrigo* seems to answer -> +1 for reopen. – Christian Rau May 14 '13 at 12:00

1 Answers1

4

Yes. At least if you could create one.

Arrays are not copyable in C++ so you will have a hard time trying to insert them in the array.

UPDATE: Yes, vector<array<float, N>> has the contiguous memory because vector has that requirement and array is layout compatible with native arrays, so no padding or anything that may be in the middle.

Note that vector<vector<float>> will not have contiguous memory, because each vector has additional fields.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • 2
    "because each vector has additional fields" Actually, this is not the reason. The reason is because the vector's actual data is stored somewhere else (indirectly), so their contents can be spread over the whole memory, not only separated by their "overhead". Vectors are more or less a pointer to their data + some extra information (I guess size + allocated size). – leemes May 14 '13 at 12:18