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 ?

Était-ce utile?

La solution

Yes you're wrong in thinking that you can delete something that you did not allocate.

Only delete things you have newed yourself. Don't delete someone else's stuff.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top