Ethereal-dev: [Ethereal-dev] A gcc 3.3 warning I don't understand

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Joerg Mayer <jmayer@xxxxxxxxx>
Date: Mon, 15 Sep 2003 00:10:25 +0200
Hello List,

when I compile Ethereal with gcc 3.3.1, I get the following warnings:

plugins.c:232: warning: dereferencing type-punned pointer will break strict-aliasing rules
plugins.c:247: warning: dereferencing type-punned pointer will break strict-aliasing rules
plugins.c:252: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-tlv.c:2725: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-gryphon.c:809: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-gryphon.c:809: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-gryphon.c:809: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-gryphon.c:809: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-gryphon.c:809: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-bssgp.c:736: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-dns.c:1380: warning: type-punning to incomplete type might break strict-aliasing rules
packet-dns.c:1390: warning: type-punning to incomplete type might break strict-aliasing rules
packet-dns.c:1399: warning: type-punning to incomplete type might break strict-aliasing rules
packet-m2pa.c:430: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-m3ua.c:1750: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-mtp3.c:494: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-tzsp.c:395: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-tzsp.c:398: warning: dereferencing type-punned pointer will break strict-aliasing rules
packet-tzsp.c:401: warning: dereferencing type-punned pointer will break strict-aliasing rules

I don't understand what it it talking about: I have read the manpage (excerpt included below) but
still haven't really understood what type-punned means, nor what strict-aliasing (or aliasing at
all) means.

Can someone who knows C please explain these terms to a hobby programmer like me?

 Thanks
          Jörg

PS: I have made the two examples given in the manpage compile, but I don't get a warning in the
  "bad" example

PPS: And here's the manpage from gcc-3.3.1
-----------------snip-----------------------------------------
       -fstrict-aliasing
           Allows the compiler to assume the strictest aliasing rules applicable to the language
           being compiled.  For C (and C++), this activates optimizations based on the type of
           expressions.  In particular, an object of one type is assumed never to reside at the
           same address as an object of a different type, unless the types are almost the same.
           For example, an "unsigned int" can alias an "int", but not a "void*" or a "double".  A
           character type may alias any other type.

           Pay special attention to code like this:

                   union a_union {
                     int i;
                     double d;
                   };

                   int f() {
                     a_union t;
                     t.d = 3.0;
                     return t.i;
                   }

           The practice of reading from a different union member than the one most recently writ­
           ten to (called ``type-punning'') is common.  Even with -fstrict-aliasing, type-punning
           is allowed, provided the memory is accessed through the union type.  So, the code
           above will work as expected.  However, this code might not:

                   int f() {
                     a_union t;
                     int* ip;
                     t.d = 3.0;
                     ip = &t.i;
                     return *ip;
                   }

           Every language that wishes to perform language-specific alias analysis should define a
           function that computes, given an "tree" node, an alias set for the node.  Nodes in
           different alias sets are not allowed to alias.  For an example, see the C front-end
           function "c_get_alias_set".

           Enabled at levels -O2, -O3, -Os.
------------snap----------------------------------------------

-- 
Joerg Mayer                                           <jmayer@xxxxxxxxx>
We are stuck with technology when what we really want is just stuff that
works. Some say that should read Microsoft instead of technology.