5 ms·
> I wish "undefined behavior" wasn't a thing in C/C++. It's not worth it. :( What do you suggest instead? Run-time checks?
by Bromskloss 10y ago
> I wish "undefined behavior" wasn't a thing in C/C++. It's not worth it. :(
What do you suggest instead? Run-time checks?
- pjmlp 10y agoHave a proper defined behavior like most sane languages. The only reason UB exists was that when ANSI C came to be, the compiler writers and hardware vendors that provided C like compilers didn't want to give up on their semantics. So in order to please everyone, all the semantics that could not be defined on the standard became UB.
- unlinker 10y agoThat is untrue. UB exists because C is supposed to run on so many platforms there is no way to have a defined behaviour for all of them in that standard.
- wongarsu 10y agoMore importantly, C is not only supposed to run on many platforms (many programming languages do that), but also provide very low-level control on wide variety of platforms.
- pjmlp 10y agoWhich people keep forgetting is only available as implementation defined extensions, as such it is no longer ANSI C and any language compiler is free to have extensions.
- jjnoakes 10y agoThis is why implementation defined behavior exists. Undefined behavior though?
- pjmlp 10y agoI was already coding before C had any meaning outside UNIX.
- scott_s 10y agoYou are confusing undefined behavior with implementation defined behavior.
- saynsedit 10y agoI don't think UB is bad. It's a reasonable thing to do when someone invokes an operation with bad input. For instance, what should `sqrt(-1)` be? Or `head([])`? You can define all behavior and say "bad input will throw an exception" or "bad input will abort the entire program" but who really cares what it does since it's a malformed program in the first place? Just say it's undefined and it's up to the programmer to make sure it never happens. Otherwise you're littering your code with countless checks for things that should never happen in a properly coded program. Programmer error is distinct from operational error. There is no sane thing to do in a program error. Constantly checking for programmer error at runtime is wasteful.