Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>typedef is your friend</strong></p> <p>After going back and looking at many of the other answers I found that a deeper explanation is in order, as many of the other answers either suffer from performance problems or force you to use unusual or burdensome syntax to declare the array, or access the array elements ( or all the above ).</p> <p>First off, this answer assumes you know the dimensions of the array at compile time. If you do, then this is the best solution as it will both give <strong>the best performance</strong> and allows you to use <strong>standard array syntax to access the array elements</strong>. </p> <p>The reason this gives the best performance is because it allocates all of the arrays as a contiguous block of memory meaning that you are likely to have less page misses and better spacial locality. Allocating in a loop may cause the individual arrays to end up scattered on multiple non-contiguous pages through the virtual memory space as the allocation loop could be interrupted ( possibly multiple times ) by other threads or processes, or simply due to the discretion of the allocator filling in small, empty memory blocks it happens to have available. </p> <p>The other benefits are a simple declaration syntax and standard array access syntax.</p> <p>In C++ using new:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(int argc, char **argv) { typedef double (array5k_t)[5000]; array5k_t *array5k = new array5k_t[5000]; array5k[4999][4999] = 10; printf("array5k[4999][4999] == %f\n", array5k[4999][4999]); return 0; } </code></pre> <p>Or C style using calloc:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(int argc, char **argv) { typedef double (*array5k_t)[5000]; array5k_t array5k = calloc(5000, sizeof(double)*5000); array5k[4999][4999] = 10; printf("array5k[4999][4999] == %f\n", array5k[4999][4999]); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload