I know this answer is in violation of the reinterpret_cast
rules but it also presumes that sub-arrays will be allocated linearly.
I believed this was not guaranteed, but as I search the standard, I find my confidence wavering. If I statically allocate a 2D array, like this:
int foo[][4] = { { 5, 7, 8 },
{ 6, 6 },
{},
{ 5, 6, 8, 9 } };
Am I allowed to assume that all elements will be allocated linearly? That is to say that if foo[0]
is at address 0x00000042, will:
foo[1]
be at address 0x00000052foo[2]
be at address 0x00000062foo[3]
be at address 0x00000072
These addresses are in hex, and yes they are providing space for the 4-element sub-array with sizeof(int) == 4
; they may and may not be zero-initialized.