Help with understanding strict aliasing rules

Martin Lucina mato@kotelna.sk
Mon Aug 28 11:10:00 GMT 2006


aph@redhat.com said:
> Please read the actual standard too.  The rules about pointer type
> conversions are at 6.3.2.3.  The appropriate paragraphs are paragraphs
> 1 and 7.

So according to paragraph 1, the following is legal (obviously, this is
basic C):

void *return_item (int *rc);

...
item_t *item_p;
int rc;

item_p = return_item (&rc);

but the case 

int return_item (void **item_p);

...
item_t *item_p;
int rc;

rc = return_item ((void **)&item_p);

is not legal since (void **) doesn't fall under the specification of
paragraph 1, but will work fine as long as item_p is only ever
dereferenced as an item_t?

In other words, (void *) is convertible to any incomplete or object
type, but (void **) is not, despite the fact that *(void **) is a (void
*)?

> (unless you cast it back to its original type first).  This warning
> should be interpreted as saying that your interfaces are badly designed,
> and the correct way to avoid the warning is to redesign them in a way
> where you do not need to cast between conflicting types.  (Even if you

Unfortunately I can't change the interfaces since they're used by
legacy code.  I'd just like to get rid of the warnings since if people
get used to ignoring the warnings in this code then they are likely to
miss other more important problems.

-mato



More information about the Gcc-help mailing list