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.
delete[] vs delete in a for loop
Both versions of new
and delete
each have two tasks: allocation/deallocation and construction/destruction.
new
will allocate memory and call a constructor.delete
will call a deconstructor and deallocate memory.new []
allocates single chunk of memory and then calls a constructor possibly several times.delete []
calls a deconstructor possibly several times and then deallocates a single chunk of memory.
So using delete
multiple times means deallocating multiple chunks of memory whereas using delete[]
will deallocate a single chunk of memory; using delete
multiple times is not equivalent to using delete []
.
delete vs delete []
If you use new[]
you must balance it with delete[]
.
If you use new
you must balance it with delete
.
Mixing them will cause memory leaks / program crashes (technically undefined behaviour). (In your example, C++ is recording internally that you've created 4 int
s; calling delete[]
allows C++ to access this record. Calling delete
does not).
delete vs delete[]
From the standard (5.3.5/2) :
In the first alternative (delete
object), the value of the operand of
delete shall be a pointer to a
non-array object or a pointer to a
sub-object (1.8) representing a base
class of such an object (clause 10).
If not, the behavior is undefined.In the second alternative (delete
array), the value of the operand of
delete shall be the pointer value
which resulted from a previous array
new-expression. If not, the
behavior is undefined.
So no : they are in no way equivalent !
Proper use of delete vs delete[ ] with respect to char * in C++
Your both first code example is wrong.
char * str = new char;
cin >> str;
You've only allocated memory for a single character. If you read anything other than an empty string, you'll write into unallocated memory and will have undefined behaviour.
if I then delete str, does it delete the array completely?
It will only delete the one character that you allocated. The rest of the string that you wrote in unallocated memory won't be directly affected by the delete
. It's not a memory leak, it's a memory corruption.
vs.
char * str = new char[];
This is not legal c++. Array size must be specified.
EDIT: After your fix, the second code is correct as long as you read a string of 29 characters or shorter. If you read a longer string, you'll get undefined behaviour again.
But what if new itself allocates contiguous memory locations?
It doesn't. new
(as opposed to new[]
) allocates and constructs exactly one object. And delete
destroys and deallocates exactly one object.
TLDR Neither program has memory leaks but the first one has undefined behaviour due to memory corruption.
C/C++ delete vs delete[]
delete: This frees the memory currently allocated by the pointer the delete is performed upon. It only deletes the memory pointed to by the first variable.
delete []: This frees the memory allocated for the whole array. An array consists of several variables - delete frees memory only allocated for the first variable, while delete [] does the whole thing.
A good way to think of it is considering delete as an instruction while delete [] as a loop; where the array is looped through and delete is called individually on each variable in the array. This is NOT how it works in reality (the real workings are a bit more complicated), but is a good way to understand the diff.
The destructor is called on all objects, because in some cases such as in the case of an array of objects that contain pointers, calling the destructor on only the first element doesn't free all memory.
Delete vs Delete[] in a custom operator in C++ (different than common delete vs delete[] questions)
First of all, this is not an allocator, or your name convention is wrong. In C++, an allocator allocates memory for a given type, not for raw bytes. More on that - https://en.cppreference.com/w/cpp/memory/allocator
Second, don't mix malloc()
/delete
and new
/free()
. malloc()
should be used with free()
, new
with delete
, not the other way around. More on that - https://stackoverflow.com/a/20488420/12979560
So, all you need to do is to replace malloc()
with new
. Also, when you use new
, C++ will throw std::bad_alloc
in case if it can't allocate the requested memory.
Delete vs operator delete (and void pointer)
delete ptr
will do overload resolution for operator delete
, so it may not call the global ::operator delete
But otherwise, yes. The delete
operator calls the relevant destructor, if any, and then calls operator delete
.
Related Topics
Getting Size of Array from Pointer C++
Dynamic_Cast and Static_Cast in C++
Where Exactly Does C++ Standard Say Dereferencing an Uninitialized Pointer Is Undefined Behavior
Why Does C++ Allow Us to Surround the Variable Name in Parentheses When Declaring a Variable
Cmake Cannot Find Library Using "Link_Directories"
Generating Combinations in C++
Difference Between String and Char[] Types in C++
C++ Syntax For Explicit Specialization of a Template Function in a Template Class
How to Use Stringstream to Separate Comma Separated Strings
Is Std::Vector or Boost::Vector Thread Safe
No Matching Function - Ifstream Open()
Examples of When a Bitwise Swap() Is a Bad Idea
How to Link Opencv in Qtcreator and Use Qt Library