Tuesday, 20 June 2017

debugging - Why do I get: double free or corruption (top) after running this c++ script?

I've got the following piece of code:



Class Chain


with



char* c; 



as its only public atrib



istream& operator>>(istream& i, Chain& s) {
delete [] s.c;
const int L = 256;
char *t = new char[L];
i.getline(t,L);
s.c = t;

return i;
}

ostream& operator<<(ostream& o, Chain s) {
o << s.c;
return o;
}

#include
#include "Chain.h"

using namespace std;

int main(){

Chain id;

cin >> id;

cout << id;
cout << id;



After running the code under the Eclipse IDE on Xubuntu (last version) I get the following error:




Error in [...] double free or corruption (top): 0x00000000008fd290 ***




What could be wrong?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...