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;

?

6
  • 2
    Yes you can. They are the same, don't they? Commented Nov 3, 2015 at 11:49
  • this is the origin of setting deleted pointers to NULL Commented Nov 3, 2015 at 11:52
  • Thanks a lot, I'm impressed by such a fast answer :) Commented Nov 3, 2015 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. Commented Nov 3, 2015 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 :) Commented Nov 3, 2015 at 12:01

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Just pointing out to those reading this that this behavior is "automated" if using std::shared_ptr<T> instead
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.
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.
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.

Comments

-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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.