News for hackers. Stuff that matters.
Re:A Guide to Undefined Behavior in C and C++ (2010) by almostgotcaught on Sunday March 16, 2025 @09:24PM (#43384295)
you realize UB is basically an escape hatch from the standard for compilers right? it's not like a flaw in the language, it's gaps negotiated by the standards committee (well for the most part i guess). so the reason new languages don't have UB is because new languages don't have multiple implementations (go definitely doesn't, does anyone use rust-gcc?).
Re:A Guide to Undefined Behavior in C and C++ (2010) by Dylan16807 on Sunday March 16, 2025 @09:34PM (#43384347)
You only need implementation-defined behavior to grease the wheels of multiple implementations. You don't need the gaping void of undefined for that use.

There's a big difference between "it'll be some number, not promising which one" and "the program loses definition and anything can break, often even retroactively".

Re:A Guide to Undefined Behavior in C and C++ (2010) by almostgotcaught on Sunday March 16, 2025 @09:38PM (#43384368)
Potato potato. My point is UB isn't an accident it's intentional. Mind you I'm not saying it's great, just that it's not some kind of slipup.
Re:A Guide to Undefined Behavior in C and C++ (2010) by maxlybbert on Monday March 17, 2025 @12:05AM (#43385106)
"Implementation defined" and "undefined" are different things.

On my laptop, sizeof(long) is 8; that's implementation defined. It could be different on my phone, or my desktop, or my work laptop.

Undefined means, roughly, "doing X is considered nonsensical, and the compiler does not have to do anything reasonable with code that does X." In "Design and Evolution of C++," Stroustrup says that undefined behavior applies to things that should be errors, but that for some reason the committee doesn't think the compiler will necessarily be able to catch. When he came up with new ideas for the language, he would often have to choose between making a convoluted rule that his compiler could reliably enforce, or a simple rule that his compiler couldn't always give a sensible error message for.

For instance, the original compilers relied on the system's linker. If the compiler could interact with the linker, it could perhaps detect violations of the One Definition Rule ( https://en.cppreference.com/w/cpp/language/definition ), but since the linker might have been written by a completely different company, and it's acceptable for different source files to be compiled by different compilers (and even be written in other languages -- https://en.cppreference.com/w/cpp/language/language_linkage ) and put together by the linker, and it's common for binary libraries to be sold without source, there's no guarantee that the compiler will ever have the information necessary to detect a violation of the One Definition Rule. So the committee says that violations create a nonsense program, which isn't required to behave in any particular way.

Re:A Guide to Undefined Behavior in C and C++ (2010) by Dylan16807 on Monday March 17, 2025 @01:04AM (#43385361)
Do you think my last sentence is describing things incorrectly? I don't really understand how you could take that depiction and call it "potato potato".
Re:A Guide to Undefined Behavior in C and C++ (2010) by wolvesechoes on Monday March 17, 2025 @04:53AM (#43386397)
"go definitely doesn't"

What? gccgo, TinyGo, and GopherJS.

Re:A Guide to Undefined Behavior in C and C++ (2010) by porridgeraisin on Monday March 17, 2025 @03:47PM (#43392088)
More prominently, microsoft go