我正在使用VisualStudio2019学习std::share_pointer,我编写了一个程序,实现了两个整数的交换。
#include <iostream>
#include <memory>
void swap0(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
void swap1(std::shared_ptr<int> a, std::shared_ptr<int> b)
{
int t = *a;
*a = *b;
*b = t;
}
int main()
{
int a = 10;
int b = 20;
std::cout << a << " " << b << std::endl; // 10 20
std::shared_ptr<int> pa(&a);
std::shared_ptr<int> pb(&b);
swap1(pa, pb);
std::cout << a << " " << b << std::endl; // 10 20
}
但该程序显示了一个对话框,名为MicrosoftVisualVisualC++运行时库。下面是对话框的信息。
Debug Assertion Failed!
Program:
....../ConsoleApplication1.exe
File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp
Line: 904
Express: _CrtIsValidHeapPointer(block)
......
然后我用MinGW尝试了相同的代码,程序正常运行。我是不是滥用了shared_ptr?
发布于 2021-03-21 13:29:10
您的问题是,在这一行中,共享指针要求它使用的数据在堆上分配,而不是在堆栈上分配。这就是构造函数接受指针而不是引用的原因。
//...
std::shared_ptr<int> pa(new int{a});
std::shared_ptr<int> pb(new int{b});
//...
注意,在swap1中,您交换的是包含在shared_ptrs中的in,而不是shared_ptrs本身。
另外,通常使用std::swap来交换东西效果更好。
//...
int a = 0;
int b = 5;
std::swap(a, b); //values of a and b are swapped, no need to roll your own swap functions
这甚至可以用来交换shared_ptrs,注意这与交换它们的内容不同。
//...
std::shared_ptr<int> a(new int{0});
std::shared_ptr<int> b(new int{5});
std::swap(*a, *b); //swap contents of shared pointers
std::swap(a, b); //swap the shared pointers
发布于 2021-03-21 13:31:18
是的你滥用了它。
shared_ptr
很有用,因为它会在必要时自动删除它拥有的对象。这是唯一的目的。在这里,您尝试从指向局部变量的指针中生成一个shared_ptr
,然后该对象将尝试删除它。但是您不能这样做,因为变量在堆栈上(尝试在delete &a
的末尾调用main
,您将得到相同的结果)。
通常,您将使用shared_ptr
创建一个std::make_shared
。这样,原始指针根本不需要通过您的手。
https://stackoverflow.com/questions/66732824
复制相似问题