2 ms·
> The C standard specifies that values “cannot” be accessed through pointers that do not match the effective type of the value Yet they can be and often are by
by jancsika 8y ago
> The C standard specifies that values “cannot” be accessed through pointers that do not match the effective type of the value
Yet they can be and often are by using the union trick.
To decide whether to use the union trick requires a discussion of a program's desired portability which-- while usually desirable-- is a separate issue from undefined behavior.
- FartyMcFarter 8y agoOn top of this, unions behave differently in C++ wrt undefined behaviour.
- mort96 8y agoThe union thing is still 100% undefined behavior though, right? Some specific compilers guarantee a particular behavior, but by relying on it, you're not _really_ writing standard C anymore.
- vyodaiken 8y agoThe rule allows both the union trick and a totally hacky char type exception (this was added because the standards authors found out that their rule was inconsistent and instead of backing it out, tried to hack it up). 7 An object shall have its stored value accessed only by an lvalue expression that has one of the following types:88) — a type compatible with the effective type of the object, — a qualified version of a type compatible with the effective type of the object, — a type that is the signed or unsigned type corresponding to the effective type of the object, — a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object, — an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or — a character type