News for hackers. Stuff that matters.
Re:“Fiercely resist any further broadening of the scope of t... by physguy1123 on Thursday April 14, 2016 @12:47PM (#11498273)
To add to this, a lot of what compilers decide is undefined isn't bizarre corner case code that rarely happens, but common and useful techniques that the compiler will happily demolish without warning. The 'defined' workarounds, however, are usually difficult, less safe, harder to understand, and often slower. This is without getting into the nightmare that is LTO finding 'undefined' behavior across program boundaries.
Re:“Fiercely resist any further broadening of the scope of t... by haberman on Thursday April 14, 2016 @12:53PM (#11498338)
> The 'defined' workarounds, however, are usually difficult, less safe, harder to understand, and often slower.

That has not been my experience. Do you have an example of this?

Re:“Fiercely resist any further broadening of the scope of t... by physguy1123 on Thursday April 14, 2016 @01:49PM (#11498916)
Sure - all sorts of stuff with type-punning in unions because much harder to understand when it's transformed into a whole series of casts to/from void/char , with the loss of checking that one gets from a union. Even without unions, aliasing rules can screw you over in surprising ways. Look at the free list example linked in the post for another one.

Slower - the various methods of checking for integer overflow are slower than x = a + b; if (a < x) {/overflow*/}. It can also be easier to just use a slower, or more memory hungry way of doing things than a better but needlessly complicated method.

At my current job we've sacrificed memory (and in this case, performance as a consequence) because the complexity cost of keeping some critical code in the defined behavior realm was too risky.

Re:“Fiercely resist any further broadening of the scope of t... by Kristine1975 on Thursday April 14, 2016 @02:11PM (#11499108)
Type punning in unions is best done using memcpy. Compilers know what it does and optimize it: http://blog.regehr.org/archives/959

As for integer overflow at least gcc and clang have built-in functions that generate optimal code by checking the CPU's overflow flag: http://clang.llvm.org/docs/LanguageExtensions.html#checked-a... and https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins...

Re:“Fiercely resist any further broadening of the scope of t... by colin_mccabe on Friday April 15, 2016 @12:12AM (#11502079)
C99 Techincal Corrigendum 3 clarifies that union-based type punning is valid by amending 6.5.2.3 with this footnote:

If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called “type punning”). This might be a trap representation.

(copied from a comment on blog.regehr.org)

This is actually a really common area of misunderstanding. But ultimately, one of the main uses of unions is to provide multiple ways to access the same data.

Re:“Fiercely resist any further broadening of the scope of t... by physguy1123 on Monday April 18, 2016 @10:51AM (#11520333)
In C, yes, type-punning with union is valid. However, in C++, it is considered undefined behavior and may get removed by the compiler if it sees fit.
Re:“Fiercely resist any further broadening of the scope of t... by colin_mccabe on Monday April 25, 2016 @12:39AM (#11562533)
Thanks for the clarification. Do the new C++ standards explicitly discuss this anywhere?
Re:“Fiercely resist any further broadening of the scope of t... by physguy1123 on Thursday April 28, 2016 @04:50PM (#11591802)
There's this stackoverflow qanswer: http://stackoverflow.com/questions/11373203/accessing-inacti...

where answers try to distill the standard into something legible by humans. Unsurprisingly, they come to different conclusions.

The overall interpretation in some situations, if you look at things just right, you can do type punning in C++. But it's pretty risky since you're a LOC away from the compiler deleting your function.

Or, you can't really do it at all safely. Which I would conclude from the above anyways.

Re:“Fiercely resist any further broadening of the scope of t... by AlexandrB on Thursday April 14, 2016 @01:43PM (#11498853)
There were examples in the linked message board post [1]. Note how there is now a paragraph describing why the code is written with 4 casts instead of comparing pointers naively. Multiply this by hundreds of instances of similar code in a large code base.

[1] http://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=tools/libx...

Re:“Fiercely resist any further broadening of the scope of t... by Kristine1975 on Friday April 15, 2016 @01:42AM (#11502442)
Why not just write a function is_ptr_less that does all the casts.

P.S: In C++ std::less is guaranteed to do the right thing(tm).