Simple 'delete []' ptr ownership question

Basically I'm just asking if this works:
1
2
3
4
5
6
7
8
  
int* ptr1;
  if (true)
  {
     int* ptr2 = new int [n];
     ptr1 = ptr2;
  }
    delete [] ptr1;

Sorry for the simple question I just couldn't think how to phrase it to google.
Last edited on
Not great style, but yes that works because the if condition is always true.
If the condition were not always true, then you would be trying to delete an uninitialized pointer.

Since the condition is always true, why have the if at all?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Last edited on
Thanks for the tip. This isn't my actual code. The if is just there to show that ptr2 goes out of scope.
Thanks for the tip. This isn't my actual code. The if is just there to show that ptr2 goes out of scope.


Just fyi you don't need an 'if' to introduce a new scope:
1
2
3
4
5
6
7
8
  int* ptr1;

  {
    int* ptr2 = new int [n];
    ptr1 = ptr2;
  }

  delete [] ptr1;
should delete [] be called on ptr1 or ptr2? maybe delete [] ptr1 is valid here because the object it points to has been created on the heap but in that case and what happens to ptr2 after delete [] ptr1 as the object it's pointing to has been deleted? perhaps an interm step ptr2 = nullptr;?
maybe delete [] ptr1 is valid here because the object it points to has been created on the heap but in that case and what happens to ptr2 after delete [] ptr1 as the object it's pointing to has been deleted?
ptr2 does not exist when delete is invoked.
delete is invoked on the object pointed to by ptr1, which is also being pointed to by ptr2. Once this object is deleted what does ptr2 point to? Sorry I fail to see why ptr2 does not exist when delete[] is invoked on ptr1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
constexpr auto SIZE = 4;

int main()
{
    int* ptr1;
    int* ptr2 = new int[SIZE];
    ptr1 = ptr2;
    delete [] ptr1;
    std::cout << "ptr1 " << ptr1 << '\n';
    std::cout << "ptr2 " << ptr2 << '\n';
}
/*Output (system specific)
ptr1 0x702260
ptr2 0x702260*/

delete is invoked on the object pointed to by ptr1, which is also being pointed to by ptr2.

Incorrect. In the snippets you were responding to, ptr2 does not exist when delete is invoked as opposed to the code in your post where you've removed the inner scope.


Once this object is deleted what does ptr2 point to?

In your modified code, both ptr1 and ptr2 point to invalid objects (after deletion.) (By contrast, only ptr1 ever points to an invalid object in the previous code snippets.)


Sorry I fail to see why ptr2 does not exist when delete[] is invoked on ptr1:
Yes, well... I suppose if you change the code so it does exist, then comments applied to other code are no longer relevant. Try reinserting the scope you removed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
constexpr auto SIZE = 4;

int main()
{
    int* ptr1;
    {
        int* ptr2 = new int[SIZE];
        ptr1 = ptr2;
    }
    delete [] ptr1;
    std::cout << "ptr1 " << ptr1 << '\n';
    std::cout << "ptr2 " << ptr2 << '\n';
}


That won't compile because ptr2 doesn't exist on line 13
Last edited on
cire: you're right ptr2 being in the inner scope indeed
Topic archived. No new replies allowed.