【发布时间】:2015-05-25 06:59:46
【问题描述】:
我在 main 中创建了一个 sample 类的指针。我将此指针传递给函数function1()。这个函数必须使用指针作为共享指针,并使用这个指针做一些操作。在function1() 退出期间,sample 的析构函数因shared_ptr 而被调用。当我将相同的指针传递给不同的函数时,该指针不再有效并且程序崩溃。
1.如何推迟function1()中的删除操作(销毁调用)?
2.什么是替代方法,以便我可以将指针传递给不同的函数并安全地使用它,尽管有些函数使用指针作为shared_ptr?
这里有示例代码和输出。
#include <memory>
#include <iostream>
#include <string.h>
using namespace std;
class sample
{
private:
char * data;
public:
sample( char * data )
{
cout << __FUNCTION__ << endl;
this->data = new char[strlen( data)];
strcpy( this->data, data );
}
~sample()
{
cout << __FUNCTION__ << endl;
delete this->data;
}
void print_data()
{
cout << __FUNCTION__ << endl;
cout << "data = " << this->data << endl;
}
};
void function1( sample * ptr )
{
shared_ptr<sample> samp( ptr );
/* do something with samp */
ptr->print_data();
}
void function2( sample * ptr )
{
ptr->print_data();
}
int main()
{
char data[10] = "123456789";
data[10] = '\0';
sample * s = new sample( data );
function1( s );
function2( s );
return 0;
}
输出:
sample
print_data
data = 123456789
~sample
print_data
data =
【问题讨论】:
-
您在
sample构造函数中有一个错误。你忘记了 C 风格的字符串有一个额外的终止字符。对字符串使用std::string。 -
当您执行
data[10] = '\0';时,您也有一个错误。由于你用字符串初始化数组,而且数组足够大,它已经被终止了,不需要再添加一个终止符。同样,在 C++ 中使用std::string处理字符串。 -
为什么
function1需要shared_ptr?它是否拥有样本的所有权? -
你不应该这样做。您编写的代码以一种根本错误的方式使用智能指针;您应该努力重写代码,以便它以预期的方式使用智能指针,而不是尝试修改代码以使其按原样工作。
标签: c++ pointers shared-ptr smart-pointers