In said:
When I compile this (with GCC and optimizations on):
void foo(void)
{
int a;
float f = *((float *) &a);
}
I get the warning : "dereferencing type-punned pointer will break
strict-aliasing rules". Does this mean that f and a can be stored in the
same memory location?
Nope, it means that you're trying to access a memory area using another
type than the one used in the declaration of the object occupying that
area. Sometimes, this is allowed, but in the general case this results
in undefined behaviour. Here's the exact text of the standard:
7 An object shall have its stored value accessed only by an lvalue
expression that has one of the following types:73)
- 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.
____________________
73) The intent of this list is to specify those circumstances
in which an object may or may not be aliased.
Dan