[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

glibc detected: double free or corruption

chuan

11/15/2008 3:22:00 PM

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.
3 Answers

Victor Bazarov

11/15/2008 3:25:00 PM

0

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).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Salt_Peter

11/15/2008 4:11:00 PM

0

On Nov 15, 10:21 am, chuan <chuan...@googlemail.com> 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];
>
> }

What is 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;

Passing by value and returning a copy by value. Bad news. Think member
data (which is a pointer). Rename member to p_data and you'll get it.

>
> }
>
> Please give me a hint.

don't use pointers, pass by reference to constant and use dynamic
containers.

James Kanze

11/15/2008 11:45:00 PM

0

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