Does the release() member function of unique_ptr class free the memory of a dynamically allocated array?

According to my textbook, if a unique_ptr points, say 'unikp', to a dynamically allocated array, then after the user use release() member function on 'unikp', the memory of the dynamically allocated array which 'unikp' points to should be automatically freed. In order to test this, I wrote the code as shown below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <memory>
using namespace std;
int main()
{
    int *x1 = new int[3]{1, 2, 3};
    int *x2 = new int[3]{1, 2, 3};
    unique_ptr<int> unikp1(x1);
    unique_ptr<int> unikp2(x2);
    unikp1.release();
    unikp2.reset();
    cout << x1[0] << " " << x1[1] << " " << x1[2] << endl;
    cout << x2[0] << " " << x2[1] << " " << x2[2] << endl;
}

Surprisingly, the output turned out to be
1
2
1 2 3
4004040 3997892 3

on my computer, which should be indicating that the member function release() cannot free the corresponding memory and only reset() can free the memory.
Am I right about this? Can someone help me out here? Really appreciate it!
Memory always exists. It is not created or destroyed. Allocating and freeing memory is simply the act of gaining and giving away permission to use certain parts of memory. What happens after you give away permission is impossible to determine - you shouldn't go back and look at or alter memory you no longer have permission to access.

If a standard library function says it does something, it does that. It's not like some poorly developed product with horrible "documentation". It's the C++ Standard Library - people vote on this stuff in committees.

See also, firedraco's post below.
Last edited on
release() releases control of the pointer without deleting; the intention being that you are giving control of the deletion to whatever you are passing it to, so the unique_ptr no longer owns it.

As it stands, you would have a memory leak with unikp1 if you didn't have another pointer to it like x1.

Also, your intuition about whether or not the pointer was deleted by looking at x1/x2 is invalid, as accessing memory that has been deleted (like you are doing with x2) is invoking undefined behavior and could do anything.
Last edited on
Thanks for your replies. I understand that release() releases control of the pointer from a unique_ptr object without deleting for the case where the unique_ptr is pointing to a non-array type object. I was just confused over this statement given by my textbook saying that if the unique_ptr is pointing to a dynamic array, then calling release() on it would "automatically use delete[] to destroy its pointer". Maybe I interpreted it wrongly or something. Anyway, just to make sure, release() does not free the memory of the dynamic object it's pointing to even if the dynamic object here is actually a dynamic array, is it correct?
Your understanding is correct; your textbook is incorrect. release() does not delete the pointer.
By the way, in that case `.reset()' would execute delete, but you need it to execute delete[], so you are invoking undefined behaviour.

valgrind is a good tool to test these things
Anyway, just to make sure, release() does not free the memory of the dynamic object it's pointing to even if the dynamic object here is actually a dynamic array, is it correct?
It does free the object. It does not fill the content with other values.
If you want to hold array in unique pointer, declare it as pointer to array:
1
2
3
{
    std::unique_ptr<int[]> foo(new int[5]);
}//foo will delete memory correctly. 
@coder777: ¿what do you understand by `free the object'?
Let me phrase it more precisely:

release(): frees the object = does not longer hold the ownership
reset(): deletes the object = gives the memory back to the system

both operations do not modify the content of the data
To me, free and delete are almost synonyms.
Okay, I get it. Profuse thanks to everyone. I really appreciate your help!!!
Topic archived. No new replies allowed.