[英]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
后, a
和b
用于指向的对象不再存在。 a
and b
are therefore no longer valid pointers, and you can't do anything with them that uses their value. 因此,
a
和b
不再是有效的指针,你不能对使用它们的值做任何事情。 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. a
和b
指向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.