删除它并在c ++中再次分配新内存后指针是否获得相同的内存地址?

时间:2016-07-11 15:45:16

标签: c++ pointers visual-c++ memory-management memory-address

  

这是我想澄清的事情。我有另一个显示链接的功能。调用该显示功能后,我的控制台上出现了垃圾值。   但当我评论“删除临时”声明它工作正常,我得到了预期的结果。请帮忙。谢谢。

void MyLinkedList::insertFirst(double data){
    MyLink *temp = new MyLink(data);
    temp->next = first;
    first = temp;
    delete temp;
}

1 个答案:

答案 0 :(得分:1)

删除操作符不是用于删除指针本身,而是用于删除它指向的内存。

 MyLink *temp = new MyLink(data); //allocate space for a MyLink dataType
 first=tmp; //temp still points to the allocated space
 delete temp; //deallocate the memory space pointed by temp which is the same memory space pointed by first.