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.
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.
when we allocate memory dynamically using new operator for a int data type. it makes sense to use delete operator.
For example
if a code would like bellow :
int *p=new int;
delete p;
Here it makes sense to use delete . Here we can think like this that the block, p points ,delete/de-allocate that memory block .
But for the bellow code :
int *p=new int[5];
delete[] p;
How does it make any sense to use delete[] here. I am asking this because p is not the name of the array. Here p is just a simple pointer which is pointing to the first element of the array memory block. Now how does delete[] works to delete the whole array.As here was not mentioned the size of the array. Then how does the statement delete[] p; delete the whole array.
It's up to the compiler to figure out how to do that. One fairly standard way to do this is to store the size of the array in a technical header that precedes the allocated memory block.
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.
This question already has answers here:
How does delete[] "know" the size of the operand array?
(9 answers)
Closed 6 years ago.
Imagine I have a pointer to an array of integers and then I want to delete it like I do below:
int * numbers = new int[10];
delete[] numbers;
How does the delete operator knows where the array numbers ends to free that memory (since C++ does not keep track of the length of the array as far as I know)?
Thanks!
It can do it however it wants. There are two common ways:
The implementation may use an associative array of allocated pointers mapped to their sizes.
The implementation may allocate a few extra bytes at the beginning to store the size and pass a pointer into the block to the caller.
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)