12 ms·
I'm assuming it's meant to be: std::cout << *p; ? I still think discussing it is largely pointless. It's UB and the compiler can do about anything, as your
by ratmeadow 2y ago
I'm assuming it's meant to be:
std::cout << *p;
?
I still think discussing it is largely pointless. It's UB and the compiler can do about anything, as your example shows. Unless you want to discuss compiler internals, there's no point. Maybe the compiler assumes the code can't execute and removes it all - ok that's valid. Maybe it segfaults because some optimisation doesn't get triggered - ok that's valid. It could change between compiler flags and compiler versions. From the POV of the programmer it's effectively arbitrary what the result is.
Where it gets harmful IMO is when programmers think they understand UB because they've seen a few articles, and start getting smart about it. "I checked the code gen and the compiler does X which means I can do Y, Z". No. Please stop. You will pay the price in bugs later.
- MathMonkeyMan 2y ago> I'm assuming it's meant to be: [...] Nope, I mean inserting the character pointer ("string") into the stream, not the character to which it maybe points. Your second paragraph demonstrates, I think, why my former colleague asked the question. And I agree with your third paragraph.
- ratmeadow 2y agoAh, I got confused for a minute why printing a character pointer is UB. I was thinking of printing the address, which is valid. But of course char* has a different overload because it's a string. You can tell how much I use std::string and std::string_view lol. I reckon we are generally in agreement. Perhaps I am not the best person to comment on the purpose of discussing UB, since I already know all the ins and outs of it... "Been there done that" kind of thing.
- johnnyanmac 2y ago>No. Please stop. You will pay the price in bugs later. indeed. It is called UB because that's basically code for compilers devs to say "welp don't have to worry about changing this" while updating the compiler. What can work in, say, GCC 12 may not work in GCC 14. Or even GCC 12.0.2 if you're unlucky enough. Or you suddenly need to port the code to another platform for clang/MSVC and are probably screwed.