Re: C++ auto_ptr template query solved

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxx> 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@xxxxxxxxxxx [mailto:gcc-help-owner@xxxxxxxxxxx] On
Behalf Of Digvijoy Chatterjee
Sent: Wednesday, May 24, 2006 12:54 AM
Cc: gcc-help@xxxxxxxxxxx
Subject: Re: C++ auto_ptr template query

On 5/24/06, John (Eljay) Love-Jensen <eljay@xxxxxxxxx> 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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux