Inv*_*tus 0 c++ stringstream ostringstream
#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;
}
Run Code Online (Sandbox Code Playgroud)
该行delete tmpstr.rdbuf(); 给出了分段错误.我猜rdbuf返回char*指针因此.我可以使用删除来释放分配给的内存空间tmpstr
我错了吗?
是的,你认为你可以delete做一些你没有分配的东西是错误的.
只有delete你自己new编写的东西.不要delete别人的东西.
| 归档时间: |
|
| 查看次数: |
238 次 |
| 最近记录: |