public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Digvijoy Chatterjee" <chatterjee.digvijoy@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Re: C++ auto_ptr template query solved
Date: Fri, 26 May 2006 06:14:00 -0000	[thread overview]
Message-ID: <9dd10b1d0605252312y2a75a812u8d71cb4bee71c239@mail.gmail.com> (raw)

Thanks everyone,
I have written my custom Copy Constructors and there are no memory
leaks now . Actually until Martin showed me the code Compiler
automatically generates , i was under the assumption ,that the
compiler takes care of everything (even pointers ) until i figured it
all out.
Digz

On 5/24/06, Martin York <martin_york@symantec.com> wrote:
>
> I mailed you yesterday about your class:
>
> >Things to thing about:
> >
> >1: The compiler automatically generates a copy constructor for Car
> >
> >Car::Car(const Car& copy);
> >
> >2: The compiler automatically generates an assignment operator for Car
> >
> >Car& operator=(const Car& copy);
>
> To be more specific:
> If you do not specify a copy constructor the compiler will generate one
> for you:
>
> Car::Car(const Car& copy):
>   a(copy.a);
>   M1(copy.M1);
> {}
>
> If you think how that works in the following example:
>
> int main(int argc,char* argv[])
> {
>     Car   car1;
>     Car   car2(car1);  // This is what happens when you put a Car in a
> list
>
>     // Destructor for car2 called
>     // Destructor for car1 called
>     //
>     // The problem is that the member variables of car1 and car2
>     // both point at the same memory So you will get a double delete.
> }
>
>
>
>
>
>
> Neither of these auto generated methods behave well if the class
> contains raw pointers.
>
> -----Original Message-----
> From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
> Behalf Of Digvijoy Chatterjee
> Sent: Wednesday, May 24, 2006 12:54 AM
> Cc: gcc-help@gcc.gnu.org
> Subject: Re: C++ auto_ptr template query
>
> On 5/24/06, John (Eljay) Love-Jensen <eljay@adobe.com> wrote:
> > HI Digvijoy,
> >
> > std::auto_ptr is for holding a single object, not arrays of objects.
> >
> > Don't use std::auto_ptr to hold arrays of objects.
> >
> > Use a std::list<Car> instead.
> > try
> > {
> >   std::list<Car> carList;
> >   carList.push_back(Car());
> >   carList.push_back(Car());
> >   carList.push_back(Car());
> > }
> >
> > Or I bet Boost (www.boost.org) has something like a std::auto_ptr that
> holds arrays of objects.
> >
> > HTH,
> > --Eljay
> >
> >
> Hello Eljay,
> I tried using std::list<Car> as you have suggested above, the code as
> above runs but tells me :
> *** glibc detected *** double free or corruption (fasttop): 0x0804b038
> *** and  i loose 40 bytes, (the last Car when it allocated 10 ints but
> the Motor fails ) if I comment the delete M ,and delete []a in the Car
> destructor method, there is no double free corruption detected ,
>   ~Car ()
>  {
>    //delete []a;
>    //delete M1;
>  }
>  but when i run valgrind :
> ==32159== LEAK SUMMARY:
> ==32159==    definitely lost: 122 bytes in 5 blocks.
> which is i think
> [(4 * 10 ints) *3 Car objs ]=120
> [ (1*1Motor) * 2 Car objs ] =2
> Any  suggestions ???
> Digz
>

                 reply	other threads:[~2006-05-26  6:14 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9dd10b1d0605252312y2a75a812u8d71cb4bee71c239@mail.gmail.com \
    --to=chatterjee.digvijoy@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).