0

If I have a pointer ptr1 used to create a dynamic array, and I delete that pointer before deleting the array itself i.e use delete ptr1 instead of delete[] ptr1, will the array automatically be deleted?

And, if I have another pointer ptr2 having the same value as ptr1, after deleting ptr1, can I still access the array that ptr1 pointed to using ptr2?

For me, when I try to output the array using ptr2 after deleting ptr1, it displays garbage values instead of the values of the array.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
GA Haroon
  • 55
  • 2
  • 4
    Your question is confusing, you should show code, not describe code. But if you have allocated an array with `new[]` then using `delete` is a mistake, you must use `delete[]`. And the answer to the second question is no, you cannot access the array using `ptr2`. – john Mar 23 '23 at 14:05
  • 1
    If you allocated memory with `new` you release it with `delete`. If you allocated it with `new[]` you release it with `delete[]`. You don't delete the pointer; the pointer is an argument to the appropriate `delete` operator, which releases the memory that the pointer points at. Colloquially, yes, you delete the pointer, but unless you're completely clear on what's going on, don't use colloquialism; they often hide important details. – Pete Becker Mar 23 '23 at 14:05
  • When you use `delete` or `delete[]` then what is deleted is the memory, not the pointer. Any pointer which points to the same deleted memory can no longer be used (unless you make that pointer point somewhere else first). – john Mar 23 '23 at 14:09
  • If you use `delete` after allocating with `new[]` that is wrong and triggers undefined behavoir. After you have triggered undefined behavoir its pointless to analyse what happens next. Additionally if you try to access memory which has been correctly deleted (through the original pointer or a copy of that pointer) you too trigger undefined behavoir and trying to analyse what happens is pointess. – Mike Vine Mar 23 '23 at 14:11
  • _"when I try to output the array..."_ You should show that code. You probably made a mistake. – Wyck Mar 23 '23 at 15:14

1 Answers1

1

If I have a pointer ptr1 used to create a dynamic array

A pointer does not create an array, it merely points at a memory location, nothing more. But a pointer can point at a memory block that is holding the content of an array, yes.

[If] I delete that pointer before deleting the array itself i.e use delete ptr1 instead of delete[] ptr1, will the array automatically be deleted?

Not correctly, no. If you allocate the array with new[] and then try to free it with delete instead of delete[], that is undefined behavior. You must match new with delete, and new[] with delete[].

And, if I have another pointer ptr2 having the same value as ptr1, after deleting ptr1, can I still access the array that ptr1 pointed to using ptr2?

No. The memory block that both pointers are pointing at is no longer valid once delete/[] touches it. You cannot use either pointer anymore, unless you first reassign them to point at valid memory, or set them to nullptr.

For me, when I try to output the array using ptr2 after deleting ptr1, it displays garbage values instead of the values of the array.

Correct, because you are trying to access invalid memory.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770