Segmentation fault (core dumped) when I delete pointer

前端 未结 5 1555
北恋
北恋 2021-01-27 04:21

I am trying to remove duplicates from a linked list, and encountered a problem, which is probably obvious and straightforward but I haven\'t used C++ in many years

5条回答
  •  太阳男子
    2021-01-27 04:56

    You cannot use free() if you allocated memory with new

    You can use malloc() with free() or new with delete. You also should not use malloc() and free() with objects as they do not call the objects constructor and destructor unlike new and delete

    So since you allocated the nodes with new we can change

    free(temp); //Here is my problem!!!
    

    to

    delete temp;
    

提交回复
热议问题