Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning to pointer immediately after deleting

Tags:

c++

pointers

I was wondering if it is safe to do this...

delete p_pointer;
p_pointer = p_otherPointer;

Rather than...

delete p_pointer;
p_pointer = 0;
p_pointer = p_otherPointer;

I would assume so since there aren't any new memory allocations between the deletion and assignment, but I just want to make sure.

like image 343
Anonymous Avatar asked Nov 15 '09 00:11

Anonymous


1 Answers

Yes it is safe. It's useless to set the deleted pointer to NULL if you're about to reassign it anyway. The reason people set deleted pointers to NULL is so they can "mark" it as deleted, so later they can check if it has already been deleted.

like image 142
Charles Salvia Avatar answered Sep 24 '22 23:09

Charles Salvia