0

I am running this program on a resource constrained ARM based ThreadX platform. The code is built using GCC g++ compiler. I am seeing a weird crash that I would like to understand more.

I have a class that has a member that is a string. I see that the program crashes when I make the object global(Case 1 below). It crashes in the print statement where I try to find the length of the string object. But, when I create a local object(Case 2 below), the program does not crash. When I create an object using 'new' it does not crash(case 3). I have rough idea of what might be going wrong(constructor invocation not taking place? Global objects are tricky etc.). I would like to understand what might be going wrong under the hood.

class A
{

    public:
        void printS()
        {
            printf(" string sz = %d \r\n", s.size());
        };
        string s;

};

Case 1:

A a;
void main()
{
    /* crashes inside the function prints while accessing the string s */
    a.printS();
}

Case 2:

void main()
{
    A a;  
    a.printS(); //works
}

Case 3:

A* a;
void main()
{   a = new A();
    a->printS(); //works
}
Raghav Navada
  • 307
  • 2
  • 7
  • 1
    Yes, it seems like static object constructors are not called. Perhaps, the problem is in the linker script. Check if it contains lines for condtructors (`.init_array` section). – ReAl Feb 29 '20 at 16:29

1 Answers1

0

Both cases work you just made a typo.

print instead of **printf**
Dressy Fiddle
  • 163
  • 10