STL containers do not allocate memory by calling
new
on the appropriate element types. Instead they allocate memory blocks of arbitrary size, and then call
placement new
on blocks within that already allocated memory. It works something like this:
if (required_element_number > reserved_element_number) {
size_t new_element_number = determine_new_reserved_size(required_element_number);
size_t allocated = allocator->alloc(new_element_number*size_of(element_type);
for () {
new() element_type();
}
for () {
new() element_type;
}
}
note that
allocator
is a template argument that can be passed as an optional argument. I believe the default is calling
new
on type
char
, so you may get unexpected calls to
new char
, rather than calls to
new
for the element type you actually have. Also note that the in-place calls to
new
that call the constructors do not assign the result of the new operator and therefore may not fit into the scheme you provided. Also, the two uses of placement
new
involve both the copy and default constructor of your element type.