Thx in advans,
Karthik Balaguru
> What could be the scenarios that will cause the below error -
> warning: dereferencing type-punned pointer will break strict-aliasing
> rules
Illegal pointer casts. Simply: if you have a ponter to Type A, do not
cast it to an incompatile Type B.
Andrew.
Note: This is a simplified description, you better consult with a search on
"Aliasing C"
To understand this Problem you first have to understand what aliasing is.
The most simple "version" of aliasing should be known by you, because it is a
language element: The union.
union foo
{
float a;
int b;
};
In this case float a and int b share the same storage, but also alias each other.
The C Std. says not much about this, besides that both share the same storage
and you can write and read a OR b (but not both, so essence still no aliasing
allowed, GCC makes a special exception to this, a union is a special alias
sandbox in GCC).
Since C supports pointers, you can generate (kind of) the same thing with pointers:
int some_storage;
float *f = (float *)&some_storage;
Now you also have aliasing.
There is only one problem:
Since a language with pointers is a nightmare for compilers and optimizers (they
always have to assume the worst case, any memory access over a pointer
invalidates any variable, they could alias), the C Std declared:
Pointer of different types except char and void can not alias.
And it is a "natural" viewing point:
A mem location where you saved some int, there could be no floats or something else.
Our code above violates this.
This mostly breaks down if you either do some nasty bit-tricks or have some
broken interface/API.
And the Compiler is free to stick to the C rules and misscompile this code
(variables do not get reloaded because they can not alias, code removed because
the compiler thinks it does nothing etc.)
Casts give you the rope to shoot yourself in the foot in this case, thats the
part about type-punning.
And that you dereference it and not only do some pointer math means there is a
high probability you have a problem here.
In most developers view C is some kind of high level ASM, and foster this view
anytime they see the result of the compiler, which did things in ASM as you
"meant" it/thought it should work this way.
Unfortunately this is not true.
Yes, C has an abstract memory model, not very sophisticated, but abstract (Ex.:
There is nowhere said an architecture must have a stack). That things still
mostly fall into place is "coincidence" (or: the compiler generates good code).
> Thx in advans,
> Karthik Balaguru
Greetings
Jan
--
"Remember: With great power comes great current squared times resistence."
Ohm never forgot his dying uncle's advice.
xkcd.com/643/
hmm. Interesting !
> Casts give you the rope to shoot yourself in the foot in this case, thats the
> part about type-punning.
> And that you dereference it and not only do some pointer math means there is a
> high probability you have a problem here.
>
> In most developers view C is some kind of high level ASM, and foster this view
> anytime they see the result of the compiler, which did things in ASM as you
> "meant" it/thought it should work this way.
> Unfortunately this is not true.
> Yes, C has an abstract memory model, not very sophisticated, but abstract (Ex.:
> There is nowhere said an architecture must have a stack). That things still
> mostly fall into place is "coincidence" (or: the compiler generates good code).
>
Thx for the info.
Karthik Balaguru