Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>AFAICT, your screenshot of the array having 20 elements is because.. you told Netbeans (with that <code>@20</code>) to display 20 elements starting at a memory location (<code>**(matrix)</code>). What happens when you change it to be '@3'?</p> <p>However, based on this comment:</p> <blockquote> <p>Actually it's more than the double. I know because when i try to assign values they're not contiguous in memory but they are shifted (i can see it from the watches in the debug window in netbeans).</p> </blockquote> <p>When you allocate memory, you're calling a library that figures out how to request from the operating system what space it may use and then of what space it's been given by the operating system what it hasn't used yet.</p> <p>You're making assumptions about how the allocator works that may or may not be correct. It's possible for it to allocate space efficiently in a way you don't expect, especially due to virtual paging mechanisms. In short, contiguous calls to the allocator are not guarenteed to allocate contiguous memory, and rarely will.</p> <p>If you want to use an array-of-pointers for a double array style structure and absolutely must have contiguous addresses in a more "expected" way, you can try this:</p> <pre><code>int **allocateMatrix(int rows, int columns) { int i = 0; int **p = NULL; int *d = NULL; p = (int**) calloc(rows, sizeof(int*)); d = (int*) calloc(rows * columns, sizeof(int)); for(; i &lt; rows; i++) { p[i] = d + i * columns; } return p; } </code></pre> <p>(code not tested)</p>
 

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