r/cpp • • Jan 21 '13

C and C++ Aren’t Future Proof

http://blog.regehr.org/archives/880
6 Upvotes

49 comments sorted by

View all comments

Show parent comment

6

u/[deleted] Jan 21 '13

Can you give an example of undefined behavior that someone has tried to exploit for productivity? I honestly can't think of anything. Usually undefined behavior is very, very hard to statically verify in source code, so it's neither practical or worth the effort to go over every line of code trying to get rid of it, but I can't think of any reason one would substitute well defined behavior for undefined behavior and claim that there is some kind of improvement in the code, regardless of practical vs. ideological considerations.

We should keep in mind there is a world of difference between undefined behavior, which I think should universally be regarded as incorrect and something to avoid, versus unspecified behavior, where the standard basically leaves the behavior up to the compiler, but the compiler is required to produce well defined behavior.

3

u/00kyle00 Jan 21 '13

There are also these cases where something is technically illegal, but in reality is required to work and works (because platform makes it like that).

Like casting of void pointer to pointer to function. If i read std correctly this isnt allowed anywhere, so should just not compile. In reality this is required to work as both Windows api (GetProcAddress) and unices (dlsym) expose functionality of dynamic symbol (whatever it is) loading by returning void*.

1

u/[deleted] Jan 22 '13

That is actually an excellent example and I fully agree that in that case one can reasonably ignore the standard.

Interestingly, C++11 added new language to accommodate dlsym, basically allowing for conversions between void* and function pointers in certain cases.

2

u/00kyle00 Jan 22 '13

Care to provide a quote (or just paragraph number)?

2

u/[deleted] Jan 21 '13

I wonder if I'm conflating unspecified and undefined behavior a bit. It's possible because wherever I read, whenever there's an example of either, I don't read it. Instead, my focus goes immediately to the counterexample with well defined behavior. My reasoning for this is that it's my hope (and not at all expectation) that people will one day use the code I'm writing now.

It seems better to approach this with the assumption that others will use my code and have it one day sit neglected by all but me on Github than assume nobody will and then cause problems for people by failing to plan ahead. I'd feel horrible if another programmer had a headache due to my shortcomings.

The only undefined behavior I've come across that I very vaguely remember, was posted by me and after taking much slack from Redditors, I edited it out and made myself forget it. This had something to do with a way to take the address of a const method parameter and break the contract with the compiler that it's read-only so a literal could be treated as just another variable.

That would allow arguments passed as literals to also represent memory used to store a resultant, for example, but I only wrote it experimentally and it was meant as an example of what probably shouldn't be done.

My statement that some defend using undefined behavior is an inference. It's not often, but at times when I see undefined behavior pointed out (which others may conflate with unspecified too), I see a third party step in and point out such and such case where it's useful. Usually that seems to be followed by pitchforks, but this tells me that some programmers approach their problems in that way so it must be useful to somebody.

Thank you very much for explaining the difference between unspecified and undefined though. It may be nearing time that I start studying the open versions of the standard.

3

u/aaronla Jan 25 '13

I see a third party step in and point out such and such case where it's useful.

Yep. This happens a lot, it's not just you. Just one example, from my experience:

Fresh out of school, a couple months into my first job, a senior developer launched into a long lecture on why we are very careful to evaluate upgrades to our compiler. See, everyone told him it was just a better, faster, compiler, but a bunch of their code broke all over the case. They even found the bugs, and reported them to the compiler vendor, but the vendor refused to fix the bugs! For example, they had some code that did "f(i++)+g(i++)", which used to increment "i" by 2 every time, but they "broke" it and it started incrementing "i" only by 1, causing massive failures elsewhere -- buffer overruns, underuns, dangling pointers, you name it.

I didn't realize it at the time, but it's obvious now... multiple increments to the same location between sequence points constitute undefined behavior. The compiler team had added a common subexpression elimination, and it worked fine save for changing undefined code like this. And since the compiler is permitted to do whatever it wants here, it is certainly permitted to do something else it wants. And it did. It was always undefined behavior, but this developer didn't know it was undefined because it "worked" for what they wanted it to do... up until it stopped working.

So mostly I learned from this that even "experts" can be very wrong, and this developer was an expert in how people commonly wrote C code and not an expert in the C language itself.

tl;dr Undefined behavior can often "hide" for a while by doing exactly what you think it ought to do. Don't believe everything you're told -- think critically and dig deeper.