-2

I have this simple example which fails in "delete poinerToBufferPointer":

char* buffer = new char[8];
memset(buffer, 1, 8);
char** poinerToBufferPointer = &buffer;
delete poinerToBufferPointer;
delete[] buffer;

But if I comment the "delete poinerToBufferPointer"

char* buffer = new char[8];
memset(buffer, 1, 8);
char** poinerToBufferPointer = &buffer;
//delete poinerToBufferPointer;
delete[] buffer;

it's working but the question is who will delete the double pointer? Also very strange behavior is when I do delete on the pointer only it fail on delete[] buffer.

char* buffer = new char[8];
memset(buffer, 1, 8);
char** poinerToBufferPointer = &buffer;
delete *poinerToBufferPointer;  // <--- only delete the pointer that points 
delete[] buffer;

What is going on in memory, and what is the right way to delete both pointers?

halfer
  • 19,824
  • 17
  • 99
  • 186
user63898
  • 29,839
  • 85
  • 272
  • 514
  • Why do you think you need to `delete` the pointer? Your second code snippet is correct. – user17732522 Feb 11 '23 at 11:25
  • 4
    The right way in C++ is to avoid this all together (avoid [new/delete](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r11-avoid-calling-new-and-delete-explicitly) & avoid owning raw pointers). Learn how to use `std::vector` and/or `std::string` first. (even memset is hardly ever needed) – Pepijn Kramer Feb 11 '23 at 11:25
  • 2
    It might be you are learning C++ from an outdated source. Good sources to learn cpp from are : [cppreference](https://en.cppreference.com/w/). A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). When you've mastered the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date. – Pepijn Kramer Feb 11 '23 at 11:27
  • 1
    You should `delete` something once, and if and only if it was `new`ed. In this case `poinerToBufferPointer` is assigned an address of existing data and is not `new`ed. And in the last snippet `*poinerToBufferPointer` is equal to buffer so `delete[] buffer` attempts to delete it a second time (which is invalid). – wohlstad Feb 11 '23 at 11:36
  • 1
    Adding a note to wohlstad's, `delete *poinerToBufferPointer` should not be working either. Since `*poinerToBufferPointer` is equal to `buffer` and `buffer` was allocated using `new[]`, it must be deleted using `delete[]`. Using `delete` in this case would be undefined behavior. – nasy Feb 11 '23 at 11:38

1 Answers1

3
  1. You do not delete pointers, you delete the thing a pointer points to.
  2. You only need to delete things you allocate with new

delete[] buffer; deletes the array pointed to by the pointer buffer. Since you allocated that with new[] that is correct.

delete pointerToBufferPointer; attempts to delete the pointer pointed to by pointerToBufferPointer. You did not allocate that pointer using new, however, so that is incorrect. A program that attempts to do that has undefined behavior.

If you had allocated the pointer pointed to by pointerToBufferPointer using new then you would need to delete it. For example, the following is correct (though you should almost never need to write code like this):

char** pointerToBufferPointer = new char*;
*pointerToBufferPointer = new char[8];
// ...
delete[] *pointerToBufferPointer;
delete pointerToBufferPOinter;

In most real code you should avoid using new and delete directly at all. For most common use cases, containers like std::string and std::vector that manage their own memory allocation for you are perfectly sufficient and much less error-prone.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • Nice clear explanation for newbies. pointers and `delete` `new` can be confusing at first. – doug Feb 12 '23 at 05:10