Is a 3D array continuous in memory, and as for 2d?

If I declare a 2d c-style array

int data[X][Y]

I assume that the compiler will create this as a single array, similar to

int data[X*Y] , but is it guaranteed?

Let's say for simplicity we use standard compilers for the x86 architecture. Now about

int data[X][Y][Z] ?

Does the compiler create this as a contiguous block of memory and just do some offsets?

I usually use a single vector for a 2d array with a line offset of * NumCols + col and have a built-in function to calculate it for me, but I was interested in a 3D array for this question. I must also ask if anyone did this with a single vector, and what will be the bias logic.

+7
c ++ c arrays
source share
3 answers

Yes, multidimensional arrays of any order in C are adjacent. These are simply “arrays of arrays,” so to speak. Even more in the comp.lang.c FAQ section, section 6, Arrays and Pointers .

+8
source share

The resulting arrays will be contiguous in the virtual memory allocated to your process. Arrays may not be contiguous in physical memory, but that doesn't matter to you.

+6
source share

Array elements are guaranteed to be contiguous, so in both cases the layout is the same.

+2
source share

All Articles