News for hackers. Stuff that matters.
Re:Single assignment C by vanderZwan on Sunday December 22, 2013 @07:50AM (#6950156)
Because normal C is also row-major?
Re:Single assignment C by mbq on Sunday December 22, 2013 @08:50AM (#6950279)
C only has vectors (1D arrays), the implementation of higher dimensions is up to programmer. I know there is this idea to use vectors of vectors to emulate matrices but it is not at all a part of the language (nor efficient).
Re:Single assignment C by beagle3 on Sunday December 22, 2013 @09:28AM (#6950375)
So what do you call x in the definition

    int x[100][83];
Sure looks like a 2D array of ints for all intents and purposes.
Re:Single assignment C by StefanKarpinski on Sunday December 22, 2013 @11:51AM (#6950767)
Everyone always trots this out when talking about multidimensional array programming in C, but it's effectively useless because it only works for statically declared, fixed-size arrays. Since those never occur when writing real numerical code, the fact that the compiler in this one very particular case can turn x[i][j] into x[i*s+j] for me is essentially useless for any real work with dynamically allocated, variably sized multidimensional arrays.
Re:Single assignment C by mikeash on Sunday December 22, 2013 @12:01PM (#6950784)
In which case the correct statement would have been something like "Multidimensional arrays in C are not very useful in practice," rather than "they don't exist".
Re:Single assignment C by mbq on Sunday December 22, 2013 @03:30PM (#6951613)
Vector of vectors, as I wrote; quoting cdecl "declare x as array 100 of array 83 of int".
Re:Single assignment C by hello_hi on Sunday December 22, 2013 @10:08AM (#6950467)
That's not a 2D array, it's a jagged array. You're paying for an extra pointer dereference.
Re:Single assignment C by Peaker on Sunday December 22, 2013 @10:15AM (#6950482)
You're wrong. Operationally, it is equivalent to:

  int x_1d[8300];
Where x[a][b] is x_1d[a + 83*b];

An array of arrays is contiguous in memory. There are no pointers involved until you take the (r)value of the array or one of the inner arrays, at which point it is degraded to be a pointer to the array's first element, as always.

Re:Single assignment C by mpweiher on Sunday December 22, 2013 @11:29AM (#6950705)
Where hello_hi might have gotten confused is that in usage, a pointer of array pointers looks the same as a 2D array. So if you only see x[a][b] it could actually be dereferencing an array of pointers to integers.

Since we have the declaration, it's unambiguously the 2D array that you described.

EDIT: I might be seeing things, but I think you have your order mixed up, x[a][b] is not x_1d[a + 83b], but rather x_1d[a83+b]

Re:Single assignment C by Peaker on Sunday December 22, 2013 @08:54PM (#6952667)
Your edit is correct, thanks for the correction!
Re:Single assignment C by norswap on Sunday December 22, 2013 @10:34AM (#6950522)
And I'll also add that this is not an optimization, it's guaranteed by the standard.
Re:Single assignment C by vanderZwan on Sunday December 22, 2013 @01:48PM (#6951185)
Whether it's row-major or "row-major" is not the point, what matters that it is the ordering C programmers will be most familiar with.