C/C++ vs. Fortran: Memory Layout Origins
C/C++ inherited row-major order from its predecessor B, while Fortran's column-major choice dates back to early IBM machines and efficiency in mathematical operations.
The top 3
- Fortran's Column-Major: Aligned with Mathematical Notation: Fortran, designed for scientific computation, adopted column-major order because it aligns with standard linear algebra conventions where matrices are often viewed as collections of column vectors. This made operations like matrix multiplication more intuitive and efficient for its target domain.
- C/C++'s Row-Major: Driven by Array Syntax & Reading Order: C and C++ adopted row-major order largely due to their array declaration syntax (e.g., `array[rows][cols]`) where the last index varies fastest. This naturally stores elements of a row contiguously in memory, aligning with the common left-to-right, top-to-bottom reading order.
- Performance Impact: Cache Locality & Ecosystem Divide: The choice of memory layout significantly impacts performance due to cache locality; accessing data in the order it's stored is faster. This led to a divide: math-heavy languages like Fortran, MATLAB, and Julia, along with core linear algebra libraries (BLAS, LAPACK), predominantly use column-major, while general-purpose languages like C/C++, Java, and Python's default NumPy arrays use row-major.
Sources
Open the full topic