Simple array init question

Dan murpsoft at hotmail.com
Mon Apr 9 09:44:04 PDT 2007


Graham Wrote:
> (And to be pedantic, int[5][5] is a 2D rectangular array, which in 
> memory is exactly the same as int[25] - a 1D array, of course - so six 
> of one and half a dozen of the other perhaps, but int[5][5] does have > 
> 1 dimension :-))

Actually, you're wrong sir.  An int[5][5] produces an array containing 5 structs, each of:

struct Array {
  size_t(or was it int?  uint?) length;
  void* ptr;
}

Each of those arrays then points to another array of 5 elements.

What I believe you want is a rectangular array:

int[5,5], which will produce a single Array struct that you can access via taking a multiple of the first index added to the second index (or is it vice versa?)

This notation tends to typically be inferior to int[25] for that very reason, each access requires math on two indices.  I might also note that it is typically more efficient to use a power of two for array sizes for indexing purposes - so long as the array size doesn't cause the OS you're working on to have difficulty allocating the space.

Some OS's allocate in 4kb units and then take 16 bytes for metadata, some allocate anything > 2kb in a "giant data" page-set which may be more inefficient than the smaller page-sets as it may allocate your data accross pages.



More information about the Digitalmars-d mailing list