指针在删除它并在 C++ 中再次分配新内存后是否获得相同的内存地址?
Does a pointer get the same memory address after deleting it and allocating new memory again in c++?
Here's the thing I wanted to clarify. I have another function to display links. After calling that display function I got a garbage value on my console.
But when I commented "delete temp" statement it worked fine, I got results as expected. Please do help. Thanks.
void MyLinkedList::insertFirst(double data){
MyLink *temp = new MyLink(data);
temp->next = first;
first = temp;
delete temp;
}
delete运算符不是删除指针本身,而是删除指针指向的内存
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.
Here's the thing I wanted to clarify. I have another function to display links. After calling that display function I got a garbage value on my console. But when I commented "delete temp" statement it worked fine, I got results as expected. Please do help. Thanks.
void MyLinkedList::insertFirst(double data){
MyLink *temp = new MyLink(data);
temp->next = first;
first = temp;
delete temp;
}
delete运算符不是删除指针本身,而是删除指针指向的内存
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.