[英]Why does deleting a ostringstream object as in my code leads to segmentation fault?
#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;
}
The line delete tmpstr.rdbuf();
该行
delete tmpstr.rdbuf();
gives a segmentation fault . 给出分割错误。 I guess rdbuf returns char* pointer and hence .
我猜rdbuf返回char *指针,因此。 I can use a delete on it to free the memory space allocated to
tmpstr
我可以在其上使用删除来释放分配给
tmpstr
的内存空间
Am i wrong Somewhere ? 我在某处错了吗?
Yes you're wrong in thinking that you can delete
something that you did not allocate. 是的,您认为可以
delete
未分配的内容是错误的。
Only delete
things you have new
ed yourself. 只
delete
自己new
编辑的内容。 Don't delete
someone else's stuff. 不要
delete
别人的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.