0
#include <iostream>
using namespace std;

int main()
{
    int* A = new int[2];
    A[0] = 10000;
    A[1] = 2;
    int*B = A;
    delete B;
    cout << B << A;
}

I found the code above, delete B; removes the data of array A. Can we remove allocated memory by free some another assigned pointer?

심민기
  • 39
  • 3
  • 2
    Notn really sure what you're shooting for with that last `cout` chain, but the answer to your question is yes, assuming you actually delete it correctly in the first place (that should be `delete [] B;`) – WhozCraig Apr 26 '22 at 03:59
  • 3
    Q: Can we remove allocated memory by free some another assigned pointer? A: Of course. NOTES: 1) the appropriate operator is delete[], not "delete". 2) Trying to deference either A[] or B[] after the deletion is an error. 3) You're deleting the underlying heap object that's being pointed to, not the "pointer" itself. – paulsm4 Apr 26 '22 at 04:37
  • `delete` or `delete[]` for that matter do two things: free the memory and before that: destruct the object. For `int` the destruction is trivial, nothing has to be done. But e.g. if your data represents a file, that would be closed, or if your data type internally also has dynamically allocated memory that would be freed at destruction time. Assigning (`=`) a pointer normally does a shallow copy, the new pointer just points to the same memory. If it would be a deep copy, both would have to be deleted. – Sebastian Apr 26 '22 at 05:02

2 Answers2

4

You don't delete pointers. You delete the thing pointed to.

A, in your example isn't an array. It is a pointer that points to the first element of an array. When you write int* B = A;, you make B a pointer to the same memory. At that point A and B are identical, and the array they point to can be freed via either of them.

It doesn't matter how many times you copy a pointer. All that matters is that the thing pointed to by the pointer you pass to your deletion expression was allocated with the corresponding allocation expression (i.e. new/delete, new[]/delete[], malloc/free, etc).

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • 1
    And this means that if you are not careful you can blow your program up really, really easily by `delete A;`ing while you still need `B`. Rather than being really, really careful all of the time, the modern programmer uses containers and smart pointers. – user4581301 Apr 26 '22 at 05:53
  • 1
    And knows about *ownership* (the single function or object owning a resource that is responsible for cleaning up the resource). *The onus of ownership* ... – Sebastian Apr 26 '22 at 05:54
  • 1
    [What is ownership of resources or pointers?](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) – user4581301 Apr 26 '22 at 05:57
2

Can we remove allocated memory by free some another assigned pointer?

Yes, as long as you do it correctly i.e., using the appropriate delete-expression.

From new.delete.single-12:

Requires: ptr shall be a null pointer or its value shall represent the address of a block of memory allocated by an earlier call to a (possibly replaced) operator new(std​::​size_­t) or operator new(std​::​size_­t, std​::​align_­val_­t) which has not been invalidated by an intervening call to operator delete.

This means that in your program, we are allowed to write:

delete [] B;

On the other hand delete B; is incorrect because we must use array-form of delete-expression here.

Jason
  • 36,170
  • 5
  • 26
  • 60