What normal C++ syntax are you referring to that generates undefined behavior?
What normal C++ syntax are you referring to that generates undefined behavior?
If you have a function that takes two arguments and prints both, and you supply arguments like so:
my_func(a++, a)
You just generated undefined behavior, because the order in which the expressions inside the function call are evaluated, is not guaranteed (keep in mind the comma inside the function call is not a comma operator, which is one of I think 4 operators that actually does guarantee the evaluation order).You may say it's up to the programmer to know such rules of the programming language, however there's a lot of such rules which makes programming in C++ like walking through a minefield - yes, at some point you learned where the mines are, but you're supposed to remember about them at all times or you risk stepping on one.
Another issue is how the compiler reacts to this UB - usually in a programming language you'd expect that perhaps in one version the arguments are evaluated from left to right, and in another from right to left, or perhaps in some other version it depends on current program state or some other seemingly random factors. However in C++ UB means that everything can happen, for example, the compiler may no-op the entire function call (or worse, the time-space may collapse)
I consider this to actually be an advantage of C++ - there's a lot on the programmer's shoulders, but that gives more possibilities to the compilers (to optimize).
I'm not an expert on C++ but that's my understanding.
My point was about whether C++ screws you over with UB when the programmers intention can be argued to be obvious. I don't think it does. But I will stand corrected if someone can show me what they're talking about.
It seems like people are making this into a discussion about C++ being a difficult language to learn - which I am not defending.
There are two choices here which result in this confusion. C++ made those choices, which you admit are confusing. Other languages did not.
First choice, C++ has the pair of increment operators from C. These operators are a bad idea, so many modern languages either nerf them (e.g. only provide one as a statement like Go) or abolish them altogether (e.g. Rust or Swift).
Second choice, unspecified order of evaluation. In principle the idea here is - maybe we can go faster by making different choices depending on context. In practice the price paid for this potential optimisation is high and the reward dubious. So several modern languages just explicitly define the evaluation order instead.
Thus in something like Rust you can't write my_func(a++, a) but you can write my_func(a+=1, a) and while that's a weird thing to write in Rust it doesn't lead to real confusion let alone Undefined Behaviour. The arguments are evaluated left-to-right, the first argument seemingly increments a and its result is an empty tuple (because it's an assignment), the second argument is a, the variable we recently incremented.
Here's an example where a is an unsigned pointer-sized integer:
https://play.rust-lang.org/?version=stable&mode=debug&editio...
Sure enough with a set to 4000 our output says the second parameter was 4001.
Will someone address my original question?
Here it is again:
"What normal C++ syntax are you referring to that generates undefined behavior? "
The context being that C++ breaks the promise with the programmer by deviating from what was originally intended and generating UB.
For example if I propose that you might foolishly try to multiply unsigned 16-bit integer variables together, I'm sure you'll say that isn't "normal". Or if I proposed you might try to use the C++ stdlib filesystem API on a server which runs other software, doubtless again that just isn't "normal". And so in each case you can feel comfortable that your "normal" C++ is fine.
It is effectively a No True Scotsman argument, all the examples that would prove you wrong aren't "normal" according to you.
There is no valid intention with regards to storing an infinite number series in a finite data storage unit - the example that was brought up.
There is no valid intention about using a construct like my_func(a++, a) - this is a confusing construct and is not valid human-readable code.
This is not normal code that is turned into UB - its bad and invalid code to begin with. In any case it doesn't seem like we're getting anywhere so I will just let you have the last word.
No, the example that was brought up was overflow of a signed integer.
Programming languages do a variety of things here, but you don't seem to be familiar with anything except C++ so let's tour briefly.
BigNum languages. In these languages the limited storage size of a machine register is merely a consideration for optimisations. A programmer who is counting people on Earth, estimating global disk storage capacity in bytes or how many Hydrogen atoms there likely are in the visible Universe, needn't worry that there will be "too many" since the language has their back. There is a limit because machines are finite, but it isn't a real concern. Modern Python and many Lisps are in this family.
What is this "integer" of which you speak? In these languages they chose not to worry about the integer types, or at least not to expose them to programmers. All numeric values are floating point. Tremendously large numbers can be handled, albeit they cease to have integer precision. Javascript is in this category.
You can't do that. In these languages overflow is simply impossible, the compiler is doing enough type checking to figure out that your program could overflow and reject it on this basis. Some languages (including Modern Ada) can do this if you request it for specific variables, but this is how WUFFS works all the time.
Overflow is an Error. In these languages overflow is a runtime error, your program must handle Overflow the same way it's obliged to handle a File Not Found error. This might involve control-flow changes like an Exception and of course you can handle that overflow badly but that's on you. Zig treats overflow as an error.
Runtime failure: In these languages overflow is an immediate runtime failure. Some C and C++ compilers can be told to do this, and it is the behaviour of Rust's integer types in debug builds.
Wrapping Overflow. In these languages overflow causes the number to "wrap" back past the opposite end of its range. This is pretty unintuitive, but it is at least defined. Java does this, most assembly languages do this, and Rust offers Wrapping<T> for integer types to give them this behaviour, it will also do this for integer types in release mode by default.
Undefined Behaviour. This is an extraordinary response. But, it is what C and C++ chose here.
So while you may say there was "no valid intention" the reality is that people simply underestimate how high they might be counting or overestimate how big these integer types are in languages like C++, and so they ran into an overflow, and that's all it takes in C++, whereas in these other languages something better happens. This is perfectly normal code, it may not be the right solution (although in some of the language classes described above it works fine), but "Undefined Behaviour" is an astonishing punishment for this oversight.
Incrementing a number is obvious and results with UB if incremented past the maximum. Since you get no guarantee whatsoever how the UB will be handled (hence the joke with the end of the space-time, or nasal demons etc.), by my understanding (that I think many experts share), it can indeed screw you up.
Of course you may say a programmer should expect this to happen and so it's just a bug - every UB is.
> It seems like people are making this into a discussion about C++ being a difficult language to learn
I think it's more than that. Even assuming that you never caused UB yourself, you can still work on someone else's code with UB and need to debug it. So C++ is hard to learn AND use.
What would be the intention to increment past the maximum? Incrementing a number infinitely is an infinite series 1+1+1[..] A programmer needs to be aware that you cannot store an infinite series in a data type like int - and this applies to any programming language.
If the programmer is looking to exploit wrapping behavior then it is best not to ASSUME anything, but actually verify what promises the language provides. Its basic sanity checking, you do that before sending parameters to API, receiving parameters, during transfer of ownership of memory, etc, etc. All of this falls under basic programming - regardless of language.
This can be extrapolated to anything that causes UB. A programmer needs to be aware that what he does causes UB. You asked for a normal thing that is being done by a programmer that causes UB, and I think I gave a fair example.
I don't want to aggressively argue a point, just wondering what exactly you want to prove here. It's not like programmers cause UB intentionally or when doing some weird stuff like code golfing. No, programmers code in C++ using standard techniques and fall victim to the UB.
Another example of UB is accessing an array out of bounds. Again, you may say a programmer is expected to rationalize his code in this regard, and yet off-by-one is one of the most common categories of errors in practice.
Not really. I asked for examples where the language takes the intention of the programmer and turns it into UB. The quote that I responded to "C++ compiler generates the most efficient thing that may or may not correlate with what you intended"
What is the intention of the programmer attempting to store an infinite number series in an int?
In any case, I think we've run of steam here so you have the last word if you'd like.
https://people.csail.mit.edu/nickolai/papers/wang-undef-2012...
The fact is, you have to carry the cognitive overhead of UB even if you write everything correctly, because in the world of C the compiler it can almost be considered an antagonist if it can get 5% faster.
but in my cases I do even have cbmc in my CI, formal verification. not just a single value is checked, all possible values are. overflows don't just happen with your simple test cases.
In any case, its completely bizarre that I'm asking someone to backup their claim, which I didn't agree with, and you're asking me to prove a negative. I don't know what your intention is.
Whether you had "bugs related to UB" isn't what we were discussing though. The question was merely syntax that generates undefined behaviour, which of course C++ is so riddled with that its committee has (no joke) a sub-working group whose task is just to try to enumerate all the Undefined Behaviour in their language so that they can try to answer the question "Which behaviours are undefined?" without saying "Read the entire standards document and let us know if you figure out the answer".
> The people who paid me own the code.
And so I guess that's why you didn't provide any link to it. Alas, as with UFO sightings, visions of Jesus and the like - while you may be firmly convinced for everybody else it's not evidence at all.
> In any case, its completely bizarre that I'm asking someone to backup their claim [...]
This is very much a "What did the Romans ever do for us?" problem. You've had several takers and you've simply insisted that they don't count. Why should anybody offer more examples which you will likewise dismiss? There's a problem with C++, you've decided to ignore it, but Software Engineering is ultimately a practical discipline, your 15 years of C++ code (if we could see it) would demonstrate that.
Where are these takers? I must have missed them. I did not dismiss anything, I have replied to most comments. You can't pressure me into accepting arguments that I don't agree with. I will present my perspective in a polite way.
The thing is that everyone recognizes this and yet entire categories of bugs that are easily caught with appropriate language design and tooling nonetheless persist in C and C++. Yes, a programmer who does not understand the footguns involved with e.g. integer overflows can be trained to understand them and avoid them. But for every one you train, there will always be another novice programming ready and willing to use the footguns. And even if you do train every last one and every new one, they still have to practice discipline and be vigilant forever more to avoid these problems.
Discipline doesn't scale. It's far better to enforce good behavior at the language level, rather than expecting you'll be able to train your way around these issues.
At risk of sounding crazy what if we just took this undefined behavior and just defined it? Theres like maybe 100 cpu ISAs in the world we care about, and the web and the transitory nature of tech had made legacy support kinda useless. Life would probably be simpler if we just said "ok heres how these constructs work" and just removed the ambiguity
You're criticizing syntax from 30 years ago. Yes, the backwards compatibility (to me) is a BONUS. I would not want to junk working code that I paid for, just so I can use uint8_t. You have the option to begin a slow migration of old code into more modern code - without paying the massive cost of re-writing from scratch.
I guess I am not able to connect with your criticism since it is far removed from my original point - What are examples of normal C++ syntax where the language screws you over with UB? Its a honest question - I'm not longer a C++ dev but I am curious to know what people are talking about.
Man reasons where C syntax has left me out to dry? Well I once spent a week on a deadline debugging a pointer overflow, where it only happened when the game was fullscreen and not debuggable (circa 2005). Even modern C++ is awful though. I fixed a bug a while ago where an "auto&" was just an "auto", and caused the entire scene graph to get copied and then crash.