I am using g++ with the c++ 17 standard
In the accepted answer for question memset() or value initialization to zero out a struct? it states that
struct TestStruct
{
int a;
std::string b;
};
TestStruct t = {}; // OK
{
TestStruct t1;
memset(&t1, 0, sizeof t1); // ruins member 'b' of our struct
} // Application crashes here
whereas in my own testing the program does not crash at this point.
Example A:
struct TestStruct
{
int a;
std::string b;
};
TestStruct t1;
memset(&t1, 0, sizeof t1);
t1.b = "before";
std::cout << t1.b << std::endl;
std::cout << "after";
This prints
before
after
Example B:
struct TestStruct
{
int a;
std::string b;
};
TestStruct t1;
memset(&t1, 0, sizeof t1);
t1.b = "";
std::cout << t1.b << std::endl;
std::cout << "after";
This prints nothing as the program crashes directly after t1.b = "";
For the sake of thoroughness I also tested with t1 going out of scope as in the referenced question
struct TestStruct
{
int a;
std::string b;
};
{
TestStruct t1;
memset(&t1, 0, sizeof t1);
t1.b = "before";
std::cout << t1.b << std::endl;
std::cout << "after" << std::endl;
}
std::cout << "eventually";
prints
before
after
eventually
My main question is, why does example B crash where example A does not?