#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ostringstream out;
ostringstream tmpstr;
tmpstr << "ritesh is here";
out << tmpstr.str().c_str();
out << endl;
cout << out.str();
if(tmpstr.rdbuf()!=NULL)
cout << "tmpstr not null" <<endl;
else
cout << "tmpstr null" <<endl;
delete tmpstr.rdbuf(); // This line gives me segmentation fault
cout <<"deleted" << endl;
}
行delete tmpstr.rdbuf();
给出了细分错误。我猜rdbuf返回char *指针因此。我可以使用删除来释放分配给tmpstr
我错了吗?
答案 0 :(得分:7)
是的,你认为你可以delete
你没有分配的东西是错误的。
您自己只有delete
件事new
。不要delete
别人的东西。