Hacker News new | past | comments | ask | show | jobs | submit login

C standards fault. GCC has intrinsics that let you do arithmetic safely with overflow checks

Just use them

https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins...




Those look rather inconvenient. Is there a sane way to keep things infix at least without having to crack open a spew of operator overloading?

Something like #pragma come_on_be_reasonable ?

(This is rhetorical. They point is it shouldn't be every coders personal responsibility to make the tool not be openly hostile. You may take pride in personal mastery of an unreasonable thing but that doesn't make it more acceptable)


Yes there is. Compile with -fno-strict-overflow.


Sounds easy enough. I've seen it before. I'll try to remember to use it. Thanks


> C standards fault.

I take issue with this sentence, since it makes it sound like there's something wrong with the C standard for it, which isn't the case.


Huh? There's all kinds of things wrong with the C standard. For example, they really went overboard with the UB even for cases that should have arguably been implementation defined or just throw an error.

Eg ending a non-empty source file with anything but a newline is undefined behaviour. So is not closing a string literal.


The C standard (finally) fixed this in C23, see <stdckdint.h>.


Sadly the macros they proposed are ugly, long to type and hard to read. People will not use them.


They seem reasonable to me? They're similar to the compiler builtins people already know and pretty short (7 characters…). What don't you like about them?


If these intrinsics are used can clang or msvc still compile it?


Clang should, MSVC has plans for it but no news since 2018: https://developercommunity.visualstudio.com/t/please-impleme..., so looks like it's a blackhole for now




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: