jeudi 4 juin 2020

struct uninitialization issue in c++

I have following struct whose member is another struct:

struct SA {
    ...
};
struct S {
    SA a;
    ...
};

And in the code I need to reset struct frequently, with:

// data member
S m_s;
// member function
void clear() {
    m_s.a = {};
    m_s.b = {};
    ...
}

I'm using gcc version 7.3.1. With debug build, this works fine, with release build, it fails with:

error: '<anonymous>' is used uninitialized in this function [-Werror=uninitialized]

Why is this error, it is a defined way of initialize data to zero, right? If I change to :

void clear() {
    SA sa{};
    m_s.a = sa;
    m_s.b = {};
    ...
}

instead, the release build will succeed.

Aucun commentaire:

Enregistrer un commentaire