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.
Related
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:
delete vs delete[] operators in C++
(7 answers)
Closed 5 years ago.
When we deallocate the heap memory occupied by an array, I have a little confusion regarding the syntax
int *p = new int[5];
Now for deallocating, which one is correct from the following:
delete p;
OR
delete[ ] p;
The latter seems to be more correct. But it confuses me, I don't understand that how would it know that how much memory the array exists on. I mean, we are only giving it the starting address of the array(through p). So, beginning from the starting address how will the compiler know that till where does it have to deallocate, and when to stop the deallocation.
Your second syntax is correct, and the compiler knows the size of the array since it took note of it when you allocated the array.This is usually stored in a piece of memory just before the memory that you allocated for the array. That way when it's time to free the memory, the deallocator knows exactly how much memory to free, by checking this memory.
In the below statement you are creating memory dynamically for int array of 5 integer
int *p = new int[5];
Its looks like below
p = operator new [] (20); /** p points to first 20 bytes from starting address **/
To free or de-allocating p you should use
delete [] p;
It's looks like below
operator delete [] (p); /* It free frees memory of 20 bytes from where p is pointing */
Note : if you using only delete p then it won't free whole 20 bytes. delete p internally converts as operator delete (p);
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:
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)