1

I've seen code where data structure of such type:

struct TestStruct
{
    int a;
    std::string b;
};

although it contains std:string in it, gets initialized as:

TestStruct t;
memset(&t, 0, sizeof(TestStruct));

to some my knowledge and other posts I have read, above initialization should have caused program crash (due to the fact that struct TestStruct containst std::string), but the application seems to not crash, any ideas why? Thanks.

pseudonym_127
  • 357
  • 7
  • 16

5 Answers5

3

You invoke UB with that, crashing is only one of many incarnations of UB. It may crash later at some (possibly unrelated) point, or not at all, seeming to work.

What might happen in your case is that std::string is internally just a pointer to some real string allocation, which is nullptr anyways already. But thats just a guess, depends on your implementation, possibly the moonphase, and is not to be relied upon.

PlasmaHH
  • 15,673
  • 5
  • 44
  • 57
3

The crash might happen because you change an object's internal state. Initialize like this instead:

TestStruct t = { };

Easier on your fingers and also works in C.

user1095108
  • 14,119
  • 9
  • 58
  • 116
1

It does crash on my 64bit ubuntu when calling string destructor:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b78bca in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7b78bca in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00007ffff7b78c13 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x0000000000400808 in TestStruct::~TestStruct() ()
#3  0x0000000000400770 in main ()
Tsar Ioann
  • 444
  • 4
  • 9
0

Initialization it self may not cause crasches, but using such object is dangerous

Jeka
  • 1,364
  • 17
  • 33
  • because of std::string b; inside the struct? what if I had only basic types in it, then it would be ok right? – pseudonym_127 Jun 05 '13 at 08:13
  • Already the memset invokes UB and is dangerous. – PlasmaHH Jun 05 '13 at 08:16
  • 2
    @pseudonym_127 "what if I had only basic types in it, then it would be ok right?" The term used is POD ("plain old data"), and yes, in that case it would be okay. –  Jun 05 '13 at 08:24
0

The memset would be setting all the data in the std::string object to zero which will, almost certainly, give you undefined behaviour which, of course, can include not crashing. The stl that I use (StlPort) has a fancy string class that allocates short strings on the stack and longer strings on the heap. That probably would not crash immediately on a zero memset but, really, there is no point in speculating as the behaviour is certainly not portable.

You'd be better off writing a default constructor for the struct:

TestStruct() : a(0){}

which explicitly zeros the memory for the int and relies on the default constructor for the string.

(Remember that classes and structures are equivalent apart from the default access.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483