Why does deleting a ostringstream object as in my code leads to segmentation fault?
-
07-06-2021 - |
Question
#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();
gives a segmentation fault . I guess rdbuf returns char* pointer and hence . I can use a delete on it to free the memory space allocated to tmpstr
Am i wrong Somewhere ?
La solution
Yes you're wrong in thinking that you can delete
something that you did not allocate.
Only delete
things you have new
ed yourself. Don't delete
someone else's stuff.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow