• Something Burger 🍔@jlai.lu
    link
    fedilink
    English
    arrow-up
    14
    ·
    29 days ago

    Starting at 0 makes sense in low-level languages like C because it’s not really an index but a memory offset. Higher level languages like SQL or MatLab correctly start at 1 because they abstract memory management away. Other languages without manual memory management, such as JavaScript or Python, are incorrectly starting their arrays at 0.

    • azi@mander.xyz
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      28 days ago

      It’s worth noting that a number of languages comparable C in use case and performance (including its predecessors COBOL, Fortran and ALGOL) start indexing at 1 just fine because they have proper array types and don’t make heavy use of pointers poking and peeking wherever they like (eg by using references instead).

      Decoupling indices from memory offsets doesn’t get in the way of performance and actually often allows better optimization because the compiler knows you aren’t sharing pointers between arrays or some other shenanigans; see Fortran, the GOAT of fast array processing. This also allows improved type safety and thus memory safety; see index types in Ada/SPARK and the fact that it’s the only ‘legacy’ language that’s gotten comprehensive compile-time memory safety analysis (though the latter is moreso from the less willy-nilly pointers in general)