3 ms·
> An array of length 0 starts at 0 and ends with 0 Nope. The moment you "start" somewhere you occupy 1 unit of memory. Thus no longer an "array of length 0".
by foobarbaz33 4y ago
> An array of length 0 starts at 0 and ends with 0
Nope. The moment you "start" somewhere you occupy 1 unit of memory. Thus no longer an "array of length 0".
There is no such thing as an array of length 0. It absolutely does not exist. You cannot write source code to represent it.
ie assuming we are talking about the pure data structure of an array. Some languages may have some abstraction built on of arrays (ie c-strings) that can have length 0 but these are not "arrays" and if they are defined they still occupy 1 unit of memory for the terminator.
- topaz0 4y agoThe programming languages that I use most often can all represent empty arrays just fine. Of course, indexing into them is an error. But defining them is commonplace, and not being able to define them would make a lot of my code a lot more complicated.
- iopq 4y agoAllowed in Rust: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=17c4e09593574c720e4e00f255de5594 https://play.rust-lang.org/?version=stable&mode=debug&editio... it's an array of length 0, but it can't be "used" because it has a length of 0 so it occupies no RAM
- foobarbaz33 4y ago> An array of length 0 starts at 0 and ends with 0 But such an array does not "start" at 0 as it does not exist. You need an array of at least length-1 to "start" at 0.
- iopq 4y agoI can write a loop like this: `for(int i=0; i < 0; ++i)` and it will run 0 times, the exact amount of times I expected
- foobarbaz33 4y agoBut again your not starting at "index 0" of an array that exists. This loop is not processing any physically existing array. In fact the compiler would remove this block of code as it is a no-op.
- steveklabnik 4y agoC++ requires every value to take at least one byte of memory, but this isn’t true of all languages. Additionally, C++20 adds [[no_unique_address]], which allows zero-sizes types. iopq points out Rust as a language that does allow zero length arrays in a sibling comment. While ISO C does not allow zero length arrays, GNU C does. There’s also flexible array members, which can be length 0.
- foobarbaz33 4y agoI'd say this transcends language. Sure a language can define some abstraction that says "hey I'm length-0 array!". But this is more like a statement of "hey I don't exist yet, but if i do exist in the future I'll be of type array and at least length-1". You can't access index 0 of a length-0 array. You can only access index 0 of length-1+ array.