0

I wonder whether an n-dimensional array is guaranteed to be contiguous in memory or if it can have "holes" due to alignment and padding. I'm interested especially in structures that do not align to pointer sizes.

I am aware that semantically an uint8_t array[5][7]; is not identical to a uint8_t array[35], because the former is 2-dimensional and the latter is 1-dimensional (linear).

I also know that for Bitmaps pixel graphics (2D) one has to consider the stride, but that might be related to compression algorithms, not alignment requirements.

I found a similar question for C, but I'm asking for C++.

Does the standard guarantee that all elements of an n-dimensional array are contiguous? I'm looking for a quote from the C++ standard (draft versions welcome).

Jason
  • 36,170
  • 5
  • 26
  • 60
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • Built in arrays are inherited from `C` so i expect this property of arrays to be the same in `C++`. Just like many other properties of built in arrays(like *array to pointer decay*) are same. – Jason Oct 14 '22 at 07:46
  • 1
    Well. by using some logic, an `array[x]` has to be contiguous. Since `x` itself is an array, that has to be contiguous. Thus the conclusion must be that an array of array[x] must also be contiguous. – PaulMcKenzie Oct 14 '22 at 07:47
  • *"I'm looking for a quote from the C++ standard... "* **And, did you try to find it yourself?** The `C` question already explains the reasoning. You just have to put together the pieces explained there and see if they hold in `C++`. – Jason Oct 14 '22 at 07:50
  • Related/dupe: [May I treat a 2D array as a contiguous 1D array?](https://stackoverflow.com/questions/7269099/may-i-treat-a-2d-array-as-a-contiguous-1d-array) – Jason Oct 14 '22 at 07:57
  • Even single-dimension arrays are not contiguous when item type is overaligned. For example if type occupies 8 bytes and aligned to 16 then there will be 8 bytes of padding after each item. – user7860670 Oct 14 '22 at 07:59
  • @user7860670 How can you overalign a type without increasing its `sizeof`? – HolyBlackCat Oct 14 '22 at 08:06
  • 1
    @JasonLiam: good related question with interesting answers a) from high-rep users but answers have negative score b) upvoted answers contradicting each other. – Thomas Weller Oct 14 '22 at 08:19
  • @HolyBlackCat I think padding will be always included in sizeof result – user7860670 Oct 14 '22 at 08:23
  • @ThomasWeller Yes agreed. Though i've downvoted your question because i didn't see any effort from your side to look up into the standard and atleast try it for yourself first given that a `C` question already explains the basic reasoning and also there is a related question that i linked. I think any answer posted here can be posted in the dupe as well so that we can close this one as a dupe or vice-versa perhaps. – Jason Oct 14 '22 at 08:23
  • @ThomasWeller The accepted answer from 2011, there, starts with *"It's up to interpretation."*. It looks likes thing haven't changed meantime. – Bob__ Oct 14 '22 at 08:24
  • @user7860670 Then the elements wil bel contiguous. Unless you consider the trailing padding to somehow not be a part of the object. – HolyBlackCat Oct 14 '22 at 08:27
  • @JasonLiam: your downvote is probably fine. I have indeed not made enough efforts reading the standard. Partially because I'm not good at reading ^H^H^H understanding it, but that's a lame excuse ... – Thomas Weller Oct 14 '22 at 08:27
  • @HolyBlackCat contiguous == no padding of any kind – user7860670 Oct 14 '22 at 09:05
  • @user7860670 As I see it, only padding between the elements counts. If the padding is inside the element, it does not. Also there's http://eel.is/c++draft/dcl.array#6 – HolyBlackCat Oct 14 '22 at 09:56

1 Answers1

4

From the Current Draft Standard [dcl.array #6]

"An object of type “array of N U” consists of a contiguously allocated non-empty set of N subobjects of type U, known as the elements of the array, and numbered 0 to N-1."

In addition sizeof includes any padding bytes [expr.sizeof #2]

"When applied to a class, the result is the number of bytes in an object of that class including any padding required for placing objects of that type in an array."

Richard Critten
  • 2,138
  • 3
  • 13
  • 16