指向对象的内存位置的指针已在程序中删除,但仍提供正确答案而不是垃圾值
The pointer to memory location to the object has been deleted in program but still provides the correct answer and not garbage value
指针 *sbi 内存是使用 delete 运算符释放的,但代码仍然正确执行而不提供垃圾值。 是构造函数重新初始化还是代码错误/
#include <iostream>
using namespace std;
class bank
{
private:
int balance;
public:
bank();
void dep(int x);
void with();
~bank();
};
int main()
{
bank *sbi;
sbi = new bank;
sbi->dep(50000);
delete sbi; /// problem is in this section of code
sbi->with();
return 0;
}
bank :: bank()
{
balance=0;
}
void bank::dep(int x)
{
balance=x;
}
void bank::with()
{
cout<<balance<<endl;
}
bank::~bank()
{
cout<<"destroy"<<endl;
}
释放内存位置不会自动用垃圾覆盖它。巧合的是,余额中存储的值仍然相同。
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 欧拉项目#8答案是大以获得有效答案
- 使用Unreal C++获取VR耳机的世界位置/方向
- 写入位置0x0000000C时发生访问冲突
- 首要问题的答案让值班员搞错了
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何使用cudaMallocManaged在指针位置初始化对象?(C++)
- 无法获取webot::PositionSensor对象中位置传感器的值
- 非常量变量只读位置的赋值
- 如何定义更改car类中car位置的方法
- 使用迭代器时如何访问对象在向量中的位置?
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 求最大元素位置的分治算法
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 如何将元素添加到向量的字符串位置
- 指向对象的内存位置的指针已在程序中删除,但仍提供正确答案而不是垃圾值