public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Horgan <phorgan1@yahoo.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: GCC aliasing rules: more aggressive than C99?
Date: Tue, 05 Jan 2010 06:35:00 -0000	[thread overview]
Message-ID: <4B42DD98.8010200@yahoo.com> (raw)
In-Reply-To: <4B407A44.9070409@redhat.com>

Andrew Haley wrote:
> On 01/03/2010 05:46 AM, Joshua Haberman wrote:
>   
>> ...elision by Patrick...
>> To me this allows the following:
>>
>>   int i;
>>     *pu = (union u*)&i;
>>   printf("%d\n", pu->x);
>>     
>
> I do not believe that this is allowed.  A union of type
>
>    union u { int x; }
>
> is not compatible with the type int.  It might have greater alignment,
> it might be larger, and so on.  
It couldn't have different alignment since the member has to have the 
same alignment it would usually have and padding at the beginning of a 
union is not allowed.  They are certainly layout-compatible types.   I'm 
not sure about larger, why would a compiler add padding to the end?  In 
the C++ spec it couldn't, since they don't have the clause about being 
able to add padding at the end.  Certainly in gcc the size is the same.  
The standard doesn't specify anything about this other than the size 
must be sufficient for the largest member, and that padding may be added 
at the end.  What do you mean by and so on?  Not trying to be 
argumentative or anything, but now I've gotten intrigued.  It's interesting!
> We do know that a pointer to a union
> can be converted to a pointer to each of its members and vice versa.
> But, and this is crucial, *that does not mean they are compatible
> types*.
>   
A pointer to union and a pointer to int are not required to have the 
same representation.   But what about printf("%d\n", (*((union 
u*)&i)).x);  That bypasses the issues about pointers entirely.  I know 
it's silly code, but it seems to fit the aliasing rules. gcc doesn't 
warn about it even with -Wstrict-aliasing=3 and -fstrict-aliasing, nor 
does g++ warn about an equivalent program using cout instead of printf.

Patrick





      reply	other threads:[~2010-01-05  6:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <loom.20100103T063932-636@post.gmane.org>
2010-01-03 11:06 ` Andrew Haley
2010-01-05  6:35   ` Patrick Horgan [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B42DD98.8010200@yahoo.com \
    --to=phorgan1@yahoo.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).