将 shared_ptr 与对象数组一起使用
Using shared_ptr with array of objects
我看了一些关于 C++ 共享指针的教程,我有几个问题试图在 Internet 上找到答案,但没有成功。
考虑以下代码:
class A{
int v,u;
public:
A(){}
A(int p1, int p2): v(p1), u(p2) {}
~A(){};
};
void f()
{
shared_ptr<A> c(new A[5]);
// Is it correct that this causes a memory leak because...
//... the default deleter only deletes c[0] ?
// If yes, is this still true for C++17 and C++20 ?
shared_ptr<A> d(new A[5], [](A* ptr){ delete [] ptr;});
// how to pass non-default constructor argument in this case ?
}
int main(){
f();
}
问题:
1- 自定义删除器是否必须包含对象数组?
2-如何将参数传递给默认以外的构造函数?
3- 自定义删除器可以是免费功能还是会员功能? (不是 lambda)。
备注:
1- 编译器标志:-std=gnu++11 -fext-numeric-literals -std=c++11 -std=c++14 "-D MS_STDLIB_BUGS=0"
2- G++ with MinGW64 on code blocks.
3- 但是,我有兴趣了解一下一般情况。
1- Is the custom deleter is a MUST with array of objects?
从 C++17 开始否,如果您为 std::shared_ptr.
指定正确的模板参数类型
Uses the delete-expression delete ptr
if T is not an array type; delete[] ptr if T is an array type (since C++17)
as the deleter.
在 C++17 之前,您已经指定了自定义删除器(可能使用 std::default_delete)。
2- How to pass parameter to the constructor other than default?
自 C++20 起,您可以通过 std::make_shared 实现此目的。
template<class T> shared_ptr<T> make_shared(std::size_t N, const std::remove_extent_t<T>& u);
(4) (since C++20) (T is U[])
template<class T> shared_ptr<T> make_shared(const std::remove_extent_t<T>& u);
(5) (since C++20) (T is U[N])
every element is initialized from the default value u.
或者像 new A[5] { A{0, 0}, A{1, 1}, ...}
那样手动做某事。
3- Can the custom deleter be a free or member function? (Not lambda).
它可以是一个自由或静态成员函数。
我看了一些关于 C++ 共享指针的教程,我有几个问题试图在 Internet 上找到答案,但没有成功。
考虑以下代码:
class A{
int v,u;
public:
A(){}
A(int p1, int p2): v(p1), u(p2) {}
~A(){};
};
void f()
{
shared_ptr<A> c(new A[5]);
// Is it correct that this causes a memory leak because...
//... the default deleter only deletes c[0] ?
// If yes, is this still true for C++17 and C++20 ?
shared_ptr<A> d(new A[5], [](A* ptr){ delete [] ptr;});
// how to pass non-default constructor argument in this case ?
}
int main(){
f();
}
问题:
1- 自定义删除器是否必须包含对象数组?
2-如何将参数传递给默认以外的构造函数?
3- 自定义删除器可以是免费功能还是会员功能? (不是 lambda)。
备注:
1- 编译器标志:-std=gnu++11 -fext-numeric-literals -std=c++11 -std=c++14 "-D MS_STDLIB_BUGS=0"
2- G++ with MinGW64 on code blocks.
3- 但是,我有兴趣了解一下一般情况。
1- Is the custom deleter is a MUST with array of objects?
从 C++17 开始否,如果您为 std::shared_ptr.
指定正确的模板参数类型Uses the delete-expression
delete ptr
if T is not an array type; delete[] ptr if T is an array type (since C++17)
as the deleter.
在 C++17 之前,您已经指定了自定义删除器(可能使用 std::default_delete)。
2- How to pass parameter to the constructor other than default?
自 C++20 起,您可以通过 std::make_shared 实现此目的。
template<class T> shared_ptr<T> make_shared(std::size_t N, const std::remove_extent_t<T>& u); (4) (since C++20) (T is U[]) template<class T> shared_ptr<T> make_shared(const std::remove_extent_t<T>& u); (5) (since C++20) (T is U[N])
every element is initialized from the default value u.
或者像 new A[5] { A{0, 0}, A{1, 1}, ...}
那样手动做某事。
3- Can the custom deleter be a free or member function? (Not lambda).
它可以是一个自由或静态成员函数。