Re: aliasing issue after automatic inlining

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

On Wednesday 20 April 2011 09:47:42 Felix J. Ogris wrote:
> On Apr 20, 2011, at 2:29 AM, Ian Lance Taylor wrote:
> > This code is accessing values of type int using a pointer of type
> > short*.  That is invalid aliasing and your program uses undefined
> > behaviour.  Your suggested workarounds are the right one.
> > 
> > To get a warning, you could try the -Wstrict-aliasing option.  I don't
> > think it exists in gcc 4.2, though.
> 
> I tried with -Wstrict-aliasing, -Wstrict-aliasing=1..2 and
> -Wstrict-aliasing=3 with gcc 4.6, but didn't get a warning. This is a
> little bit annoying as it just happens after the implicit
> optimization/inlining. Declaring struct s as volatile or compiling with
> -fno-inline-functions helps, too. However, I'll stick with
> -fno-strict-aliasing.

I guess the warning is suppressed by the use of void*. I would assume you'll 
get a warning once you pass the pointer as short*.

You might also be able to "fix" your program with __attribute__((may_alias)). 
Like this:
int chksum(void *ptr, unsigned int len) {
  typedef short short_alias __attribute__((may_alias));
  int sum = 0;
  short_alias *val = ptr;

  for (; len >= sizeof(short); len -= sizeof(short)) {
    sum += *val;
    val++;
  }

  return sum;
}


Regards,
	Matthias


-- 
Dipl.-Phys. Matthias Kretz
http://compeng.uni-frankfurt.de/?mkretz

SIMD easy and portable: http://compeng.uni-frankfurt.de/?vc


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux