Topic: No support for pools of arrays


Author: krixel@qed.pl ("Krzysztof elechowski")
Date: Tue, 28 Feb 2006 01:56:02 GMT
Raw View
I was trying to design

template<class T> class Pool<T> { T *allocate(void); void release(T *); };

with the requirement that T::T() must exist and be accessible.

Everything worked well until I tried making a pool of int[2].  The problem
is that

new(a_v) T

returns int *, not int (*)[2] as expected.  I changed it to

new(a_v) T[1].

Everything worked well until I tried making a pool of a class with a
destructor because the pointer returned by the new statement was different
from a_v; the assumption that the size of a single allocation equals the
sizeof(T) was wrong; I was unable to guess the correct size and I was unable
to release the pointer thus allocated afterwards.

I also noticed that the delete[] statement can be used to get the allocation
block address from a pointer which is passed to operator delete[].  The
problem is there is I can see no way of calling it with this purpose because
the operator delete[] goes to the heap with the pointer and the programme
breaks.  Although new T[] statement can be redirected to use my pool,
delete[] statement apparently cannot.  I cannot understand this asymmetry.
What am I missing?

On the other hand, I think that it is a design flaw not to allow new X[2] to
return X (*)[2].  My rule would be
new X[2] returns X (*)[2]
new X[n] returns X * when n is unknown at compile time.  It would be much
better than it is now.

I also noticed that std::allocator<T>::allocate(size_t) returns an invalid
pointer: the object it points to contains garbage and cannot be used,
especially if T is a polymorphic class.  I do not think this pointer should
be of type T * in such a situation.  This is a misunderstanding and I think
it could make a defect report.

Chris


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]