简体   繁体   中英

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"?

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.

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. a now points to nothing. You can still "use" the object via the b pointer, and you can delete the object via delete b; .
You can delete a; here - it will have no effect (since a is NULL), but it is safe and will not lead to undefined behavior.

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. a and b are therefore no longer valid pointers, and you can't do anything with them that uses their value. 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. 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.)

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

what will happen to both "a" and "b"?

a will be NULL, and b is unmodified

 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).

And also I want to know what happens to pointer when we assign is NULL.

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

*a is not deleted, so the memory is leaked. 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 . a points to address 0x0, dereferencing it will cause a segfault.

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

both a and b points to the address space where A once was instanciated. Now that A is deleted, you should neither dereference a nor b as it might segfault.

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

Now a points to address 0x0 and A is lost in cyberspace. 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." 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; which now contains NULL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM