<p>This came up from <a href="https://stackoverflow.com/questions/55093/how-to-deal-with-arrays-declared-on-the-stack-in-c#55183">this answer to a previous question of mine</a>.
Is it guaranteed for the compiler to treat <code>array[4][4]</code> the same as <code>array[16]</code>?</p>

<p>For instance, would either of the below calls to <code>api_func()</code> be safe?</p>

<pre><code>void api_func(const double matrix[4][4]);

// ...

{
  typedef double Matrix[4][4];

  double* array1 = new double[16];
  double array2[16];

  // ...

  api_func(reinterpret_cast&lt;Matrix&amp;&gt;(array1));
  api_func(reinterpret_cast&lt;Matrix&amp;&gt;(array2));
}
</code></pre>

[{'answer_id': 55539, 'author': 'Josh Matthews', 'author_id': 3830, 'author_profile': 'https://Stackoverflow.com/users/3830', 'pm_score': 0, 'selected': False, 'text': '<p>I would be worried about padding being added for things like Matrix[5][5] to make each row word aligned, but that could be simply my own superstition.</p>\n'}, {'answer_id': 55579, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 2, 'selected': False, 'text': "<p>I don't think there is a problem with padding introduced by having a <strong>multi-dimensional</strong> array.</p>\n\n<p>Each element in an array must satisfy the padding requirements imposed by the architecture.   An array [N][M] is always going to have the same in memory representation as one of [M*N].</p>\n"}, {'answer_id': 55580, 'author': 'Henk', 'author_id': 4613, 'author_profile': 'https://Stackoverflow.com/users/4613', 'pm_score': 1, 'selected': False, 'text': '<p>Each array element should be laid out sequentially in memory by the compiler. The two declarations whilst different types are the same underlying memory structure.</p>\n'}, {'answer_id': 55660, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 3, 'selected': True, 'text': "<p>From the C++ standard, referring to the <code>sizeof</code> operator:</p>\n\n<blockquote>\n  <p>When applied to an array, the result is the total number of bytes in the array. This implies that the size of an array of <code>n</code> elements is <code>n</code> times the size of an element.</p>\n</blockquote>\n\n<p>From this, I'd say that <code>double[4][4]</code> and <code>double[16]</code> would have to have the same underlying representation.  </p>\n\n<p>I.e., given </p>\n\n<pre><code>sizeof(double[4]) = 4*sizeof(double)\n</code></pre>\n\n<p>and</p>\n\n<pre><code>sizeof(double[4][4]) = 4*sizeof(double[4])\n</code></pre>\n\n<p>then we have</p>\n\n<pre><code>sizeof(double[4][4]) = 4*4*sizeof(double) = 16*sizeof(double) = sizeof(double[16])\n</code></pre>\n\n<p>I think a standards-compliant compiler would have to implement these the same, and I think that this isn't something that a compiler would accidentally break.  The standard way of implementing multi-dimensional arrays works as expected.  Breaking the standard would require extra work, for likely no benefit.</p>\n\n<p>The C++ standard also states that an array consists of contiguously-allocated elements, which eliminates the possibility of doing anything strange using pointers and padding.</p>\n"}, {'answer_id': 55937, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 0, 'selected': False, 'text': '<p>A bigger question is: do you really need to perform such a cast?</p>\n\n<p>Although you <em>might</em> be able to get away with it, it would still be more readable and maintainable to avoid altogether.  For example, you could consistently use double[m*n] as the actual type, and then work with a class that wraps this type, and perhaps overloads the [] operator for ease of use.  In that case, you might also need an intermediate class to encapsulate a single row -- so that code like my_matrix[3][5] still works as expected.</p>\n'}, {'answer_id': 57934, 'author': 'aib', 'author_id': 1088, 'author_profile': 'https://Stackoverflow.com/users/1088', 'pm_score': 1, 'selected': False, 'text': "<p>@Konrad Rudolph:</p>\n\n<p>I get those two (row major/column major) mixed up myself, but I do know this: It's well-defined.</p>\n\n<p>int x[3][5], for example, is an array of size 3, whose elements are int arrays of size 5. (§6.5.2.1) Adding all the rules from the standard about arrays, addressing, etc. you get that the second subscript references consecutive integers, wheras the first subscript will reference consecutive 5-int objects. (So 3 is the bigger number; you have 5 ints between <strong>x[1]</strong>[0] and <strong>x[2]</strong>[0].)</p>\n"}]
['https://Stackoverflow.com/questions/55532', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4790/']