This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
vector doesn't use the pointer defined in its allocator
- From: Tom <tom_t at gmx dot at>
- To: libstdc++ at gcc dot gnu dot org
- Date: Fri, 24 Mar 2006 15:34:32 +0100
- Subject: vector doesn't use the pointer defined in its allocator
Hi!
I'm doing a project for my university, and I run in some troubles with the
stl:
I dried to define my own allocator, which allocates memory and returns a
class defined by me due to a call to allocate(...). This class wraps a
pointer, and frees it automatically (with refernce-counting), but that's
not really important...
I have set up the typedefs for the pointer-type in my allocator to this
class like
//...
typedef SmartPointer<T> pointer;
typedef const SmartPointer<T> const_pointer;
//...
The problem is, that the vector-class doesn't use these typedefs. I've
downloaded the newes snapshot available, and I've found in the file
stl_vector.h code like this:
//...
_Tp*
_M_allocate(size_t __n)
{ return _M_impl.allocate(__n); }
//...
where I think that this should be:
//...
allocator_type::pointer
_M_allocate(size_t __n)
{ return _M_impl.allocate(__n); }
//...
Otherwise the conversation-operator from my class is called, and the
instance from my class is destructed after the call to allocate, which
leads my class to free the memory :-(
There are a lot of other places where _Tp* is used, where i think it would
be better to use allocator_type::pointer. I haven't tested this with other
containers, but I think there is the same problem.
Is there a reason why the typedef pointer isn't used? I've looked in the
book "The C++ Programming Language" from Bjarne Stroustrup, and he
descripes some examples where to use an own allocator, where one of these
is to redefine the pointer-type to be able to access memory which is too
big for a single pointer (for example with a long). This wouldn't be
possible with this implementation because there would be always a cast to
a normal pointer.
Thanks for explications
Thomas