3 ms·
"Matlab and Octave have multidimensional arrays built in. Few modern languages do. C, C++, Python, Go, Rust - no built-in multidimensional arrays. (Arrays of ar
by vram22 10y ago
"Matlab and Octave have multidimensional arrays built in. Few modern languages do. C, C++, Python, Go, Rust - no built-in multidimensional arrays. (Arrays of arrays are not the same thing.)"
What is the difference? I was thinking about this just recently (in connection with Python and C). But not clear on the diff (for either language), though I know that internally all arrays have to be represented as 1D sequences of bytes in memory, and the translation betwen 1D and say 2D (for 2D arrays) is done by adding and multiplying indices, offsets, etc. Is there any more to it than that? Interested to know.
- Animats 10y agoThere are performance, and in some languages, safety issues with arrays of arrays. Arrays of arrays can be ragged - not all the rows need be the same length. Languages which do subscript checking have to deal with this, and usually can't hoist subscript checks out of inner loops. Allocating an array of arrays can require a lot of allocations, as does copying such an array. This gets expensive if you have some language where arrays are first-class result types. You can't just do one allocate, then copy the entire array. The C idiom "a[i][j]" has become pervasive, replacing actual multidimensional array subscripts such as "a[i,j]" (Pascal, Modula) or "a(i,j)" (Octave, MATLAB and FORTRAN). Amusingly, you can't overload "operator[]" in C++ with more than one argument. It's a syntax problem. The comma operator (the "evaluate but ignore first operand, return second operand" operator) has higher precedence inside "a[]" than inside "a()". This is a legacy from C's use of the comma operator inside macros.