James Kanze
11/15/2008 11:45:00 PM
On Nov 15, 4:25 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> chuan wrote:
> > I define a class with a private dynamical member
> > Class A
> > {
> > double *data;
> > public:
> > A();
> > ~A();
> > }
> > In constructor I assign a block memory to data,
> > A::A(){
> > data = new double [size];
> > }
> > and delete it in the desturctor
> > A::~A(){
> > delete [] data;
> > }
> > So far so good, but when I trying to overload some
> > arithmetic operator (+ or /), I receive the "glibc detected:
> > double free or corruption" error. Here is my code
> > A A::operator+(A add){
> > A tmp;
> > for (int i=0; i<size; i++)
> > tmp[i] = this->data[i] + add.data[i];
> > return tmp;
> > }
> > Please give me a hint.
> Google "the rule of three c++" (without quotes, of course).
Actually, putting the quotes around "rule of three" might not be
a bad idea.
More generally, of course, I'd ask why he was using new[] and
delete[] to begin with. They're almost always a sign of bad
implementation; in this case, I fail to see why
std::vector<double> wouldn't do the job, much better.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34