This question already has answers here:
delete vs delete[] operators in C++
(7 answers)
Closed 4 years ago.
This may sound a bit strange, but if I have the code uses delete [] as follows:
int main()
{
int *test = new int(5);
delete [] test //Does this work?
// delete test (This is the standard syntax)
}
Of course, I tried to compile and run, and delete [] didn't return any errors. According to http://www.cplusplus.com/reference/new/operator%20delete[]/, delete[] operator first calls the appropriate destructors for each element in the array (if these are of a class type), and then calls an array deallocation function. I'm not 100% sure what array deallocation function is, but I presume this will not cause memory leak?
Running delete [] for a pointer which was allocated with new is an undefined behavior. It may work, or may not, or may stop working at any moment, or may ruin your hardware. You have no guarantee.
Related
This question already has answers here:
delete vs delete[] operators in C++
(7 answers)
Closed 3 years ago.
I'm writing some memory management code in C++, and at first I use char *ptr = new char[1024] instead of void *malloc(unsigned int size) to get a buffer, after this, there's no concept of array in my code, all the operation is done by pointers.
But when I want to free them, I got some worries. As far as I know, C++ asked programmers to use delete[] when acquire memory by using new *type*[], but at this moment I only got a pointer (which is ptr in the case above). Before coding this I think why using delete[] is to call the destructors on each element. But I'm not sure if there's a difference between delete and delete[] on a pod array.
So is it safe to use delete ptr on a pod array?
new comes with delete. new [] comes with delete []. You have no other option:
Called by delete-expressions to deallocate storage previously
allocated for a single object. The behavior of the standard library
implementation of this function is undefined unless ptr is a null
pointer or is a pointer previously obtained from the standard library
implementation of operator new(size_t) or operator new(size_t,
std::nothrow_t).
Mixing these operators results in undefined behavior.
This question already has answers here:
How does delete[] "know" the size of the operand array?
(9 answers)
Closed 3 years ago.
How does delete operator work in C++?
int *ptr = new int[2];
delete[] ptr;
How does delete operator know the amount of memory allocated, since ptr is just a int pointer and increment-ing(++) it would simply make it point to the next location in the continuously allocated structure.
Allocators typically hide allocation information just before the pointer in question. The allocation includes that space, but the pointer is moved after it so you don't access/modify it. This is part of why writing to a pointer at a negative index breaks things so badly.
As noted in the comments, your code is broken as written, since you used delete ptr;, not delete[] ptr;; only the latter knows to look for the information needed to destruct the whole array, not just a single element.
This question already has answers here:
how to properly delete a pointer to array
(6 answers)
Closed 8 years ago.
Is this code legal?
int * a = new int[1];
delete a;
Or this one?
int * a = new int;
delete [] a;
Obviously, this doesn't seem right and should be discouraged at the very least but will it cause any actual problems (memory leaks and whatnot)?
Matching combination must be:-
new; delete;
new[]; delete[];
If you mix these up you would get undefined behaviour. I have seen code blowing up due to this.
No. If you new you should delete. And if you new[] you should delete[].
Mixing scalar and array (de)allocation operators leads to undefined behaviour. A defined difference between delete and delete[] is that the latter calls the destructors for all elements of the array.
http://www.cplusplus.com/reference/new/operator%20delete[]/
This question already has answers here:
Can I delete[] a pointer that points into an allocated array, but not to the start of it?
(7 answers)
Closed 9 years ago.
What would happen in the following code?
int *p1 = new int[100];
int *p2 = &p1[50];
delete [] p2;
I've heard that some implementations of new store the size of the array in the (-1)th array index, but then wouldn't things go horribly wrong in the above?
Things would definitely go wrong!
The delete [] operator is defined to only work on proper array pointers. And by proper I mean it must receive a pointer that was previously initialized to point to a location in memory where an array was created with the new operator.
You should also never mix and match new/delete and malloc/free. As a rule always delete memory that has been allocated with new, and free memory that has been allocated with malloc (and derivatives)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Undefined, unspecified and implementation-defined behavior
I know calling delete on same object is disastrous. But that is true as long as the memory is not reallocated for some other object before the second call to delete. But, even doing the below is wrong? If I remove the cout, the code is not dumping core.
int main()
{
A *a1 = new A();
delete a1;
cout<<a1<<endl;
delete a1;
}
See What happens in a double delete?:
Yes, it is very wrong.
The big problem is that the behavior is undefined. So it you might get away with it in one situation on one compiler, but in general it tends to cause a crash.