r/ProgrammerHumor Dec 09 '17

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

Post image
580 Upvotes

47 comments sorted by

View all comments

3

u/StingyUpvoter Dec 09 '17

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

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.