- #1
JonnyG
- 233
- 30
I am working through C++ Primer by Lippman et al. In Section 12.2.1 on smart pointers and dynamic arrays, they give this bit of code:
I just want to make sure that I am understanding everything properly - doesn't this create a memory leak? The release() method turns the unique pointer, up, into a null pointer but doesn't delete the dynamically allocated array it was pointing at, right? The authors, in the code comments, claims that .release() destroys the pointer. Is this an error on their part or am I missing something?
C++:
// up points to an array of ten uninitialized ints
unique_ptr<int[]> up(new int[10]);
up.release(); // automatically uses delete[] to destroy its pointer
I just want to make sure that I am understanding everything properly - doesn't this create a memory leak? The release() method turns the unique pointer, up, into a null pointer but doesn't delete the dynamically allocated array it was pointing at, right? The authors, in the code comments, claims that .release() destroys the pointer. Is this an error on their part or am I missing something?