C++ hanging pointer: some understanding of new and delete

This is my first technical blog, simple and short.

Not much to say, directly on the code:

#include <iostream>
using namespace std;
class test{
	private:
	int myprivateNum;
	public:
	test():myprivateNum(0){
		cout<<"the test begin with "<<myprivateNum<<endl;
	}
	test(int i):myprivateNum(i){
		cout<<"the test begin with "<<myprivateNum<<endl;
	}
	~test(){
		cout<<"the test is done!\n";
	}
	void printsomething()
	{
		cout << "something\n";
	}
	int getMyNum(){
		return myprivateNum;
	}
	void setMyNum(int num){
		myprivateNum = num;
	}
};
int main(int argc, char *argv[])
{
	test* myTest         = (test*)new test(3);    //可以显示调用含参构造函数,注意区别 
	test* myTestArray    = (test*)new test[10];   //动态生成数组时,会调用默认构造函数(无参),不能自己显示调用想要的含参构造函数哦~ 
	(*myTestArray).printsomething();
	(myTestArray+1)->printsomething();            //验证返回数组的头指针      
	test* myTestNull     = NULL;
	myTestNull->printsomething(); 		      //不会出现错误,因为是静态绑定(考虑到缓存性能,编译期已经生成),所以根本没有用这个指针哦~ 函数会默认把调用它的指针当作参数传入 
	//myTestNull->getMyNum();	              //会出现指针为空的错误
	
	
	 
	cout<<myTest->getMyNum()<<endl;             //正常输出 
	/*
	//delete后如果未清空指针,那么这段内存在未被重新使用之前也许可以正常访问;但是这段内存同样是可分配使用的,
	//这就导致该指针访问相应内存时不知道这段内存存了啥二进制数据(未定义的行为); 
	//所以称为悬挂指针,能着地,但底下已被架空,说不定你想要着的自己家的沙发,结果落到了别人家厕所坑。 
	*/
	delete myTest;
	cout<<myTest->getMyNum()<<endl;  //随机数据 
	myTest->setMyNum(25);            //为什么依旧可以访问 
	//myTest               = NULL;   //将指针设为空后,就不能乱调用这段内存啦,所以一般指针delete后没有特殊用处的话会继续设为NULL 
	cout<<myTest->getMyNum()<<endl;  //为什么会正常输出 
	delete myTest;
	cout<<myTest->getMyNum()<<endl;  //为什么会正常输出
	return 0;
}


conclusion of issue:

1) C++ new will return a pointer to the corresponding instance and automatically call the constructor of the instance. If an array is requested dynamically, it will return the head pointer of the array and automatically call the default constructor for each instance of the array.

2) C++ empty class pointers can still call class functions. This is because the non-virtual function of the ++ class is statically bound. It is bound to this class at compile time, and all instances of this class can be shared afterwards. There is no need to rebind the cache, so the instance pointer (this) should not be used at all when calling the function, and there will be no error if it is not empty.

3) After C++ deletes the pointer, it will only mark the memory as usable. The memory may still be accessed normally before it is reused. Delete is not responsible for setting the pointer to null, so the pointer can still access that memory.

4) This leads to the fact that when the pointer accesses the corresponding memory, it is impossible to determine what binary data is stored in this memory (undefined behavior), so it is called a dangling pointer, which can touch the ground, but the bottom has been overhead, maybe you want to The sofa in his own house fell into the toilet pit of someone else's house.

5) Generally, if the pointer is deleted and has no special purpose, it will continue to clear the pointer, that is, set it to NULL, so that it will not read randomly and interfere with the memory that can be used by others. It is clean and comfortable.

Guess you like

Origin blog.csdn.net/g1093896295/article/details/77954654