2

Is memory allocated for multidimensional arrays in C or C++ always contiguous, or is the storage dependent on the compiler? If it is guaranteed to be contiguous is there a standard on it somewhere for reference? For example

int x[2][2] = { { 1 , 2 } , { 5 , 10 } } ;

Are the integers 1, 2, 5, 10 in sequence in memory ?

1
  • 1
    The "multidimensional array" is really a single-dimension array whose elements are themselves arrays. An array is guaranteed to have no padding and to have its elements in order of their index. Commented Apr 17, 2015 at 3:33

2 Answers 2

3

Arrays are guaranteed contiguous. What we have here is an array of arrays - each layer of which is contiguous. The inner most arrays we know must be {1, 2} and {5, 10}, and the outer most array must also be contiguous. Therefore, {{1,2},{5,10}} must be 1, 2, 5, 10 sequentially in memory.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes. Arrays are always allocated in contiguous memory location. It doesn't matter whether its a single or multi dimensional array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.