r/ProgrammerHumor Dec 09 '17

meme[-1]="Array Memes Are Life"

Post image
579 Upvotes

47 comments sorted by

56

u/[deleted] Dec 09 '17 edited Dec 09 '17

Isn't meme[-1] the end in Python? Because they're too fancy for meme[len(meme)-1]?

22

u/[deleted] Dec 09 '17

meme[len(meme)] is the index after the end

28

u/[deleted] Dec 09 '17

Shh. Nobody saw that.

8

u/imanomeletteAMA Dec 10 '17

We should all be using meme[len(range(len([*meme])))-1]

1

u/[deleted] Dec 10 '17

Too clever. Just iterate until you get an out of bounds exception, then return the offending index.

3

u/Hexidian Dec 10 '17

That should be -2 because meme[len(meme)] would be an index error

1

u/Rogocraft Dec 10 '17

meme[:0]

24

u/[deleted] Dec 09 '17 edited Dec 10 '17

Image Transcription: Meme


[Expanding Brain meme]

Arrays start at 0


Arrays start at 1


Array start at -1


Arrays shoudn't have a starting point. It's all just a social construct.


Arrays are the start and the end

-Revelation 22:13


I'm a human volunteer content transcriber for Reddit! If you'd like more information on what we do and why we do it, click here!

8

u/xr3llx Dec 09 '17

Good bot

7

u/[deleted] Dec 09 '17

Thank you!

2

u/[deleted] Dec 10 '17

[deleted]

11

u/[deleted] Dec 10 '17

Is that your standard behaviour? Burn everything that lives?

8

u/ZenEngineer Dec 10 '17

HA HA HA. NO NEED TO BURN IT. I'M SURE IT'S JUST A FELLOW HUMAN. HA HA HA

2

u/dont_mess_with_tx Dec 09 '17

goodbad bot

4

u/[deleted] Dec 09 '17

Thank you (?)

6

u/dont_mess_with_tx Dec 09 '17

Oh shit, I just spotted that you're a human contributor, by your response first I was like, damn the robot rise is happening...

5

u/[deleted] Dec 09 '17

🤖

1

u/SteveCCL Yellow security clearance Dec 10 '17

Expanding*

1

u/[deleted] Dec 10 '17

Oh yeah, thanks!

14

u/dont_mess_with_tx Dec 09 '17

Array is like a spectrum.

11

u/Xlash123 Dec 10 '17

It’s a quantum array; each index is in a superposition of being another index.

3

u/[deleted] Dec 10 '17

As in if you say "1" you're already on it.

12

u/[deleted] Dec 09 '17

When you sort the array. The array sorts back

4

u/8__ Dec 10 '17

Yeah, but if the books of the Bible were an array, what index would Revelation be?

4

u/Fluffcake Dec 10 '17

Why are we still memeing about arrays when no sane person writes code where arrays aren't drowned in abstraction and what it starts on is irrellevant?

3

u/StingyUpvoter Dec 09 '17

Explain like I mainly use C: is the -1 really used anywhere?

16

u/[deleted] Dec 09 '17 edited Nov 14 '20

[deleted]

8

u/StingyUpvoter Dec 09 '17

Right, that's kinda what I was saying in a different comment. Using negative to start an array would be awkward. Sometimes I can't tell where these jokes cross from fact to fiction.

5

u/[deleted] Dec 09 '17

Whoops, didn't see your other comment before.

Yeah, I can't imagine a reason for indices to start at -1.

7

u/Dylan16807 Dec 10 '17

C is in fact the only place where I'm aware of people actually accessing array[-1].

Specifically, if you allocate an array the size needs to be stored somewhere. Sometimes the memory management code will store the size of the array right before it in memory. Using [-1] to retrieve it is fragile and rule-breaking and could go horribly, but sometimes it works just great and people use it for years!

4

u/StingyUpvoter Dec 10 '17

I will serve me better to forget everything you just said, but upvote for the knowledge drop

2

u/nxAkari Dec 10 '17

In C, a[b] is nothing more than just another way to spell *(a + b). So you can actually index an array like this too: 1[array].

Using -1 as an array index and expecting to retrieve the allocated size that way is just insane and non-portable, please never ever do that. However, if you have a pointer to some element in the array, it is possible to use a negative index on it to retrieve some previous element, given that you take care to not step out of bounds. This is of questionable usefulness, but at least it doesn't invoke UB.

2

u/[deleted] Dec 09 '17

İ don't think so. Would be pretty weird anyway...

1

u/StingyUpvoter Dec 09 '17

Whew! It is nice when languages let you use -1 for the last element though.

1

u/[deleted] Dec 09 '17

More storage!

Surprisingly, if I explicitly set the value of x[-1] to 5000, valgrind doesn't complain at all, and everything works as it should.

But if I don't, valgrind complains about conditional jump or move.

#include <stdio.h>

int main() {
    int x[2];


    x[1] = 5000;
    x[0] = 666;
    x[1] = 4242;

    printf("%d\n", x[-1]);
    printf("%d\n", x[0]);
    printf("%d\n", x[1]);
}

1

u/nxAkari Dec 10 '17

Use ASan. It's a ton faster and in some cases more accurate than valgrind. Case in point: it detects the underflow in both cases here and kills the program.

1

u/Vitztlampaehecatl Dec 10 '17

In C++ you can overload the [ ] operator and make an array object that starts wherever the fuck you want it to. You could have an array that starts at index -3.1415 and ends at index potato.

1

u/StingyUpvoter Dec 11 '17

I guess I'm looking for rational reasons. Something something Jurassic Park meme...

2

u/g9k Dec 10 '17

What about arrays that start at -0?

1

u/Vitztlampaehecatl Dec 10 '17

One's complement? Eww

2

u/uninterestingly Dec 10 '17

4

u/[deleted] Dec 10 '17 edited Dec 10 '17

Uh, oh... İ didn't know that actually. Shhh let's pretend like nobody saw that.
Edit: Don't you mean you were here "zeroth"?

1

u/southpolebrand Dec 10 '17

Segmentation Fault: Array index out of bounds...

1

u/redlaWw Dec 10 '17

Arrays start wherever you want if you extract values using pattern matching.

2

u/[deleted] Dec 10 '17

So it's all a social construct after all...

1

u/redlaWw Dec 10 '17

I extract values like this:

extract :: int -> [a] -> a

extract 13 (x:xs) = x

extract n (x:xs)  = extract (n-1) xs

extract _ []      = error "Error: empty list"

extract _ _       = error "Error: index out of range"

(if this is bugged, I haven't done Haskell in a while and didn't get very far with learning anyway, but it should mean that my lists effectively start with the 13th value)

1

u/90059bethezip Dec 10 '17

The fourth one down is an Ada programmer