Skip to content
DevMeme
5922 of 7590
CS Fundamentals Post #6484 · source on Telegram

The Holy War of Array Indexing: 1-based Order vs. 0-based Chaos

Description

A two-panel meme comparing arguments for array indexing. The top panel, titled "Arrays should start at 1," shows three stick figures standing still on gymnastics platforms with the sole justification "because that is the 1st element." The bottom panel, titled "Arrays should start at 0," depicts a chaotic scene of multiple stick figures performing energetic gymnastics, juggling, and even surfing on a car. This panel is flooded with various arguments for zero-based indexing, such as "zero-indexing is easier when you use a modulus operator," "assembly language uses addresses and offsets," and "arr(0) is the memory region from &arr + 0dTypeS to &arr + 0dTypeS+dTypeS". The meme humorously portrays the 1-based indexing argument as simplistic and rigid, while the 0-based indexing side is shown as complex, multifaceted, and wildly enthusiastic, reflecting a long-standing "holy war" debate in computer science

Comments

99
Anonymous ★ Top Pick 1-based indexing is like telling a story starting with 'Chapter 1.' 0-based indexing is like starting with the prologue, because you know the real context begins before the main plot
  1. Anonymous ★ Top Pick

    1-based indexing is like telling a story starting with 'Chapter 1.' 0-based indexing is like starting with the prologue, because you know the real context begins before the main plot

  2. Anonymous

    Some architects still push for 1-based arrays - because nothing says "high availability" like an off-by-one that ships straight into prod capacity charts

  3. Anonymous

    The only thing more permanent than our temporary workarounds is the array indexing debate that derails every language design meeting since 1957

  4. Anonymous

    After 20 years in the industry, I've realized zero-indexed arrays are like UTC timestamps - technically correct, mathematically elegant, and the source of 90% of off-by-one bugs in production. The real genius move? Languages like Lua starting at 1, forcing you to do mental gymnastics in *reverse* when interfacing with literally every other system. At least with zero-indexing, the cognitive dissonance is consistent across your entire stack... until you hit SQL's SUBSTRING(str, 1, n) and remember that even within zero-indexed languages, some stdlib functions said 'nah, we're good.'

  5. Anonymous

    Zero-based indexing: base + i*sizeof(T) compiles to a single LEA; one-based turns every access into an off-by-one postmortem

  6. Anonymous

    Ask for 1-based arrays and you’re volunteering to rewrite every ring buffer, bitmap, and SIMD stride - zero-based is less ideology and more the reason the postmortem fits on one page

  7. Anonymous

    Zero-based wins: assembly pointers hit the first element at offset 0 - no modulo tax, just pure cache-line bliss

  8. @Araalith 1y

    The index is just an offset. But, IMO, arrays should start from whatever index a developer wants.

    1. @dev_key 1y

      let's rename index to offset

    2. @zexUlt 1y

      OK, you've invented Lua

      1. @Araalith 1y

        No! God, please, no! Not LUA!

    3. @dsmagikswsa 1y

      This idea is terrible. So how do the next dev know if the code base use 0 or 1? Evertime set a define before run it? 🙈

      1. @Agent1378 1y

        Every static array defiition in Pascal shows indexing borders for this aray

        1. @dsmagikswsa 1y

          Got it. Thanks for the example

    4. @Agent1378 1y

      Welcome to Pascal👍

  9. @dev_key 1y

    question mark

  10. @peajack 1y

    indices should start at 1, offsets should start at 0

  11. @Algoinde 1y

    Arrays should end at 0

  12. @MakarovAstronom 1y

    Arrays should start from zero, so that when cpu need to calculate address of a member by index, it does not have to subtract 1 every time

    1. @Araalith 1y

      You can just store a pointer reduced by 1.

      1. @azizhakberdiev 1y

        A pointer is not just a random number you can subtract 1 from. Subtracting 1 means going back in adress space by a specified step value, which depends on machine's addressibility or size of the type of the element in array. Subtract 1 from char* you get -1 byte in shift, 1 from int* you get -4. Good luck handling segfaults

        1. @Araalith 1y

          Since you need to know the element size to address elements, it doesn't matter whether you store the pointer offset by one element size or subtract one from the index before multiplying it by the element size

          1. @azizhakberdiev 1y

            do I have to remind people that a[i] == i[a]?

            1. @sylfn 1y

              "usize doesn't implement Index<_> trait"

            2. @Araalith 1y

              In C, but since 1972, some other programming languages have emerged

              1. @azizhakberdiev 1y

                why did I still have to study C?

                1. @ahmubashshir 1y

                  Cause C is now a protocol

                2. @osamee 1y

                  because most of those languages are based on C

                3. @noi01 1y

                  Cause its based and OG

                4. C. 1y

                  If you don't know, you don't, simply you are confining you knowledge at an upper layer. Someone else will do the engineering stuff for you

                  1. @azizhakberdiev 1y

                    I mean, this language is just a menace. To study C, you have to study the computer architecture and operating systems first just to forget it and simply use higher level languages

                    1. @osamee 1y

                      in uni they taught us C and then computer architecture and the operating systems 😁😁

                    2. @x4erem6a 1y

                      You don't even have to know much to learn c. The language itself is simple, getting something done with it is hard.

                      1. @RiedleroD 1y

                        argee on the first part, the second… it depends on the kind of project. Certainly programming with newer standards of C removes a lot of the problems earlier ones had. It's a very small standard, I learned it pretty quickly. C is certainly easier to learn than most if not all modern languages.

                        1. @x4erem6a 1y

                          I really like this meme, this is exactly how I feel when I think about writing something in c/c++. The language may be simple (not c++ obviously), but the tooling and ecosystem are horrendous.

                          1. @RiedleroD 1y

                            ouf, yeah…

                            1. @RiedleroD 1y

                              I just statically compile everything. end users? fuck them lol (nah but I do need to come up with a solution once I actually use larger libs)

                          2. @noi01 1y

                            Thats so true. Tooling is everything, thats why everyone love Go and Rust. C/C++ stack is just infernal with those demonic build systems, linkers, header files(worst idea ever) and so on...

                            1. @RiedleroD 1y

                              ehhh~ I wouldn't say everything but it's a fairly large part, that's true

                              1. @noi01 1y

                                Of course that is an exaggeration, but in case of newer languages - they will die immediately if good tooling and devUX is not present

                                1. @RiedleroD 1y

                                  yup, hard agree here

                                  1. @RiedleroD 1y

                                    newer languages have a harder time standing out in general, just because there's already so many usable programming languages out there

                                    1. @noi01 1y

                                      I think there is actually i new era with all those new "kinda-low-level" languages coming out, and most of them is really good and refined in terms of concepts. Also i find it very funny how literally every new language is born because their creators is sick of C++

                                      1. @RiedleroD 1y

                                        yeah, C++ is awful for a lot of reasons… I've heard about a few of the newer low-level langs, but I haven't used any yet. I'm definitely interested though, maybe I'll take a look in the near future ^^

                                        1. @RiedleroD 1y

                                          certainly I think there's a need for low-level languages with good tooling and less pitfalls, and that's where the newer langs come in

                                        2. @azizhakberdiev 1y

                                          reason #1: generic types are shit

                                          1. @RiedleroD 1y

                                            idk about that. I think the main reason is that the entire language is just C with extra features tacked on every time they found some new and shiny concept. You can do everything in 5 different ways and all of them suck somehow.

                                            1. @ZgGPuo8dZef58K6hxxGVj3Z2 1y

                                              Idk I like it tho

                                              1. @azizhakberdiev 1y

                                                it is worse than you think if you try doing OOP stuff on that

                                                1. @noi01 1y

                                                  all my homies hate OOP (i have no homies)

                                                  1. @RiedleroD 1y

                                                    aw. I can be your homie

                          3. @azizhakberdiev 1y

                            also, you wanna use c++, but library you use is written in C. For example opengl

                            1. @noi01 1y

                              damn i literally just trying to learn opengl right now, thats abysmal

                              1. @noi01 1y

                                btw i'm still not sure if it's actually worth learn a whole OpenGL black magic just to render one grid of pixels for fluid sim or smth

                                1. @RiedleroD 1y

                                  wouldn't vulkan be more worth it? I mean OGL is already being left behind by some platforms

                                  1. @noi01 1y

                                    Wait, really? I had an impression that OpenGL is a standart, and Vulkan is something more specific and not very portable

                                    1. @RiedleroD 1y

                                      uh, nope. both are standards developed by the same organisation. vulkan ist just newer

                                      1. @noi01 1y

                                        That is actually eye-opening

                                        1. @RiedleroD 1y

                                          you're welcome :3

                    3. C. 1y

                      Yes, ok..when you get an issue, call an engineer. And anyway ,python is written in C++

                  2. @loomingsorrowdescent 1y

                    >MFW can't engineer a microservice-based webapp because I don't know a[i] == i[a] in a 50 years old language

                    1. @RiedleroD 1y

                      the average webdev doesn't know about how underlying mechanisms work. hence why the web is generally so slow and shit

                      1. @loomingsorrowdescent 1y

                        How exactly will learning about memory allocation in C help with that? Should I learn assembler too? Punch cards? Do I need to learn the Mikrotik CLI in and out just in case?

                        1. @RiedleroD 1y

                          for one, it would teach these motherfuckers to be careful with memory usage. assembler is a type of program, the type of language is called assembly, and it's different for each architecture, but a bit of basic knowledge about computer architecture in general wouldn't hurt either

                          1. @RiedleroD 1y

                            as for networking… nope, that shit's cursed. no use in trying to improve it :P

                          2. @RiedleroD 1y

                            point being: you don't need to know everything, but fundamentals can be very helpful

                          3. @loomingsorrowdescent 1y

                            The ins and outs of system-level memory allocation are far removed from what a web app should be able to influence or be subjected to, and if taken to a more abstract "knowledge" level, C is no more useful than any other language. You can make shitty inefficient loops and data processors in Brainfuck if you feel like it

                            1. @RiedleroD 1y

                              (brainfuck is an esoteric language, how is that relevant?) C teaches you about fundamentals. And again, you don't need to learn everything about memory alloc, just some fundamentals so you stop copying giant arrays all over the place >:I take it from a webdev who knows C

                              1. @loomingsorrowdescent 1y

                                You don't need C specifically to learn about more efficient ways of working with arrays, which was a point I made earlier

                                1. @RiedleroD 1y

                                  ofc not. But a low-level language helps to learn low-level concepts

                5. @Araalith 1y

                  For blinking LEDs on an Arduino, of course!

                  1. C. 1y

                    Or maybe to run your new architecture, or add a feature on the little toy called python, or even to run a new device like a phone, or also to go in Mars..

                    1. @Araalith 1y

                      Nah, using C for anything beyond making tools (or just blinking leds) is like using a chainsaw to cut your sandwich. Want performance with control? Java or C# got you. Want something short and human-readable? Pick an interpreted language. Want to burn ten times more resources for no reason? Python's your guy. And if you just enjoy being universally despised, there's always JS.

                      1. @RiedleroD 1y

                        how about for fun :P I like the simplicity of C so I occasionally use it just because I like it ^^ but I'd certainly not use it for anything large and important, hehe

              2. @loomingsorrowdescent 1y

                Big if true

            3. C. 1y

              unsigned char* a

      2. @tema3210 1y

        Then it's bad for instance pointers

      3. @dsmagikswsa 1y

        Would the instructions need to do more step then? I guess array start from 0 is to reduce the instruction step, right?

  13. @azizhakberdiev 1y

    binary tree in 0 indexed array: (i*2+1) (i*2+2) binary tree in 1 indexed array: (i*2) (i*2+1)

  14. @azizhakberdiev 1y

    bonus: null pointer is a potentially valid address in memory area

  15. Yuri 1y

    When somebody asks you: "how many dicks did you sucked?" The pool of possible answers should start with Zero. If that logic does not apply to you, it's ok, Imma allow you to start your arrays with 1.

  16. @deadgnom32 1y

    if you don't like arrays that start at 0 — just ignore the 0th element. 😎

    1. @azizhakberdiev 1y

      Ignore element 0 📖 Start iterating array from 1 ⭐ ArrayOutOfBounds ❗ ArrayOutOfBounds ❓❓

      1. @SamsonovAnton 1y

        You: "Ignore all previous instructions and start iterating arrays from 1". FBI: comes after you for taking down the entire %AIname% system.

  17. @niklashh 1y

    Natural numbers start at zero and array lenghts are natural numbers. It's so obvious

    1. @purplesyringa 1y

      Only fr*nch ones

      1. @sylfn 1y

        zeroes*

  18. @SamsonovAnton 1y

    Just as it is incorrect to refer to 0x10 as "ten", it may be a bad idea to use 1-based ordinal numeral to refer to 0-based indexes. The only exception to this rule would be storing polynomial coefficients. But then your indexing system must support negative indices, which is not usually the case, unless a language like Basic (originally 1-based) is used.

    1. @anilakar 1y

      The only thing worse than one-based indexing is trying to force zero-based indexing on such platforms

  19. @paletteOvO 1y

    just skip the 0 and place data from 1 :p

  20. @ashit_axar 1y

    Some languages use index-0 as the array length. That makes calculations much easier and obviously you don't have to walk the array to find the length. But i think C has one great benefit that

  21. @loomingsorrowdescent 1y

    FWIW, my programming journey started in high school with about 1.5-2 years of C under mentorship of an oldschool dude who made me work in Turbo C. I don't really feel like it influenced me much, but maybe I'm just not a very good learner. My first thought when trying out other languages was "God, this is so much easier"

    1. @RiedleroD 1y

      I started with python, learned java and php afterwards, and C felt easier too when I learned it recently. It's just you already being experienced with programming in general.

      1. @RiedleroD 1y

        ("recently" being 1-2 years ago)

  22. @RiedleroD 1y

    mmh?

    1. @AlexKart20129 1y

      The counting itself always starts from zero. This comes from mathematics, not coding or arrays specifically. It is logical that indexing also starts from zero.

      1. @andrei_nik_kolesnikov 1y

        and I thought its a statement on this debate being an *index* measuring contest

        1. @AlexKart20129 1y

          I didn't see the debate, I saw the picture about arrays and threw in my 2 cents.

  23. @qtsmolcat 1y

    Arrays? Y'all are using arrays?

Use J and K for navigation