gpt4 book ai didi

c++ - 共享指针 : why no double free?

转载 作者:太空狗 更新时间:2023-10-29 23:25:19 27 4
gpt4 key购买 nike

为什么当共享指针超出范围时此代码不生成双重释放?

int main()
{
{
auto * ptr = new int(1);
shared_ptr<int> a( ptr );
shared_ptr<int> b( ptr );
cout << "ok: " << *a << *b << endl;
}
cout << "still ok" << endl;
return 0;
}

最佳答案

Why does this code NOT generate a double free when the shared pointers go out of scope?

为什么你认为它没有?

这是未定义的行为,任何事情都有可能发生。这包括您的程序打印出 still ok

关于c++ - 共享指针 : why no double free?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48478857/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com