Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

12
  • 66
    It's a little heavier weight than it needs to be, and it allocates more blocks than you need. Multidimensional arrays only need one block of memory, they don't need one block per row. Allocating only one block makes cleanup simpler too. Commented Jun 1, 2009 at 21:35
  • 14
    @Kevin: Allocating just a single contiguous block is the way to go (less impact on allocator, better locality, etc). But you don't have to sacrifice clean subscripting. See stackoverflow.com/a/29375830/103167
    – Ben Voigt
    Commented Mar 31, 2015 at 19:43
  • 11
    Shouldn't it be i*sizeX+j? If I recall correctly, with row major ordering it should be row*numColumns+col.
    – arao6
    Commented Oct 4, 2015 at 20:29
  • 2
    hm, nice thinking, indeed it's only a matter of representation - the rest is perspective. clever Commented Nov 2, 2016 at 0:33
  • 2
    @Borna: In general using a single 2D array is going to be faster than an array of arrays. Following two pointers can cause pipeline stalls. As always, it depends on access patterns. Commented May 3, 2017 at 22:13