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
}