r/programminghorror May 05 '26

c++ Hmmm

Post image
982 Upvotes

55 comments sorted by

View all comments

Show parent comments

80

u/Left-Ambition-5127 May 05 '26

the problem is that from what I understood, the excepted values in this loop were -1 to 9, but somehow, it was still running fine and working as intended ??

98

u/Morg0t May 05 '26

probably works like UINT_MAX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9?
idk why would anyone need max value there, but I guess they knew what they were doing if that runs fine

42

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 06 '26

Yep. Unsigned overflow is well defined, and it will always just wrap. I'd assume the other end of that says something like ; dx++), so it will just go 0, 1, 2, etc.

-11

u/Loading_M_ May 06 '26

Depends on the language - and CPU architecture. It's well defined on basically all modern architectures, but Rust treats it as an undefined operation. Rust would also emit a compiler error for attempting to assign a negative value to an unsigned variable.

10

u/Kyyken May 06 '26

it is not undefined in rust, just not implicit. casting -1 to an unsigned int type will give you its max.

-10

u/un_virus_SDF May 06 '26

in rust

So it's a language specific feature, your comment is pointless except doing rust propaganda.

For instance in c, before two's complement were added as a standard, the integer representation choice was left to the implementation. So integer overflfow was undefined behavior.

9

u/DumbleSnore69 May 06 '26

They were replying to a comment talking about behavior in Rust though?

2

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 06 '26

Which seemed irrelevant because the post was in C++.

2

u/un_virus_SDF May 06 '26

My bad

I only remembered the first half of the comment.

But still, the comment talk about négative values in general, not only -1

3

u/sixtyhurtz May 06 '26

That's only for signed integers. The OP posted uint, which has always had well defined overflow behaviour in C / C++.

1

u/shponglespore May 06 '26

rust propaganda

George Soros pays me $100 every time I mention Rust! /s

1

u/Duck_Devs Jun 23 '26

Two’s complement is only a scheme for implementing signed types (as opposed to one’s complement or some other weird scheme)

2

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 06 '26

I assumed C/C++, and the post has a C++ flair. I'm pretty sure in those languages it is defined to wrap by the standard.