public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Yet another aliasing question.
@ 2006-12-23 10:45 Sergei Organov
  2006-12-23 11:32 ` Andrew Haley
  0 siblings, 1 reply; 3+ messages in thread
From: Sergei Organov @ 2006-12-23 10:45 UTC (permalink / raw)
  To: gcc-help

Hello,

I'm trying to understand the implications of strict aliasing rules on
programming practices. I've got two example functions foo() and boo()
that I think both are valid from the POV of strict aliasing
rules. GCC either warns about both (with -Wstrict-aliasing=2) or doesn't
warn about any (with -Wstrict-aliasing), and generates the assembly as
if the functions don't violate the rules.

I'm still in doubt, especially w.r.t. the boo() function. Could anybody
clarify the issue please?

$ cat alias.c

typedef struct { int i; } S;

int i;
int foo()
{
  // Accessing object 'i' of type 'int' through 'S' containing 'int'
  // field.  Should be OK from C99 standard POV?
  S const sc = { 10 };
  *(S*)&i = sc;
  return i;
}

S s;
int boo()
{
  // Accessing 's' of type 'S' through 'int'. Is it aliasing rules
  // violation?  Maybe yes, but on the other hand this could be
  // considered as accessing 's.i' of type 'int' through 'int' that
  // should be OK from C99 standard POV?
  *(int*)&s = 10;
  return s.i;
}

$ gcc-4.2 -O3 -W -Wstrict-aliasing=2 -c alias.c -o alias.o
alias.c: In function 'foo':
alias.c:9: warning: dereferencing type-punned pointer might break strict-aliasing rules
alias.c: In function 'boo':
alias.c:19: warning: dereferencing type-punned pointer might break strict-aliasing rules

-- Sergei.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Yet another aliasing question.
  2006-12-23 10:45 Yet another aliasing question Sergei Organov
@ 2006-12-23 11:32 ` Andrew Haley
  2006-12-23 12:24   ` Sergei Organov
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Haley @ 2006-12-23 11:32 UTC (permalink / raw)
  To: Sergei Organov; +Cc: gcc-help

Sergei Organov writes:

 > I'm trying to understand the implications of strict aliasing rules on
 > programming practices. I've got two example functions foo() and boo()
 > that I think both are valid from the POV of strict aliasing
 > rules.

It's hard to discuss this without looking at the standard.  Can you
please quote the exact language in the standard thta justfies your
opinion?

Andrew.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Yet another aliasing question.
  2006-12-23 11:32 ` Andrew Haley
@ 2006-12-23 12:24   ` Sergei Organov
  0 siblings, 0 replies; 3+ messages in thread
From: Sergei Organov @ 2006-12-23 12:24 UTC (permalink / raw)
  To: gcc-help

Andrew Haley <aph@redhat.com> writes:

> Sergei Organov writes:
>
>  > I'm trying to understand the implications of strict aliasing rules on
>  > programming practices. I've got two example functions foo() and boo()
>  > that I think both are valid from the POV of strict aliasing
>  > rules.
>
> It's hard to discuss this without looking at the standard.  Can you
> please quote the exact language in the standard thta justfies your
> opinion?

Yes, sure:

  An object shall have its stored value accessed only by an lvalue
  expression that has one of the following types:

  - a type compatible with the effective type of the object,
  [...]
  - an aggregate or union type that includes one of the aforementioned
    types among its members (including, recursively, a member of a
    subaggregate or contained union)


-- Sergei.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-12-23 12:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-23 10:45 Yet another aliasing question Sergei Organov
2006-12-23 11:32 ` Andrew Haley
2006-12-23 12:24   ` Sergei Organov

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).