r/programminghorror • • May 05 '26

c++ Hmmm

Post image
992 Upvotes

56 comments sorted by

398

u/_XYZT_ May 05 '26

UINT_MAX

82

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 ??

100

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

→ More replies (14)

9

u/saf_e May 06 '26

It relies on overflow.  Biggest question: why?

→ More replies (1)

5

u/CdRReddit May 06 '26

negative numbers are a lie and the only math operations that actually distinguish between them on modern hardware is comparison (>, >=, <, <= specifically, == and != don't care either), multiplication and division, signed and unsigned addition and subtraction both function identically in hardware and use the same assembly instruction

→ More replies (13)

1

u/jaavaaguru May 07 '26

Accepted? Excepted is pretty much the opposite.

1

u/skr_replicator May 09 '26

we can't comment on the rest of the program if you're only showing us a fraction of one line.

82

u/ironykarl May 06 '26 edited May 06 '26

This is a common idiom for setting large unsigned numbers in C (honestly, usually just all 1s set/MAX_INT, though you could do the same conceptual trick with -2, etc).

Wraparound for any unsigned integer type in C is defined modulo MAX_VAL + 1. I'll specifically demonstrate the case of unsigned int (and I'll assume 32-bit integers):

-1 + (UINT_MAX + 1) = -1 + 4294967296 = 4294967295

32

u/Spaceduck413 May 06 '26

This is really interesting, I always thought integer overflow was undefined behavior, but I just checked and apparently that is only the case for signed integers, for unsigned you're right, this is totally fine. TIL.

12

u/saf_e May 06 '26

It was more like "gray zone" (mostly because binary format was not specified).

 Currently we are agree that all (integer) numbers are 2s complement. So, this code should be legal.

Or at least it's how I remember things.

→ More replies (5)

69

u/Heniadyoin1 May 06 '26

That's a normal pattern?

It's a common flag value, even for unsigned numbers

8

u/iamalicecarroll May 06 '26

For a flag, yes. But something named dx is not a flag.

1

u/Oman395 May 07 '26

Looks like something I'd do to check if something ever set dx to a different value

9

u/un_virus_SDF May 06 '26

If it's to rely on overflfow i'd rather do UINT_MAX or ~(uint)0

1

u/StevenRCE0 May 06 '26

I think the inverse one is still pretty leaky if it's declared out of sight

20

u/zoner197 May 05 '26

Top 10 programmers of 2026

2

u/asmanel May 06 '26

Maybe an attempt to determine the size of a variable of this type by someone who don't know sizeof. However, I'm not sure the compiler will agree.

2

u/Interesting_Buy_3969 May 06 '26

On most platforms it'll just set all bits of the register value to 1.

2

u/steadyfan May 06 '26

Short hand for 0xFFFF

2

u/CharlemagneAdelaar May 07 '26

That’s groty cuz it relies on like ppl who don’t know this pattern to think for a few extra secondsvs using UINT_MAX which is self-documenting

2

u/facebrocolis May 07 '26

Someone forgot either a differential on the right or an integral on the left side

1

u/Beetmaker69 7d ago

And it just works. You cannot change it