https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116469

--- Comment #2 from Arvid Jonasson <jonassonarvid02 at gmail dot com> ---
Quick update: I initially overlooked that the classes were aggregate types,
which don't require zero-initialization. However, the issue persists with
non-aggregate types. To demonstrate this, I've modified example 1 for Outer1
and Outer3 by making the dummy integer private, which makes them non-aggregate
(https://eel.is/c++draft/dcl.init.aggr#1.2).


------------------------------------------------------------------------------
#include <iostream>

struct Inner {
    Inner(){}
    unsigned char arr[10];
};

// Struct 1: Zero-initialized
struct Outer1 {
private:
    int dummy;
public:
    Inner inner;
};

// Struct 3: Not zero-initialized
struct Outer3 {
public:
    Inner inner;
private:
    int dummy;
};

int main() {
    std::cout << "Outer1:\n";
    for(int i = 0; i < 2; ++i) {
        unsigned char counter = 0;
        Outer1 outer{};
        for(auto &c : outer.inner.arr) {
            std::cout << int(c) << ' ';
            c = counter++;
        }
        std::cout << '\n';
    }

    std::cout << "Outer3:\n";
    for(int i = 0; i < 2; ++i) {
        unsigned char counter = 0;
        Outer3 outer{};
        for(auto &c : outer.inner.arr) {
            std::cout << int(c) << ' ';
            c = counter++;
        }
        std::cout << '\n';
    }
}
------------------------------------------------------------------------------

Output:
arvidjonasson@Arvids-MacBook-Air ~/testZeroInit
% g++-14 -O3 -std=c++11 -Wall -Wextra example1.cpp -o example1.out -save-temps
arvidjonasson@Arvids-MacBook-Air ~/testZeroInit
% ./example1.out
Outer1:
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
Outer3:
0 0 0 0 0 1 2 3 4 5 
0 1 2 3 4 5 6 7 8 9

------------------------------------------------------------------------------
Expected behavior:
Both Outer1 and Outer3 objects should be list-initialized
(https://eel.is/c++draft/dcl.init#list-3.5), leading to value-initialization.
This should zero-initialize the objects before default-initializing them
(https://eel.is/c++draft/dcl.init#general-9), resulting in all zeroes being
printed.

Actual behaviour:
 * Outer1 is properly zero-initialized.
 * Outer3 is not properly zero-initialized.

Reply via email to