I have a very simple piece of code with 2 structures and one dynamic allocation. The program crashes on the “nume” initialization.
typedef struct{
int key;
string name;
} TElement;
typedef struct nod {
int cheie;
string nume;
struct nod *stg, *dr;
} NOD;
when i try to do this
void ABC::inserare_element(TElement e){
NOD *p, *q;
int n;
/* construction nod p*/
n=sizeof (NOD);
p=(NOD*)malloc(n);
p->cheie = e.key;
p->nume = e.name; // on this line the program crashes
Thanks
malloc()will not invoke the constructor ofNOD, meaning the constructor ofnumewill not have been invoked resulting in an attempt to usestd::string::operator=on an unconstructed/uninitializedstd::string: usenew.