News for hackers. Stuff that matters.
Re:Bjarne Stroustrup Answers C++ Questions by taneq on Tuesday October 15, 2019 @09:52AM (#21258313)
How is the top C++ question not "undefined behaviour: what the hell?"
Re:Bjarne Stroustrup Answers C++ Questions by otabdeveloper4 on Tuesday October 15, 2019 @10:34AM (#21258819)
"Undefined behavior" is a quirk of the C++ ISO standard legalese, not some property of computing or of the C++ language.

Briefly, the C++ standard defined three classes of stuff that isn't covered by the standard:

Implementation-defined behavior - "behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation documents".

Unspecified behavior - "behavior, for a well-formed program construct and correct data, that depends on the implementation".

Undefined behavior - "behavior for which this document imposes no requirements".

So 'undefined behavior' is stuff that isn't mandated by the standard and also may or may not be an error.

Some UB stuff is bugs (like accessing arrays out of bounds), some isn't bugs but just processor/OS dependent stuff that is out of scope of the C++ standard. (Like what happens when integers overflow.)

In short, the existence of UB is a good thing; having a standard that clearly states what is part of it and what isn't is vastly better than a standard that mandates too much or is too vague.

(And it goes without saying that having no standard at all, like is usual for other programming languages in 2019, is worse in every way.)

Re:Bjarne Stroustrup Answers C++ Questions by twic on Tuesday October 15, 2019 @10:59AM (#21259115)
I'd rather have a language with no standard and no UB than one with a standard and UB.
Re:Bjarne Stroustrup Answers C++ Questions by jcelerier on Tuesday October 15, 2019 @11:07AM (#21259213)
since the language has no standard, from the point of view of ISO, using the whole language itself is UB :-)
Re:Bjarne Stroustrup Answers C++ Questions by twic on Tuesday October 15, 2019 @01:42PM (#21261341)
If ISO want to come round and do some programming for me, i will be happy to discuss it with them!
Re:Bjarne Stroustrup Answers C++ Questions by kllrnohj on Tuesday October 15, 2019 @04:19PM (#21263260)
Then you can still use C++. Just replace all the UB in the standard with whatever your particular compiler & hardware platform do.
Re:Bjarne Stroustrup Answers C++ Questions by alexeiz on Tuesday October 15, 2019 @10:17AM (#21258590)
If you were doing software development in the 80es when C++ was born, you wouldn't be asking this question. You'd either choose a language that allows undefined behavior and write a program that completes the work today or a language with all possible runtime checks and a program that completes the work sometime next week.
Re:Bjarne Stroustrup Answers C++ Questions by taneq on Tuesday October 15, 2019 @09:35PM (#21266177)
Sure, and in the 80s maybe it was justified, but in the 30 years since then, that justification has worn thin and IMO the cost of UB far outweighs the benefits. I've been writing software in C++ since the 90s and have been growing gradually more uneasy with it the whole time.

The thing is, by the very nature of UB, it could have been (and still could be) patched out in a backwards-compatible way. At the very least, catching UB before the nasal demons get out would allow a bit more confidence in C++ code.

Re:Bjarne Stroustrup Answers C++ Questions by pnako on Tuesday October 15, 2019 @09:49PM (#21266252)
Undefined behavior is stuff that is incorrect and must not happen (e.g. out of bounds access). Making it explicitly undefined helps writing efficient implementations.

So not only undefined behavior has not hindered the success of C++, it's probably one of the things contributing to its success, in particular its success in developing embedded and safety-critical systems.

If you write a program for which correctness is particularly important (e.g. a safety-critical program), you must ensure, through tests, proofs, magic, etc. that you don't have any incorrect behavior. Which might be undefined, but again the problem is that it's incorrect, not that it's undefined. If you're flying at Mach 2 and an out-of-bounds read or write happens in your flight control software, it doesn't really matter that much whether you get "undefined behavior" (i.e. read or write "random" address), a panic, a log message, a popup window, etc. The safety is compromised by the fact that you did an incorrect operation, not that the operation is undefined.

Now, if you want to write a music player or something like that, it does make sense to use a language like Python, Go, or whatever might give you more guarantees than C++, because realistically you're not going to write a proper test suite for it.

Re:Bjarne Stroustrup Answers C++ Questions by sixplusone on Tuesday October 15, 2019 @01:03PM (#21260813)
Someone has to write the operating systems and debuggers directly for hardware, including microcontrollers which don't have various cpu facilities/guarantees.
Re:Bjarne Stroustrup Answers C++ Questions by cjfd on Tuesday October 15, 2019 @10:20AM (#21258631)
Because it is quite a bit too obvious?