Questions tagged [delete-operator]

In the C++ programming language, the delete operator calls the destructor of the given argument, and returns memory allocated by new back to the heap.

A call to delete must be made for every call to new to avoid a memory leak. After calling delete the memory object pointed to is invalid and should no longer be used. Many programmers assign 0 (null pointer) to pointers after using delete to help minimize programming errors.

Note, however, that deleting a null pointer has no effect (if the deallocation function is one supplied in the standard library), so it is not necessary to check for a null pointer before calling delete.

Arrays, allocated with new[], must be deallocated with delete[].

1322 questions
1447
votes
29 answers

Deleting array elements in JavaScript - delete vs splice

What is the difference between using the delete operator on the array element as opposed to using the Array.splice method? For example: myArray = ['a', 'b', 'c', 'd']; delete myArray[1]; // or myArray.splice (1, 1); Why even have the splice…
lYriCAlsSH
  • 57,436
  • 10
  • 26
  • 20
345
votes
9 answers

Is it safe to delete a NULL pointer?

Is it safe to delete a NULL pointer? And is it a good coding style?
qiuxiafei
  • 5,827
  • 5
  • 30
  • 43
333
votes
10 answers

Meaning of = delete after function declaration

class my_class { ... my_class(my_class const &) = delete; ... }; What does = delete mean in that context? Are there any other "modifiers" (other than = 0 and = delete)?
Pat O'Keefe
  • 3,333
  • 3
  • 15
  • 5
278
votes
10 answers

Is "delete this" allowed in C++?

Is it allowed to delete this; if the delete-statement is the last statement that will be executed on that instance of the class? Of course I'm sure that the object represented by the this-pointer is newly-created. I'm thinking about something like…
208
votes
7 answers

delete vs delete[] operators in C++

What is the difference between delete and delete[] operators in C++?
shreyasva
  • 13,126
  • 25
  • 78
  • 101
169
votes
11 answers

Does delete on a pointer to a subclass call the base class destructor?

I have an class A which uses a heap memory allocation for one of its fields. Class A is instantiated and stored as a pointer field in another class (class B. When I'm done with an object of class B, I call delete, which I assume calls the…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
148
votes
16 answers

How does delete[] know it's an array?

Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed, void deleteForMe(int* pointer) { delete[] pointer; } The pointer could be all sorts of different things, and so performing an…
GRB
  • 1,811
  • 3
  • 14
  • 10
145
votes
12 answers

Why doesn't delete set the pointer to NULL?

I always wondered why automatic setting of the pointer to NULL after delete is not part of the standard. If this gets taken care of then many of the crashes due to an invalid pointer would not occur. But having said that I can think of couple of…
aJ.
  • 34,624
  • 22
  • 86
  • 128
132
votes
6 answers

What does Visual Studio do with a deleted pointer and why?

A C++ book I have been reading states that when a pointer is deleted using the delete operator the memory at the location it is pointing to is "freed" and it can be overwritten. It also states that the pointer will continue to point to the same…
114
votes
6 answers

Deleting a pointer in C++

Context: I'm trying to wrap my head around pointers, we just saw them a couple of weeks ago in school and while practicing today I ran into a silly? issue, it can be super straightforward to you but I have little to none programming experience. I've…
leopic
  • 2,958
  • 2
  • 27
  • 42
112
votes
2 answers

Is it still safe to delete nullptr in c++0x?

In c++03 it is pretty clear that deleting a null pointer has no effect. Indeed, it is explicitly stated in §5.3.5/2 that: In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. However, in the…
Mankarse
  • 39,818
  • 11
  • 97
  • 141
104
votes
5 answers

Deleting a pointer to const (T const*)

I have a basic question regarding the const pointers. I am not allowed to call any non-const member functions using a const pointer. However, I am allowed to do this on a const pointer: delete p; This will call the destructor of the class which in…
Naveen
  • 74,600
  • 47
  • 176
  • 233
103
votes
4 answers

delete vs delete[]

Possible Duplicate: ( POD )freeing memory : is delete[] equal to delete ? When I was taught C++, this was a long time ago. I was told to never use delete but delete[] as performing delete[] on a single object will be equivalent to delete. Knowing…
onemasse
  • 6,514
  • 8
  • 32
  • 37
102
votes
4 answers

How do you 'realloc' in C++?

How can I realloc in C++? It seems to be missing from the language - there is new and delete but not resize! I need it because as my program reads more data, I need to reallocate the buffer to hold it. I don't think deleteing the old pointer and…
bodacydo
  • 75,521
  • 93
  • 229
  • 319
82
votes
12 answers

Calling delete on variable allocated on the stack

Ignoring programming style and design, is it "safe" to call delete on a variable allocated on the stack? For example: int nAmount; delete &nAmount; or class sample { public: sample(); ~sample() { delete &nAmount;} int nAmount; }
unistudent
  • 889
  • 1
  • 6
  • 5
1
2 3
88 89