Does dynamic type choice break strict aliasing in C? -
Say I have some code that looks like this:
void foo (Zero * Obj, int type) {TypeA * a = (TypeA *) obj; TypeB * b = (TypeB *) obj; TypeC * c = (TypeC *) obj; If (type == 1) uses the {// code "a"} and if (type == 2) uses the {// code "b"} and if (type == 3) {// Code "C"}}
I am curious to know that this function breaks tight aliasing from a priority or the validity of the function depends on that How exactly is this used? I want to expect a certain part of me that things will work properly if I only used the function as foo (& objectOfTypeA, 1)
, foo (& objectOfTypeB, 2) Is
, or foo (and Object Opti C, 3)
. Still, I only know about aliasing for errors for a few days, and therefore I worry that the function foo ()
is broken right from the start because it will come in different velocities * Start with it in various types of cast.
This does not break strict aliasing, because you can only type * obj
Casting obj
does not result in * obj
.
Strict aliasing rule
Strict aliasing can not only manipulate points for one object, pointer manipulation.
from n1570 §6.5 ¶7
An object will have stored value delivered by only one of the live expressions in which the following types ...
Since you are not reaching * obj
, but just obj
, strict aliasing rule has not been violated.
conversion
The second question here is whether the conversions are acceptable or not. The answer is yes, because you can change any object pointer type to void *
. Type is zero
, this is the only type with this property.
int * i; Zero * ptr = & amp; I; Float * f = ptr; // Valid
The standard f
is silent on what is the value. In other words, until conversion is not acceptable, you can not do anything useful with f
. You can not change it back to void *
.
It is explained in n1570 §6.3.2.3 ¶1:
An indicator with
zero
in an item type with an indicator Can be changed. The indicator of any object type can be converted to an indicator onzero
and again, the result can be compared to the equivalent of the original pointer.
, the conversion is always acceptable but is not useful in any way.
Comments
Post a Comment