MW compiler class member (struct) initialization

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MW compiler class member (struct) initialization

Jump to solution
1,024 Views
juhalaukkanen
Contributor III

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();

}

Labels (2)
0 Kudos
Reply
1 Solution
819 Views
marius_grigoras
NXP Employee
NXP Employee

Hi,

Thank you for your research, you're not missing any MW compiler flag and indeed, there is a bug in our MW compiler. I'll discuss with our compiler team and will be fixed.

Regards,

Marius

View solution in original post

1 Reply
820 Views
marius_grigoras
NXP Employee
NXP Employee

Hi,

Thank you for your research, you're not missing any MW compiler flag and indeed, there is a bug in our MW compiler. I'll discuss with our compiler team and will be fixed.

Regards,

Marius