3 ms·
Why do you think that foo[1][2][3] requires more than one memory reference?
by pklausler 2y ago
Why do you think that foo[1][2][3] requires more than one memory reference?
- nine_k 2y agoAFAIK in regular C a T[][][] foo would be a the same as T ** foo, so it gets implemented as a pointer to an array of pointers to arrays of pointers, each pointing to contiguous allocations of multiple Ts, each not necessarily near the other. So you need three dereferences to get to an element. T value = foo[1][2][3]; // becomes: foo -> (T **) (T **) -> (T *) (T **) (T *) ... (T *) -> (T) (T *) (T) ... (T) (T) <-- This one! This allows for jagged arrays, yay! So useful. This is in stark contrast to a Fortran-style array, which is allocated as one contiguous piece, all dimensions folded up for linear access with one dereference.
- deleted 2y ago[deleted]
- ynik 2y agoYou are mistaken there. An array `int arr[10][20][30];` is a single contiguous (stack) allocation. I recommend you read up on what pointer decay actually does; it's more complicated than replacing all arrays with pointers! In particular, the type of an `arr` expression (after decay) is `int(*)[20][30]`. Decay only ever changes the top-level (outermost) type! And the type of `&arr` is even `int(*)[10][20][30]` -- using `&` or `sizeof` prevents decay from happening. Pointers to arrays are rarely used because using decay is more idiomatic (and because their syntax is unwieldy), but they still exist and would be safer than using decay.
- pklausler 2y agoNope.