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]

Possible invalid code - GCC 4.8/5.1.


Hi,

I wonder if the following is a bug:

#include <stdio.h>

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


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