This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Undefined behavior due to 6.5.16.1p3


On 03/10/2015 05:18 PM, Martin Sebor wrote:
I suspect every compiler relies on this requirement in certain
cases otherwise copying would require making use of temporary
storage. Here's an example:
Thanks, this example is indeed not already undefined by effective types, nor 6.2.6.1p6.

An entirely worked out example is:

#include<stdio.h>

struct A { int a [32]; };

union {
  struct A a;
  struct { char b1; struct A b2; } b;
} u;

void init(struct A *p) {
  for (int i = 0; i < 32; i++) { p->a[i] = i; }
}

int test(struct A *p) {
  int b = 0;
  for (int i = 0; i < 32; i++) {
    printf("%d=%d\n", i, p->a[i]);
    if (p->a[i] != i) b = 1;
  }
  return b;
}

int main() {
  init(&u.a);
  u.b.b2 = u.a;
  return test(&u.b.b2);
}

The return value is 1 instead of 0 when compiled with GCC and clang with optimizations enabled.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]