On Sat, 10 Apr 1999, Dan Jue wrote:
> > The above creates a matrix of [4][4]; that is, each dimension varies from
> > [0] to [3]. a[0][3] is the last element of the first row; a[0][4] is
> > infact the first element of the second row which is better represented by
> > a[1][0].
>
> Are we allowed to assume that row-major order arrays are totally
> contiguous in memory for any platform (by your example of address
> a[0][4])? For arrays of structures or objects (or some other big unit),
> is it not possible for their locations in memory to be non-contiguous?
You are right; you cannot just assume the whole array is allocated in one
contiguous location. I was merely trying to explain what was happening
in the code originally posted. In that code only a few bytes of memory
were assigned, as such one can pretty much be sure that they are in one
contiguous location. Of course there is no guarantee....
>
> Also i'm assuming that you cannot access a[0][4] directly because wouldn't
> that cause an out-of-bound subscript error? So you would instead do some
> manual pointer arithmetic to get that address, right?
I thought one of the reasons for speed of C (compare to other languages)
was because it wasn't checking for boundaries. After all, this is the
reason for all those "buffer overflow" bugs that have surfaced recently.
Please correct me if I am wrong.
cheers,
Hossein