A
Alex Vinokur
I am looking for any custom allocator sample code for std::vector.
Thanks.
Thanks.
Alex Vinokur said:I am looking for any custom allocator sample code for std::vector.
Sharad Kala said:
When should 'construct' and 'destroy' be invoked?
Sharad Kala said:An allocator has to provide a construct and destruct operation. You may want
to take a look at 20.1.5 (Allocator requirements).
Construct is used to initialize the memory allocated with a value. This is
basically a call to placement new. So basically construct is invoked when an
object is created in the container.
Alex Vinokur said:For instance, v.push_back (element) for vector?
However, in http://www.josuttis.com/libbook/memory/myalloc1.cpp.html construct isn't invoked while push_back invocation.
construct isn't invoked while push_back invocation.Alex Vinokur said:For instance, v.push_back (element) for vector?
Yes
However, in http://www.josuttis.com/libbook/memory/myalloc1.cpp.html
Sharad Kala said:construct isn't invoked while push_back invocation.
Try putting a breakpoint in Construct and then see.
I put assert(0) in construct().
No response.
Sharad Kala said:Is NDEBUG macro defined ? I can very much see the assert failure on MS VC 7
if I insert the above line (in debug build). Try putting cout << "In
construct" and then see.
[snip]Alex Vinokur said:Sharad Kala said:construct isn't invoked while push_back invocation.
Try putting a breakpoint in Construct and then see.
I put assert(0) in construct().
No response.
// initialize elements of allocated storage p with value value
void construct (pointer p, const T& value)
{
// initialize memory with placement new
new((void*)p)T(value);
assert (0); // Added by me
}
[snip]
Thanks.
In that sample we can see that methods 'allocate' and 'deallocate' were invoked.
But methods 'construct' and 'destroy' were not invoked.
When should 'construct' and 'destroy' be invoked?
They may never be invoked - containers are not required to call them.
I believe that Dinkumware's lib does call them, since they support
"unusual" pointer types and memory models. I doubt anyone else does.
Why are they then as part of standard in allocator requirements in 20.1.5 ?
Is it an optional feature or are the compilers not being conformant ?
Alex Vinokur said:Thanks.
In that sample we can see that methods 'allocate' and 'deallocate' were invoked.
But methods 'construct' and 'destroy' were not invoked.
When should 'construct' and 'destroy' be invoked?
[snip]jose luis fernandez diaz said:'allocate'only reserves raw memory. Wnen you insert objects in that
allocated memory, for example with vector 'push_back' method,
'construct' method must be invoked. It is invoked from vector
'push_back' method. Can you post vector 'push_back' method code from
the vector library file that you are using ?
[snip]Alex Vinokur said:[snip]
Here is the Nicolai M. Josuttis' code sample with cosmetic changes added by me to profile the executable:
http://groups.google.com/[email protected]
Here are log files for the following compilers
* GNU g++ : http://groups.google.com/[email protected] [snip]
We can see that
* GNU g++ 3.3.1 doesn't invoke construct() and destroy(); [snip]
Hi everybody !
There's a behaviour in STL allocator I don't understand, I hope
somebody can
help me...
I'm trying to customize an allocator to track the insertion/deletion
of objects
in a container,
decorating the standard allocator functions construct() and destroy().
The problem is that it seems that those functions are never called in
STL
containers, instead
a global template function _Construct() (defined in <stl_construct.h>)
is
called, that is
completely unaware of allocators.
This is a bug.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.