I have the following snippet of code:
struct Node
{
Node* left;
Node* right;
string data;
};
void test()
{
Node thing;
thing.data = "h";
thing.left = NULL;
thing.right = NULL;
cout<< "Thing data = " << thing.data << endl;
Node* thing2;
thing2->data = "f";
thing2->left = NULL;
thing2->right = NULL;
cout<< "Thing2 data = " << thing2->data << endl;
}
The problem I’m having is that thing2->data = “f” is producing a segmentation fault during runtime. I’ve run the program through GDB and get this error, but I can’t figure out what it means:
Reading symbols for shared libraries ++. done
Thing data = hProgram received signal
EXC_BAD_ACCESS, Could not access
memory. Reason: 13 at address:
0x0000000000000000 0x00007fff874d59a3
in std::string::assign ()
Any help would be great. Thanks!
thing2 is a non initialized pointer.
It doesn’t point to a valid Node object.
You should allocate it:
or make it point to a valid Node object: