This question already has answers here:
C++ delete - It deletes my objects but I can still access the data?
(13 answers)
Closed 5 years ago.
I made a dynamic array (example):
int *a;
a = new int[3];
a[0] = 10; a[1] = 20; a[2] = 30;
Than I create a vector which stores pointers:
vector<int*> pa;
pa.push_back(&a[0]);
After I deleted (freed) the memory with "delete[] a;", I can still access to the element, that I push_backed. (cout << *pa[0]; output: 10)
Why is this happen? When I "delete[] a", it only deletes the pointer to the elements, but the elements are still accessable?
Your statement about how delete[] works is backwards. It deletes the elements but not the pointer. Continuing to use the pointer after its contents have been deleted, as you're doing, is undefined behavior. This means that literally anything is allowed to happen if you do it, so you shouldn't do it. It's by pure luck that you can still access the elements now, and bad things will happen in the future if you do it.
Related
This question already has answers here:
c++ delete pointer issue, can still access data [closed]
(6 answers)
C++ pointer array is still accessible after delete[] is called [duplicate]
(1 answer)
Closed 1 year ago.
What is the diffrence between
{p = NULL ;}
and
{Delete p; }
When I executed this code:
int *p ;
p = new int;
* p = 4;
cout << *p; // = 4
delete p ;
cout << *p; //
In the last line, I expected to get a garbage value because the pointer is not pointing at the same location anymore, right? But instead I got the same value which is 4, how is this possible?
p = NULL; merely sets p to NULL, but does nothing to the memory that p was pointing at.
delete p; frees the memory that p is pointing at, provided that memory was allocated with new (as it is in your example).
In your example, trying to access *p after calling delete p; is undefined behavior, so anything can happen. p is still pointing at the same address it was before, delete does not alter p itself in any way, but the memory that p is pointing at is no longer valid. You happen to be seeing the old data that was stored in that memory, but only because it simply hasn't been overwritten yet. But it is still not permissible to access invalid memory for any reason. The actual memory be still be physically allocated by the underlying memory manager, but is logically inaccessible to your code, until it is reallocated again.
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 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:
C++ delete - It deletes my objects but I can still access the data?
(13 answers)
Elements retained even after using delete[]
(2 answers)
Closed 4 years ago.
I created a Vector class containing an array (stored in a heap) and an integer n_rows counting the amount of elements inside the array. In order to delete the array properly during the instance destruction I used the delete as explained here.
Class (in header file):
private:
int n_rows;
int* vect;
public:
Vector(int);
~Vector();
std::string toString(); // returns elements from vect
Destructor (in .cpp):
Vector::~Vector(){
delete [] this->vect;
cout << this->toString() << endl;
}
However if I print the array vect after deleting it, seemingly just the first two entries get deleted.
Example:
// Vector:
[2 2 2 2 2 2]
// Destruction
[0 0 2 2 2 2]
My questions:
Is this resource properly deleted?
Why does it just appear that the two first elements get modified?
Your destructor free the memory pointed to by vect, but it doesn't do anything to the contents of that memory.
Dereferencing an invalid pointer (like the one you have after doing delete[] on it) leads to undefined behavior. Just don't do it.
The memory is freed up when you delete it, but it's not necessarily overwritten/set to zero or anything like that. Accessing the freed memory is undefined behavior and anything might happen, but what often happens (and happened in this case) is that you get the old values that were there before it was deleted. In other words, when it is deleted, it is mostly just "marked for being available again".
The program usually won't waste time writing over the deleted memory. If the memory is reassigned, then whatever is getting the memory will most likely initialize it however it wants anyway.
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)