Possible invalid code - GCC 4.8/5.1.
Hi, I wonder if the following is a bug: #include int main(void) { struct str { struct a { int a1, a2; } a; }; struct str src = {.a = {.a1 = 1, .a2 = 2}}; struct str dest = {.a = src.a, .a.a2 = 3}; printf("src: %u %u, dest: %u %u\n", src.a.a1, src.a.a2, dest.a.a1, dest.a.a2); return 0; } $ gcc -W -Wall test.c -o test $ ./test src: 1 2, dest: 0 3 GCC 4.8.4 (ARM), Red Hat 4.9.2-6, Red Hat 5.1.1-1 (x86-64 Fedora 21 and 22). Should I file a bz report? BTW changing the initializer from ".a = src.a" to ".a = {1, 2}" produces the expected result: $ gcc -W -Wall test.c -o test test.c: In function ‘main’: test.c:13:9: warning: initialized field overwritten [-Woverride-init] struct str dest = {.a = {1, 2}, .a.a2 = 3}; ^ test.c:13:9: warning: (near initialization for ‘dest.a.a2’) [-Woverride-init] $ ./test src: 1 2, dest: 1 3 -- Krzysztof Halasa Industrial Research Institute for Automation and Measurements PIAP Al. Jerozolimskie 202, 02-486 Warsaw, Poland
GCC 4.9.3 Release Candidate available from gcc.gnu.org
The first release candidate for the next release from the GCC 4.9 branch, GCC 4.9.3, is available from ftp://gcc.gnu.org/pub/gcc/snapshots/4.9.3-RC-20150617/ and shortly its mirrors. I have so far bootstrapped and tested the release candidate on x86_64-unknown-linux-gnu. Please test it and report any issues to bugzilla. If all goes well, I'd like to release 4.9.3 in the middle of the next week.
GCC 4.9.3 Status Report (2015-06-17) - branch frozen for release
The GCC 4.9 branch is now frozen for preparing of a release candidate for GCC 4.9.3. All changes to the branch require release manager approval from now on until 4.9.3 is released. I'll announce the GCC 4.9.3 release candidate once it is ready.
Re: Possible invalid code - GCC 4.8/5.1.
On 17 June 2015 at 09:16, Krzysztof Hałasa wrote: > Hi, > > I wonder if the following is a bug: > > #include > > int main(void) > { > struct str { > struct a { > int a1, a2; > } a; > }; > > struct str src = {.a = {.a1 = 1, .a2 = 2}}; > > struct str dest = {.a = src.a, .a.a2 = 3}; > > printf("src: %u %u, dest: %u %u\n", src.a.a1, src.a.a2, dest.a.a1, > dest.a.a2); > return 0; > } > > $ gcc -W -Wall test.c -o test > $ ./test > src: 1 2, dest: 0 3 > > GCC 4.8.4 (ARM), Red Hat 4.9.2-6, Red Hat 5.1.1-1 (x86-64 Fedora 21 and 22). > > Should I file a bz report? N.B. mails about bug are almost never appropriate on this mailing list, either ask on the gcc-help mailing list or file a bugzilla, don't ask "should I file a bugzilla" on this list. This is the correct behaviour. When the initializer .a.a2 = 3 is processed the current object is the whole of dest, so all the other members of the current object are set to zero, which overrides the values set by the earlier .a = src.a initializer. It would be nice if the compiler warned you about this. > BTW changing the initializer from ".a = src.a" to ".a = {1, 2}" produces > the expected result: > > $ gcc -W -Wall test.c -o test > test.c: In function ‘main’: > test.c:13:9: warning: initialized field overwritten [-Woverride-init] > struct str dest = {.a = {1, 2}, .a.a2 = 3}; > ^ > test.c:13:9: warning: (near initialization for ‘dest.a.a2’) [-Woverride-init] > > $ ./test > src: 1 2, dest: 1 3 I believe this is also correct behaviour. The brace-enclosed initializer list {1, 2} makes .a the "current object", then the elements make .a.a1 and .a.a2 the current object in turn. When the .a.a2 = 3 initializer is processed .a.a2 is the current object, and that object has no other members, so nothing else is set to zero.
Re: Possible invalid code - GCC 4.8/5.1.
See bug 63944 and DR#413 regarding such cases. -- Joseph S. Myers jos...@codesourcery.com
gcc-4.9-20150617 is now available
Snapshot gcc-4.9-20150617 is now available on ftp://gcc.gnu.org/pub/gcc/snapshots/4.9-20150617/ and on various mirrors, see http://gcc.gnu.org/mirrors.html for details. This snapshot has been generated from the GCC 4.9 SVN branch with the following options: svn://gcc.gnu.org/svn/gcc/branches/gcc-4_9-branch revision 224585 You'll find: gcc-4.9-20150617.tar.bz2 Complete GCC MD5=c04987775920ec50d3251eef076c8ce2 SHA1=d46de07a1525f4cd077b6ee47a6b839bff31ca9d Diffs from 4.9-20150610 are available in the diffs/ subdirectory. When a particular snapshot is ready for public consumption the LATEST-4.9 link is updated and a message is sent to the gcc list. Please do not use a snapshot before it has been announced that way.