F
Francis Moreau
Hello,
I did some googling to understand what "type punning" means and I
found, in my understanding, it is no more and no less than a what's
done by a cast operator.
I wrote a simple example, that makes me feel I'm really missing
something, so I'd like to show you that small piece of code:
struct bar {
double a;
};
struct foo {
int a;
};
struct foo global;
void func_ok(void)
{
struct bar *g = (struct bar *)&global;
g->a++;
}
void func_ko(void)
{
((struct bar *)&global)->a++;
}
Here, gcc emits the following warning:
In function ‘func_ko’: warning: dereferencing type-punned pointer will
break strict-aliasing rules.
I can understand the warning, but why doesn't 'func_ok()' emit the
same warning too ?
Thanks
I did some googling to understand what "type punning" means and I
found, in my understanding, it is no more and no less than a what's
done by a cast operator.
I wrote a simple example, that makes me feel I'm really missing
something, so I'd like to show you that small piece of code:
struct bar {
double a;
};
struct foo {
int a;
};
struct foo global;
void func_ok(void)
{
struct bar *g = (struct bar *)&global;
g->a++;
}
void func_ko(void)
{
((struct bar *)&global)->a++;
}
Here, gcc emits the following warning:
In function ‘func_ko’: warning: dereferencing type-punned pointer will
break strict-aliasing rules.
I can understand the warning, but why doesn't 'func_ok()' emit the
same warning too ?
Thanks