Bug 69901

Summary: Iniitializing non-const global array variable from runtime const global variable does not copy all values properly
Product: gcc Reporter: Piotr Nycz <piotrwn1>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED DUPLICATE    
Severity: normal CC: daniel.kruegler, jakub, webrown.cpp
Priority: P3    
Version: 5.2.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description Piotr Nycz 2016-02-22 09:52:21 UTC
We discovered this in our project working under gcc4.9.2 (-std=c++14) but it is also present in gcc5.2 - whilst not present in clang.

The code below does not copy b1 to b2 properly:
  - b2[0].c.a is 0
  - b1.c.a

Making b1 compiler time constant (by making makeA() function constexpr) "fixes" the problem:

#include <iostream>
 
struct A 
{
    int a;
};
 
struct B
{
    int b;
    A c;
};
 
/*constexpr*/ //<-- contstexpr here "fixes" the problem
A makeA()   
{
    A a = {2};
    return a;
}
 
const B b1 = {1, makeA()};
 
B b2[] = { b1 };
 
 
int main()
{
    std::cout << b2[0].c.a << std::endl; // 0 (should be 2)
    std::cout << b1.c.a << std::endl;    // 2
}

The link to coliru: http://coliru.stacked-crooked.com/a/4f1ef718fea4e76f

The original example was with std::vector and much more complicated structs. We did an effort to simplify example. Simplifying more also "fixes" the problem.

BR,
Piotr Nycz (piotrwn1(at)gmail.com)
Comment 1 Markus Trippelsdorf 2016-02-23 09:52:31 UTC
This bug is already fixed.
Comment 2 Piotr Nycz 2016-02-23 23:36:07 UTC
How to get information in which version it is fixed?
What we discover ourselves:
4.9.1 - work
4.9.2 - fail
4.9.4 - work
5.2 - fail
6.0 (experimental) - work

I need to know the newest stable version where it is fixed. How can I get this knowledge?
Comment 3 Markus Trippelsdorf 2016-02-24 07:25:47 UTC
(In reply to Piotr Nycz from comment #2)
> How to get information in which version it is fixed?
> What we discover ourselves:
> 4.9.1 - work
> 4.9.2 - fail
> 4.9.4 - work
> 5.2 - fail
> 6.0 (experimental) - work
> 
> I need to know the newest stable version where it is fixed. How can I get
> this knowledge?

You can run an inverse "git bisect" on the source tree for example.
Comment 4 Jakub Jelinek 2016-02-24 07:54:03 UTC
r231777 aka PR67550 fixed that.
Comment 5 Andrew Pinski 2016-02-24 07:55:49 UTC
And yes it is a dup of bug 67550.

*** This bug has been marked as a duplicate of bug 67550 ***