简体   繁体   English

将指针指定为NULL时会发生什么?

[英]What will happen when we assign a pointer NULL?

I have a pointer which points to some data. 我有一个指向某些数据的指针。 Now I have created a similar pointer. 现在我创建了一个类似的指针。 Now this new pointer is assigned to the old one. 现在,这个新指针被分配给旧指针。 If i delete the old one what will happen? 如果我删除旧的将会发生什么?

A* a = new A();
A* b = a;
a = NULL;

what will happen to both "a" and "b"? “a”和“b”会发生什么?

Moreover, if I do following things: 而且,如果我做了以下事情:

A* a = new A();
A* b = a;
delete a;

And also I want to know what happens to pointer when we assign is NULL. 而且我想知道当我们分配NULL时,指针会发生什么。

A* a = new A();
a = NULL;

does a still points to some memory or it points to nothing? 一个仍然指向一些记忆或它指向什么?

A* a = new A();
A* b = a;
a = NULL;

After this, b still points to the object allocated in the first line. 在此之后, b仍然指向第一行中分配的对象。 a now points to nothing. a现在指向什么。 You can still "use" the object via the b pointer, and you can delete the object via delete b; 你仍然可以通过b指针“使用”对象,你可以通过delete b;删除对象delete b; .
You can delete a; 你可以delete a; here - it will have no effect (since a is NULL), but it is safe and will not lead to undefined behavior. 这里 - 它将没有任何效果(因为a是NULL),但它是安全的,不会导致未定义的行为。

A* a = new A();
A* b = a;
delete a;

After the delete , the object that a and b used to point to no longer exists. delete后, ab用于指向的对象不再存在。 a and b are therefore no longer valid pointers, and you can't do anything with them that uses their value. 因此, ab不再是有效的指针,你不能对使用它们的值做任何事情。 In particular, trying to derefence either pointer will lead to undefined behavior. 特别是,尝试拒绝指针将导致未定义的行为。 (But you can reuse the variables, if you make them point to a valid object.) (但是,如果使它们指向有效对象,则可以重用这些变量。)

A* a = new A();
a = NULL;

You have just leaked the object created in the first line. 您刚刚泄露了第一行中创建的对象。 a no longer points to anything, and you don't have a handle on that object so you can't delete it. a不再指向任何内容,并且您没有该对象的句柄,因此您无法删除它。 So, it's a plain old memory leak. 所以,这是一个普通的旧内存泄漏。
(As in the first case, you can delete a; after the a = NULL; line, but it will have no effect, the memory is still leaked.) (与第一种情况一样,你可以delete a;a = NULL;行,但它没有效果,内存仍然被泄露。)

 A* a = new A(); A* b = a; a = NULL; 

what will happen to both "a" and "b"? “a”和“b”会发生什么?

a will be NULL, and b is unmodified a将为NULL,并且b未经修改

 A* a = new A(); A* b = a; delete a; 

*a has been deleted, and b became invalid (you must not dereference b ( *b ) any more past that point). *a已被删除, b变为无效(此时不得再取消引用b( *b ))。

And also I want to know what happens to pointer when we assign is NULL. 而且我想知道当我们分配NULL时,指针会发生什么。

 A* a = new A(); a = NULL; 

*a is not deleted, so the memory is leaked. *a 删除,因此内存泄露。 The memory cannot be freed anymore (unless you had a copy of the pointer somewhere else, but the code doesn't show that). 内存不能再被释放(除非你在其他地方有一个指针的副本,但代码没有显示)。

A* a = new A();
A* b = a;
a = NULL;

b keeps on pointing to A . b继续指向A a points to address 0x0, dereferencing it will cause a segfault. a指向0x0的地址,解除引用会导致段错误。

A* a = new A();
A* b = a;
delete a;

both a and b points to the address space where A once was instanciated. ab指向A曾经被实例化的地址空间。 Now that A is deleted, you should neither dereference a nor b as it might segfault. 现在, A被删除,你应该既不提领a ,也不b ,因为它可能会出现段错误。

A* a = new A();
a = NULL;

Now a points to address 0x0 and A is lost in cyberspace. 现在,在网络空间中丢失了地址0x0和A a点。 You leaked memory. 你泄露了记忆。


a bit late on that one. 那个晚了一点。 again. 再次。

@apoorva "what if i assign any new pointer to a and then make a as null. and then delete a." @apoorva“如果我将任何新指针指定给a,然后将其设为null,然后删除a。” it will have no effet..ie you wont free the memory occupied by new A(),simply because a=null and you are trying to delete a; 它将没有任何效果..你不会释放新A()所占用的内存,只是因为a = null并且你试图删除一个; which now contains NULL. 现在包含NULL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们将引用分配给变量时会发生什么? - What happen when we assign a reference to a variable? 如果将右值分配给该类的对象,该类的指针成员会怎样? - What happen to pointer members of a class if I assign a rvalue to an object of that? 指针初始化:何时将NULL分配给初始化的指针? - Pointer Initialization : When to assign NULL to the initialized pointer? 在我们将另一个取消引用的指针分配给一个取消引用的指针后会发生什么? - What happens after we assign to a dereferenced pointer an another dereferenced pointer? 如果我们将一个动态分配的指针分配给另一个指针,会发生什么? - If we assign a dynamically allocated pointer to another pointer, what happens? 当我们在C ++中将常量整数引用分配给整数指针时,行为是什么? - What is the behaviour when we assign constant integer reference to a integer pointer in c++? 当我给char分配一个short时会发生什么类型的转换? - What type conversion happen when I assign a short to a char? 将常量字符串分配给常量字符指针时会发生什么? - what happen when constant string assigned to constant character pointer? 当我们在派生的 class 中编写空的复制构造函数时会发生什么? - What happen when we write empty copy constructor in derived class? 如果我们声明一个对象并将其分配给同一类的另一个对象,将会发生什么 - What will happen if we declare one object and assign it to another object of same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM