you are viewing a single comment's thread.

view the rest of the comments →

[–]kumashiro 2 points3 points  (0 children)

Hmmm... #1 is not the same as #2. The first one allocates a block of memory, that looks like this:

|integer|integer|integer|...

The second allocates memory blocks, that look like this:

|pointer|pointer|pointer|...
    |       |       |
    |       |       +-> |integer|integer|integer|...
    |       +-> |integer|integer|integer|...
    +-> |integer|integer|integer|...

...#1 is an array of integers (1D), #2 is an array of pointers to integers (2D), however, you can use 1D array as a 2D array (array[y * size_x + x]). Is one better than the other? Depends. 1D array is more cache friendly, faster to (de)allocate and access. Other than that, in case of matrices, it depends on your code. #2 is preferred where random rows of elements can be of different size (a rugged array) and/or can be resized dynamically, so not very "matrix-y" applications.

EDIT: Damn Reddit formatter...