When a unique_ptr is deallocated?

那年仲夏 提交于 2021-01-27 16:11:58

问题


In this code:

void f(std::unique_ptr<int> q)
{
}

void g()
{
    std::unique_ptr<int> p{new int{42}};
    f(std::move(p));
}

At which line p is deallocated? I'd say at the exit of the f function because it was moved there using std::move, but I'm not sure nor confident about this answer.


回答1:


At which line p is deallocated?

At the end of the scope where it was declared i.e the function g in this case. That is when objects with automatic storage are destroyed, and their memory deallocated.

The integer with dynamic storage that you initialised to 42 will be deallocated by the destructor of q at the end of f. This is because the move construction transferred the ownership.




回答2:


At which line p is deallocated?

p is an object, and objects can be destroyed. It's nemory that can be deallocated.

The memory that p originally pointed to (allocated by new int{42}) is deallocated when control leaves f (when q is destoyed).

p itself is destroyed when control leaves g (at that moment p is null, i.e. doesn't point to anything).




回答3:


To make it clear change you code snippet the following way

#include <iostream>
#include <memory>
#include <utility>

struct A
{
    ~A() { std::cout << "~A()\n"; }
};

void f(std::unique_ptr<A> q)
{
    std::cout << "f() begins\n";
}

void g()
{
    std::cout << "g() begins\n";
    std::unique_ptr<A> p{new A };
    f( std::move( p ) );
    std::cout << "p == nullptr is " << ( p == nullptr ) << '\n';
    std::cout << "g() endss\n";
}

int main() 
{
    std::cout << "Within main()\n";
    g();

    return 0;
}

The program output is

Within main()
g() begins
f() begins
~A()
p == nullptr is 1
g() endss

So the function g transferred the ownership of the pointed object to the function f. The function f ended and did transferred the ownership of the pointed object neither other function. So at the moment of exiting of the function f the destructor was called.




回答4:


You are transferring ownership from p to q, so q now owns the resource. Therefore your integer is destroyed as soon as q is destroyed, that is, at the end of function f.

The unique pointers themselves are destroyed at the end of their scopes, i.e. p is destroyed when g() returns and q is destroyed when f() returns.

See also this small example.




回答5:


p will get destroyed on exiting g function, just as any other variable with local scope will. Solely, at that time, it doesn't hold the data any more; to understand, you actually don't need a separate function for, just consider this:

int* n = new int(42);

{
    std::unique_ptr<int> p(n); // p how owns the value pointed to by n
                               // for brevity, I'll say p owns n from now on,
                               // although technically not correct...

    { // opening another scope! (corresponds to function call)
        std::unique_ptr<int> q; // another variable, nothing else is a
                                // function parameter either...

        q = std::move(p);       // this happens, too, when calling a function
                                // at this point, the ownership is transferred to q
                                // q now owns n, p is left with just nothing
                                // (i. e. now holds a null-pointer)
    } // at this point, q is going out of scope; as it is the current
      // owner of n, it will delete it

    // p still is in scope, but it has transferred ownership, remember?

} // at this point, p is destroyed; as it doesn't own anything at all any more
  // it dies without doing anything either...


来源:https://stackoverflow.com/questions/58234814/when-a-unique-ptr-is-deallocated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!