This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: C++ auto_ptr template query
- From: "Digvijoy Chatterjee" <chatterjee dot digvijoy at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 24 May 2006 10:24:12 +0530
- Subject: Re: C++ auto_ptr template query
- References: <9dd10b1d0605230303i55809b1fgc88b838c3227f29e@mail.gmail.com> <EDC8DDD212FEB34C884CBB0EE8EC2D9121649D@namailgen.corp.adobe.com>
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