简体   繁体   中英

What happens when I call “delete” on an uninitialized pointer in C++?

Let's say I declare a pointer of char, and call delete on it without having called new. Can this cause a problem?

char* myptr;

if (condition)
    //do something involving myptr = new char[SIZE];
else
    //do something that doesnt involve myptr

//do more stuff
delete[] myptr;

I don't delete myptr under the if because another pointer in //do more stuff can point to it if condition was true. Obviously this works fine if condition was true because a "new" was called on myptr . Is deleting myptr bad if I entered the else condition, where myptr is unused?

It's undefined behavior . The pointer must come from new or must be a null pointer.

Standard (N3797) 5.3.5 [expr.delete] /2

If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In the first alternative (delete object), the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject (1.8) representing a base class of such an object (Clause 10). If not, the behavior is undefined.
[...]

The part left out at the bottom is the same for delete [] .

Deleting is only valid for

  • null pointers
  • pointers you got from new
  • or a base class pointer pointing to the above.

Null pointers:

To follow up on null pointers, calling delete on a null pointer is a noop and the pointer is still a null pointer afterwards (no need to reassign nullptr to it). This is guaranteed at least for the standard delete and deallocation functions. If you define custom ones you should also handle this properly.

Standard 5.3.5 [expr.delete] /7

If the value of the operand of the delete-expression is not a null pointer value, then:

  • [...]

Otherwise, it is unspecified whether the deallocation function will be called. [ Note: The deallocation function is called regardless of whether the destructor for the object or some element of the array throws an exception. — end note ]

So delete might do nothing at all or might call the deallocation function with the null pointer. Lets look at that function next:

Standard 3.7.4.2 [basic.stc.dynamic.deallocation] /3

If a deallocation function terminates by throwing an exception, the behavior is undefined. The value of the first argument supplied to a deallocation function may be a null pointer value; if so, and if the deallocation function is one supplied in the standard library, the call has no effect.
[...]

It is explicitly mentioned that it got no effect.

Note: If you define your own custom deallocation functions you should make sure you handle it the same way. Not handling null pointers properly would be evil.

If the pointer was initialised to nullptr , then deleting it would be safe.

As it stands, myptr is not initialised and attempting to delete it's value will likely cause a segmentation fault.

char* myptr = nullptr Will solve this issue in c++11 or later, or use 0 or NULL for c++98 or earlier.

It will be an undefined behavior . delete must be called on the pointer allocated memory using new.

However you can call delete on null pointer safely.

you can write like

char* myptr=NULL;

if (condition)
    //do something involving myptr = new char[SIZE];
else
    //do something that doesnt involve myptr

//do more stuff
if(myptr)
  delete[] myptr;

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