aliasing issue after automatic inlining

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

 



Hi,

maybe someone can enlighten me? Given a small test.c:

#include <stdio.h>

int chksum(void *ptr, unsigned int len) {
  int sum = 0;
  short *val = ptr;

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

  return sum;
}

int main() {
  struct s {
    int a;
    int b;
  } s;

  s.a = 2;
  s.b = 1;
  s.a = chksum(&s, sizeof(struct s));

  printf("%i\n", s.a);

  return 0;
}


Expected output: 3
Compiled using `gcc -W -Wall -O3 -o test test.c` with gcc 4.1.2 on CentOS 5.4 x86_64 or with gcc 4.2.1 on FreeBSD 8.2 i386 gives random rubbish instead, eg. -16897
Compiled with -O2 or -fno-strict-aliasing or -fno-inline-functions gives expected output.

Is there anything I could do against this (mis-)behaviour - except using one the above flags or using a union as *val which contains two shorts? Is there any -Warning parameter for this?

TIA,
Felix



[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