glibc detected: double free or corruption

Discussion in 'C++' started by chuan, Nov 15, 2008.

  1. chuan

    chuan Guest

    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::eek:perator+(A add){
    A tmp;
    for (int i=0; i<size; i++)
    tmp = this->data + add.data;
    return tmp;
    }

    Please give me a hint.
     
    chuan, Nov 15, 2008
    #1
    1. Advertising

  2. chuan

    Salt_Peter Guest

    On Nov 15, 10:21 am, 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];
    >
    > }


    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::eek:perator+(A add){
    >   A tmp;
    >   for (int i=0; i<size; i++)
    >          tmp = this->data + add.data;
    >   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.
     
    Salt_Peter, Nov 15, 2008
    #2
    1. Advertising

  3. chuan

    James Kanze Guest

    On Nov 15, 4:25 pm, "Victor Bazarov" <> 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::eek:perator+(A add){
    > >  A tmp;
    > >  for (int i=0; i<size; i++)
    > >         tmp = this->data + add.data;
    > >  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:
    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
     
    James Kanze, Nov 15, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Grant Edwards

    glibc detected double free or corruption

    Grant Edwards, Dec 20, 2006, in forum: Python
    Replies:
    0
    Views:
    430
    Grant Edwards
    Dec 20, 2006
  2. Delaney, Timothy (Tim)

    RE: glibc detected double free or corruption

    Delaney, Timothy (Tim), Dec 20, 2006, in forum: Python
    Replies:
    0
    Views:
    528
    Delaney, Timothy (Tim)
    Dec 20, 2006
  3. Gabriel Genellina
    Replies:
    1
    Views:
    576
    Gabriel Genellina
    Feb 25, 2009
  4. Aryeh Leib Taurog

    glibc detected double free or corruption

    Aryeh Leib Taurog, Feb 8, 2010, in forum: Python
    Replies:
    0
    Views:
    1,101
    Aryeh Leib Taurog
    Feb 8, 2010
  5. Pallav singh
    Replies:
    1
    Views:
    477
    Jens Thoms Toerring
    Jan 29, 2012
Loading...

Share This Page