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

Jan 30, 2015 at 3:46am
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!
Jan 30, 2015 at 4:00am
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 Jan 30, 2015 at 4:02am
Jan 30, 2015 at 4:00am
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 Jan 30, 2015 at 4:01am
Jan 30, 2015 at 5:11am
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?
Jan 30, 2015 at 6:03am
Your understanding is correct; your textbook is incorrect. release() does not delete the pointer.
Jan 30, 2015 at 6:15am
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
Jan 30, 2015 at 9:13am
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.
Jan 30, 2015 at 10:15am
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. 
Jan 30, 2015 at 11:27am
@coder777: ¿what do you understand by `free the object'?
Jan 30, 2015 at 12:31pm
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
Jan 30, 2015 at 1:25pm
To me, free and delete are almost synonyms.
Jan 31, 2015 at 2:04pm
Okay, I get it. Profuse thanks to everyone. I really appreciate your help!!!
Topic archived. No new replies allowed.