指针在删除指针并在 c++ 中再次分配新内存后是否获得相同的内存地址?
Does a pointer get the same memory address after deleting it and allocating new memory again in c++?
这是我想澄清的事情。我还有另一个显示链接的功能。调用该显示函数后,我在控制台上得到了一个垃圾值。 但是当我评论"删除临时"语句时,它工作正常,我得到了预期的结果。请帮忙。谢谢。
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.
相关文章:
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- CUDA:统一内存和指针地址的更改
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 将统一的内存指针传递给内核会减慢程序的速度
- malloc - 运行时内存指针类型分配
- 共享内存指针分段错误
- 使用动态分配的内存(指针)
- 与对象一起返回时清除了内存指针
- 在UWP C++项目中,程序集分配的内存指针自动更改为0xffffffff
- 使用注入的 dll 使用内存指针编辑值
- 提升共享内存 - 指针的返回向量
- 监视从应用程序写入指定的内存指针从 DLL 接收
- 非分页内存指针
- Qt读取内存指针值
- 简单的程序帮助-发送动态内存指针到函数