Assignment of string to structure element

后端 未结 5 1952
长情又很酷
长情又很酷 2021-01-22 22:50

My program crashes when I try to assign a string value to a member of a structure. My suspicion is that the member (of type string) within the structure was never properly alloc

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-22 23:43

    So before I get to answering your question I just wanted to say that you should not use Malloc in c++ unless you are forced to. This answer explains why fairly well.

    In what cases do I use malloc vs new?

    With that said changing this line

    DataRow* node = (DataRow*)malloc(sizeof(DataRow));
    

    To this

    DataRow* node = new DataRow;
    

    Will fix your problem

提交回复
热议问题