0

The situation is:

int main ()
{
    int *p1 = new int[50];
    int *p2 = p1;
    ...

When I want to delete my array I do:

delete[] p1;

Can I do it also with this:

delete[] p2;

?

  • 2
    Yes you can. They are the same, don't they? – Paolo M Nov 03 '15 at 11:49
  • this is the origin of setting deleted pointers to NULL – fatihk Nov 03 '15 at 11:52
  • Thanks a lot, I'm impressed by such a fast answer :) – Monika Sagan Nov 03 '15 at 11:53
  • 1
    @AngelinaJolly Doesn't really help if one have multiple pointers all pointing to the same memory. What if you forget to set one of the pointers to `nullptr`? It's a very easy mistake to make unfortunately. – Some programmer dude Nov 03 '15 at 11:55
  • @JoachimPileborg That is good notce. I wasn't really sure what was the point of AngelinaJolly's comment. When I have more than one pointer, after deleting p1 and setting it to nullptr, I can still make a mistake deleting p2 :) – Monika Sagan Nov 03 '15 at 12:01
  • @AngelinaJolly Setting pointers to NULL after deletion is not always good. See http://stackoverflow.com/q/1931126/3425536 – Emil Laine Nov 03 '15 at 12:26

3 Answers3

4

Both pointers are pointing to the same memory, so you can use either one when you delete[]. But only do it once, deleting already deleted memory is undefined behavior. So if you do delete[] p1; then you can't do delete[] p2; as well, it's one or the other.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 4
    Just pointing out to those reading this that this behavior is "automated" if using [`std::shared_ptr`](http://en.cppreference.com/w/cpp/memory/shared_ptr) instead – default Nov 03 '15 at 12:04
  • 1
    While the new smart pointers may be nice and tempting to use, I usually recommend against it for "normal" pointers, and instead say that the new smart pointers should be seen from a resource ownership point of view. Can a resource only have a single owner at a time? Then use the unique pointer. Can a resource have multiple simultaneous owners? Then use the shared pointer. – Some programmer dude Nov 03 '15 at 12:13
  • 1
    I also usually recommend against using pointers and dynamic allocated memory for storing data, and recommend using `std::vector` (or `std::array` if the number of elements is known at compile-time) instead. – Some programmer dude Nov 03 '15 at 12:15
0

If not to delve into the details the definition of the delete operator looks the following way

delete expression
delete [ ] expression

As you can see it uses an expression (more precisely a cast expression). For example you could write

int *p1 = new int[50];
int *p2 = p1 + 50;

delete [] ( p2 - 50 );

(Notice that you have to enclose in parentheses expression p2 - 50.)

The expression is evaluated and its values is used as the address of the memory being deleted.

After deleting the memory you may not access it. Otherwise the bahaviour of the program will be undefined.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1

Since p2 points to the same block of memory that was allocated by p1 then yes, p2 can be used to erase that block of memory.

You can verify this by running the following code...

#include <iostream>

using namespace std;

int main()
{
    int* p1 = new int[10];
    int* p2 = nullptr;

    for (int i = 0; i < 10; i++) {
        p1[i] = i * i;
    }

    // 1st run
    for (int i = 0; i < 10; i++) {
        cout << p1[i] << " ";
    }
    cout << endl;

    p2 = p1;

    if (p2 != nullptr) {
        delete p2;
    }   

    // 2nd run;
    for (int i = 0; i < 10; i++) {
        cout << p1[i] << " ";
    }

    p1 = nullptr;
    p2 = nullptr;

    return 0;
}

The 2nd run through the p1 array shows that the content of that array was deleted by the delete p2 operation which was put in an if statement for safety.

The two pointers can be put to rest, once their use is over, by equating them to nullptr keyword.

dspfnder
  • 1,135
  • 1
  • 8
  • 13