What is the purpose of “::delete” in C++?

I'm currently looking at C++ code that uses ::delete to delete a pointer.

A meaningless example of this is:

void DoWork(ExampleClass* ptr)
{
::delete ptr;
}


What is the purpose of using the delete keyword in this way?

In some cases, the operator delete might be redefined -actually overloaded- (for example, your Class might define it and also define operator new). By coding ::delete you say that you are using the standard, "predefined", deletion operator.

A typical use case for redefining both operator new and operator delete in some Class: you want to keep a hidden global set of all pointers created by your Class::operator new and deleted by your Class::operator delete. But the implementation of your delete will remove that pointer from the global set before calling the global ::delete

This is using the delete expression, but with the optional :: prefix.


Syntax

::(optional) delete expression (1)

...

Destroys object(s) previously allocated by the new expression and
releases obtained memory area.


Using the :: prefix will affect lookup:


The deallocation function's name is looked up in the scope of the
dynamic type of the object pointed to by expression, which means
class-specific deallocation functions, if present, are found before
the global ones.

If :: is present in the delete expression, only the global namespace is examined by this lookup.


delete in C++ is an operator in the same way = is. It therefore can be re-implemented. And just like = , the re-implementation is specific to the class to which it is referring. Adding the :: makes sure that the delete operator that we are calling is the global one and not one specific to a given class.

This can be useful when you re-implement the delete for a specific class and then want to refer to the real one.

::delete is synonymous with delete
:: is for scope. E.g Classname:: means anything within that class' scope. In this instance :: means anything that is scoped by default (e.g anything you don't need to include a namespace for)

delete frees a pointer from the heap. Not doing this will mean when the program exits, that chunk of memory is still counted as being used by the OS (the OS will usually clean this up but it's bad practice to not free your pointers)

So usually

int* intpointer = new int(5);
//do something with intpointer
delete intpointer

Using ::delete has quite a few uses in a large scale program, in your example it doesn't make a lot of sense but using it in a larger context does enable the following:


Global variable access, so if you have local and global x. Using ::x refers to the global and not the local
Define a function outside of a class, not sure why you would want or need to do that but the functionality is there
Access static class variables
Distinguish between the same variable name between two or more classes in the case of multiple inheritance


The link below has a pretty good explanation and examples to mull over.

Source: https://www.geeksforgeeks.org/scope-resolution-operator-in-c/


Class-specific overloads
Deallocation functions (17-24) may be defined as static member functions of a class. These deallocation functions, if provided, are
called by delete-expressions when deleting objects (17,19,21) and
arrays (18,20,22) of this class, unless the delete expression used the
form ::delete which bypasses class-scope lookup. The keyword static is
optional for these function declarations: whether the keyword is used
or not, the deallocation function is always a static member function.


This implies that ::delete is not equal to delete. The difference here is delete can be overridden and is specific to your object/class. ::delete is the global

I know some cases you should NOT use "::delete", it will not work

Basically, when deallocating, the destructor looked by the compiler is the most local to the global - if a destructor is not found in the current scope, it looks one level above until it reaches the global (that is always there). Using ::, changes the starting scope used by the compiler to be the global one.

See more here. There is a whole section for it.

It is a possibility that your class has overloaded new and delete.
So ::delete indicates that we are referring to global scope and not the ones that have been over-ridden in the current class.

Useful link: What are "::operator new" and "::operator delete"?

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

Chart JS +ng2-charts not working on Angular+2