This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Help with understanding strict aliasing rules
Martin Lucina writes:
> 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?
Well, the phrases "is not legal" and "will work fine" are clearly
contradictory! It can't be both.
> In other words, (void *) is convertible to any incomplete or object
> type, but (void **) is not, despite the fact that *(void **) is a (void *)?
That's right.
> > (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.
-fno-strict-aliasing will do that, but will disable some
optimizations. Your call.
Andrew.