AnsweredAssumed Answered

MW compiler class member (struct) initialization

Question asked by Juha Laukkanen on Sep 17, 2015
Latest reply on Sep 17, 2015 by Marius Grigoras

Hello.

 

Following test code compiled with Metrowerks compiler doesn't initialize Test2 struct members member1 and member2 to 0. GCC does however generate default initializer for Test2 that initializes all inside the struct to zero. GCC does that in all cases how class is instantiated but MW doesn't do in any case. To me this seems a bit odd. Am I missing some compiler flag which would do this?

 

Used flags are -proc e500v2 -cwd proj -ipa off -fp dpfp -spe_vector -norelax_ieee -char unsigned -use_lmw_stmw on -w all -w err -lang c++ -RTTI off

 

GCC version is 4.8.2 (gcc-4.8.2-Ee500v2-eabispe) and MW compiler is 4.3 build 278. Both supplied with CW 10.4.

 

 

class TestClass
{

public:
    TestClass();
    TestClass(char max);

    void fun1();

private:
    long*        m_data;

    struct Test2
    {
        unsigned short member1;
        unsigned short member2;

        /*Test2()
        {
            member1 = member2 = 0;
        }*/
    } m_str1;

    char             m_pp1;
    char             m_pp2;
};

TestClass::TestClass() :
    m_str1(),
    m_data(0)
{

}

TestClass::TestClass(char max) :
    m_data(0),
    m_pp2(),
    m_str1(),
    m_pp1(max)
{
    for (char i = 0; i < m_pp1; ++i)
    {
        m_data++;
    }
}

void TestClass::fun1()
{
    while (m_str1.member1 && m_str1.member2)
    {
        m_data++;
    }
}

void tester();
void tester()
{
    TestClass tTemp1(9);

    TestClass tTemp2;

    TestClass *tTemp3 = new TestClass;

    TestClass *tTemp4 = new TestClass(42);

    tTemp1.fun1();
    tTemp2.fun1();
    tTemp3->fun1();
    tTemp4->fun1();
}

Outcomes