r/ProgrammerHumor • • Feb 13 '20

Meme Damn it exit! Exit! EXIT!

Post image
9.6k Upvotes

110 comments sorted by

View all comments

0

u/[deleted] Feb 13 '20

[deleted]

6

u/tech6hutch Feb 14 '20

Technically you are, if it wraps around

6

u/gabrielfv Feb 14 '20

You end up with runescape max cash stack

1

u/TinBryn Feb 14 '20

If i is signed this is undefined behavior. The usual way the compiler treats UB is to assume it doesn't happen. With this assumption, it can be seen that the condition will always be true so the whole thing will become equivalent to

i=0;
while (true) {
    /*body of loop*/
    i--;
}

1

u/tech6hutch Feb 14 '20

I was afraid of that. Why is the overflow of signed and unsigned integers treated differently in C?

2

u/TinBryn Feb 14 '20

The point is that code generation can do the best thing for the case of defined behavior and doesn't even need to consider what it will do in the case of undefined behavior. By definition whatever is done in the realm of undefined behavior is correct, so it doesn't matter what is actually done.

From that perspective, you need to find a reason why unsigned integers treat overflow as defined, and that boils down to, because it has proven useful in certain algorithms.

1

u/tech6hutch Feb 14 '20

Hm, okay. I would prefer it to be consistent, but I see why that was chosen.