4 ms·
That's not a 2D array, it's a jagged array. You're paying for an extra pointer dereference.
by hello_hi 13y ago
That's not a 2D array, it's a jagged array. You're paying for an extra pointer dereference.
- Peaker 13y agoYou'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.
- norswap 13y agoAnd I'll also add that this is not an optimization, it's guaranteed by the standard.
- mpweiher 13y agoWhere 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]
- Peaker 13y agoYour edit is correct, thanks for the correction!