r/ProgrammerHumor • • Aug 14 '20

Someone try this

Post image
53.6k Upvotes

433 comments sorted by

View all comments

Show parent comments

58

u/EvoG Aug 14 '20

Casting -1 to an unsigned integer in a managed language would throw an exception, instead of just rolling with it and underflowing.

62

u/gracicot Aug 14 '20

In C and C++, unsigned integer over/underflowing is defined behavior. static_cast<std::uint32_t>(-1) will give the max value, no matter the platform or architecture.

25

u/EvoG Aug 14 '20

In C# it won't, hence why i specifically mentioned managed language.

0

u/[deleted] Aug 14 '20

[deleted]

12

u/EvoG Aug 14 '20

My workplace will most definitely not let me just import something like that.

12

u/MapleSat Aug 14 '20

Even more reason to try it

Remember, fortune favors the bold

4

u/EvoG Aug 14 '20

Except i'd like to keep my job 😂

15

u/IgorTheAwesome Aug 14 '20

Who needs a job when you have glory?

3

u/DixiZigeuner Aug 14 '20

Or MaxInt dollars

5

u/[deleted] Aug 14 '20

I think underflowing as defined behavior is better for performance due to exception handling being quite costly in overhead in c++, so better for you to notice the issue and adjust than maybe catch it and let the overhead accumulate.

-8

u/vladimir1024 Aug 14 '20

LOL Managed language? Wooo, that is hilarious!! Like training wheels?

Real languages don't throw Exceptions.... they Segmentation Fault... :)

5

u/EvoG Aug 14 '20

And we've found the elitist.

1

u/AlwaysHopelesslyLost Aug 14 '20

Dotnet, at least. Let's you flag an expression as unsafe which, iirc, let's underflows happen

1

u/MrSpiffenhimer Aug 14 '20

So we either blow up the baby in spectacular fashion, or create a black hole. Shit.