C++ Delete Syntax

C++ delete syntax

This is undoubtedly an error. The comma here is the comma operator, not a separator. Only the first pointer, ptr1 is deleted.

The second pointer, ptr2, is just a do-nothing expression.

The delete operator has higher precedence than the , operator, so the expression is parsed as if it were written:

(delete ptr1) , (ptr2)

and not as if it were written:

delete (ptr1 , ptr2)

If , had higher precedence than delete, then only the second pointer would be deleted.

Why does the delete[] syntax exist in C++?

Objects in C++ often have destructors that need to run at the end of their lifetime. delete[] makes sure the destructors of each element of the array are called. But doing this has unspecified overhead, while delete does not. This is why there are two forms of delete expressions. One for arrays, which pays the overhead and one for single objects which does not.

In order to only have one version, an implementation would need a mechanism for tracking extra information about every pointer. But one of the founding principles of C++ is that the user shouldn't be forced to pay a cost that they don't absolutely have to.

Always delete what you new and always delete[] what you new[]. But in modern C++, new and new[] are generally not used anymore. Use std::make_unique, std::make_shared, std::vector or other more expressive and safer alternatives.

What's the equivalent of new/delete of C++ in C?

There's no new/delete expression in C.

The closest equivalent are the malloc and free functions, if you ignore the constructors/destructors and type safety.

#include <stdlib.h>

int* p = malloc(sizeof(*p)); // int* p = new int;
...
free(p); // delete p;

int* a = malloc(12*sizeof(*a)); // int* a = new int[12];
...
free(a); // delete[] a;

C++ array delete operator syntax

You can check this MSDN link: delete[N] operator. The value is ignored.

EDIT
I tried this sample code on VC9:

int test()
{
std::cout<<"Test!!\n";
return 10;
}

int main()
{
int* p = new int[10];
delete[test()] p;
return 0;
};

Output is: Test!!

So the expression is evaluated but the return value is ignored.
I am surprised to say the least, I can't think of a scenario why this is required.

c++ syntax: default and delete modifiers

Special member functions can now be defaulted or deleted.

A deleted member function still takes part in overload resolution, but if it gets chosen, the program is ill-formed and compilation stops with a useful diagnostic. This is The Right Way to write things like non-copyable classes, and the user gets a proper error message.

A defaulted member function "does what it should", e.g. a defaulted default constructor default-initializes all bases and members and has empty body; a defaulted copy constructor copies each base and member object, and a defaulted assignment operator assigns each base and member object. If any of those operations aren't allowed (e.g. you have reference members), then the defaulted member function is defined as deleted.

Note that your first declaration-definition A() = default; makes the constructor A::A() user-declared but not user-defined; this is important for the classification of A, e.g. whether it is POD. (And notice that this is different from struct A { A(); }; A::A() = default; which is user-defined.)

Another nice consequence is the clarification of implicitly generated things: If you don't write certain functions yourself at all (like copy constructors), one gets implicitly declared for you. When the implicitly-declared one is odr-used, it gets implicitly defined as defaulted, and thus if it's not possible (e.g. if the class has non-copyable members), it actually gets implicitly defined as deleted. So that's generally a neat way of propagating things like non-copyability and non-assignability, at least in terms of the language and the consequent diagnostics.

delete vs delete[] operators in C++

The delete operator deallocates memory and calls the destructor for a single object created with new.

The delete [] operator deallocates memory and calls destructors for an array of objects created with new [].

Using delete on a pointer returned by new [] or delete [] on a pointer returned by new results in undefined behavior.

Meaning of = delete after function declaration

Deleting a function is a C++11 feature:

The common idiom of "prohibiting copying" can now be expressed
directly:

class X {
// ...
X& operator=(const X&) = delete; // Disallow copying
X(const X&) = delete;
};

[...]

The "delete" mechanism can be used for any function. For example, we
can eliminate an undesired conversion like this:

struct Z {
// ...

Z(long long); // can initialize with a long long
Z(long) = delete; // but not anything less
};

Why is there a delete[] in C++?

Typically, for non-POD classes, a delete[] expression must call destructors on a variable number of class instances that cannot be determined at compile time. The compiler typically has to implement some run time "magic" that can be used to determine the correct number of objects to destroy.

A delete expression doesn't have to worry about this, it simply has to destroy the one object that the supplied pointer is pointing to. Because of this, it can have a more efficient implementation.

By splitting up delete and delete[], delete can be implemented without the overhead needed to correctly implement delete[] as well.



Related Topics



Leave a reply



Submit