I have this (simplified) C++ class:
class node{
public:
int num;
list<int> iplist;
};
Then I dynamically allocate memory for it:
node* node1 = (node*) malloc( sizeof(node) );
It's OK to use node1->num, it's totally fine. However, (node1->iplist).push_back(10)will cause segementation fault. I changed it back to:
node* node1 = new node;
It works fine again including (node1->iplist).push_back(10).
I've Google'd for the answer, realizing maybe this is because malloc() doesn't initialize the element. But, I'm still confused on how to initialize a <list> element when using malloc().