https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67550
Bug ID: 67550 Summary: Initialization of local struct array with elements of global array yields zeros instead of initializer values Product: gcc Version: 5.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: f.knauf at mmkf dot de Target Milestone: --- Initializing a function-local struct array from values in a global struct array yields wrong values with gcc 5.2.1. The problem can be reproduced with the following code: #include <iostream> #include <limits> struct S { int x; int y; }; S const data[] = { { 1, std::numeric_limits<int>::max() } }; int main() { S data2[] = { data[0] }; std::cout << data [0].x << ", " << data [0].y << "\n" << data2[0].x << ", " << data2[0].y << "\n" ; } The output is: $ g++ -Wall -Wextra -pedantic foo.cc $ ./a.out 1, 2147483647 1, 0 The expected output was: 1, 2147483647 1, 2147483647 I am using gcc 5.2.1 on Debian sid (Linux 4.1.3) for x86-64. The error is also reproducible with gcc 5.2.0 on Arch Linux (also x86-64) and gcc 5.1.1 on Fedora 22 (also x86-64). It is not reproducible with gcc 4.9.3. A workaround for the error is to remove the "const" keyword from the declaration of "data", i.e. S data[] = { { 1, std::numeric_limits<int>::max() } }; The error also does not appear if std::numeric_limits<int>::max() is replaced with a compile-time constant value (such as 42). Consequently, in C++11 mode (where std::numeric_limits<int>::max() is constexpr) the above code does not reproduce the error. Instead, int foo() { return 23; } S const data[] = { { 1, foo() } }; can be used to produce the wrong result. With int constexpr foo() { return 23; } the correct result is produced.