This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Undefined behavior due to 6.5.16.1p3
- From: Robbert Krebbers <mailinglists at robbertkrebbers dot nl>
- To: Martin Sebor <msebor at redhat dot com>, gcc at gcc dot gnu dot org
- Date: Tue, 10 Mar 2015 17:44:44 +0100
- Subject: Re: Undefined behavior due to 6.5.16.1p3
- Authentication-results: sourceware.org; auth=none
- References: <54FDF3CE dot 6030507 at robbertkrebbers dot nl> <54FF195B dot 5060604 at redhat dot com>
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.