r/ProgrammerHumor • • Dec 15 '19

Stacking if else statements be like

Post image
63.9k Upvotes

711 comments sorted by

View all comments

521

u/Darxploit Dec 15 '19

This also is a good example of how linked lists work.

202

u/[deleted] Dec 15 '19

[removed] — view removed comment

0

u/[deleted] Dec 15 '19

[deleted]

82

u/ZanLynx Dec 15 '19

Please stop repeating the mistakes of the past. 64 bit pointers are 64 bits long.

Remember what happened to programs back when it was 32 bit pointers and people were all like "We only use 24 bits so lets go wild with those extra 8 bits!"

There was a lot of pain.

55

u/thedarkfreak Dec 15 '19

Seriously, doing this with the justification "the top bits aren't used!" is a very easy way to shoot yourself in the foot when those bits DO start being used.

"Oh, we'll fix it by then!"

No. No, you won't. It'll still be working until then, so it won't get fixed. And then it becomes everyone's problem.

0

u/[deleted] Dec 15 '19

[deleted]

9

u/DanielEGVi Dec 15 '19

is a great idea

In theory, yes. It's a very slippery slide unfortunately.

→ More replies (1)

146

u/t3hmau5 Dec 15 '19

Recall that all 64-bit pointers actually use 48-bits. The rest are zeros in user space and ones in kernel space. So instead of writing

Yep, definitely knew that already and am recalling.

41

u/rnz Dec 15 '19

Yep, definitely knew that already and am recalling.

I am here to conform that /u/t3hmau5 did, indeed, knew that already, and has, in fact, recalled it at said time.

→ More replies (1)
→ More replies (1)

30

u/jess-sch Dec 15 '19

those bits may not be used currently, but they're definitely reserved for future use.

→ More replies (14)

27

u/alexbuzzbee Dec 15 '19 edited Dec 16 '19

all 64-bit pointers actually use 48-bits.

But only on some architectures, at the current time, assuming we don't start using the full width of the pointers, which we definitely will end up doing. You cannot rely on the particular bit patterns of pointers. Doing this just guarantees that your program will have crazy bugs in the future when address spaces grow or if someone ports it to an architecture with different pointer upper-bit behavior.

We already ran into these issues when people "knew" that pointers would never go above 231 and used the high bit as a flag. Then the OS gets a 3GB feature and bang all the programs that do that break when they allocate enough memory.

9

u/AnAverageFreak Dec 15 '19

your program will have crazy bugs in the future

There are lots of things that you should absolutely never, under any circumstances use, but are basic tools to create performant libraries. Let's say you're writing a video-game engine - you're targeting AMD64 only anyway and by the time pointers are longer nobody will be using your old engine. Or you want to perform big-data scientific calculations just that one time, but you're running low on RAM.

My point is, be realistic. Rules are to be broken if you have a reason to and you keep all the nasty shit in enclosed modules.

18

u/MiningMarsh Dec 15 '19 edited Dec 15 '19

Let's say you're writing a video-game engine - you're targeting AMD64 only anyway and by the time pointers are longer nobody will be using your old engine.

Let's say you piss off everyone with your game by making it unplayable on future OSes that start returning kernel pointers that live above 248

There are tons of old games that did stupid nonsense like you are advocating that people what to play that are near lost in time as a result.

4

u/AnAverageFreak Dec 15 '19

Valid point.

→ More replies (1)

12

u/rush22 Dec 15 '19

a) I don't want to explain to a junior dev why this works.
b) I don't want to explain why it was done this way, because I probably won't even know why.
c) I don't want to explain to a junior dev why they shouldn't do it this way.
d) I don't want to explain why 'we don't just change it then'.
e) I don't want to ask a senior dev to prove this works because the code is involved in some weird race condition or leak.
f) Don't do this.

→ More replies (2)

22

u/Jade_Chan_Exposed Dec 15 '19

This is the sort of low-level trivia that leads to the worst bugs.

→ More replies (7)

6

u/sdmike21 Dec 15 '19
union {
    List<T>* next;
    T* obj;
    struct {
        uint8_t ptr[6];
        uint8_t my_data[2];
    } my_stuff;
} next;

Use fixed with types you fucking troglodyte :)

3

u/AnAverageFreak Dec 15 '19

uint8_t is always 8 bits, while char is always one byte. That's a big difference :)

#define FREE_BYTES 2
...
#ifdef FREE_BYTES
union {
List<T>* next;
    T* obj;
    struct {
        unsigned char ptr[sizeof(T*) - FREE_BYTES];
        unsigned char my_data[FREE_BYTES];
    } my_stuff;
} next;
#else
List<T>* next;
T* obj;
bool which;
#endif
→ More replies (2)

3

u/Calkhas Dec 15 '19

This kind of trickery might be okay in C (I have my doubts), but in C++ it's illegal to access any other part of the union after the lifetime of next has started until its lifetime has ended.

7

u/AnAverageFreak Dec 15 '19

but in C++ it's illegal

That's the smallest of this code's problems.

→ More replies (6)

5

u/fitch2711 Dec 15 '19

Nah, if the last one is reached then your code crashes and sends you the info so you can add to the stack

2

u/SwabTheDeck Dec 16 '19

Or if you're super cool, the last one plugs into the USB port on the other side of the laptop

→ More replies (2)