Shrinkable arrays vs vectors

The C++ standard containers implementations, such as vector, map, list, and others are very useful.

Sometimes, when a better performance is needed, these implementations may be discarded by the programmer, because it is possible to manage faster the data with native arrays or with own container implementations.

The performance offered by std::vector when shrinking may be overcome, because std::allocator doesn't support in-place reallocation.

Every time that std::vector::shrink_to_fit is called, it will allocate a block with the new size, copy the useful data and delete the old block.

We've tested this code in a personal computer and obtained 100 times better performance with in-place shrinkable arrays than with vectors.

Sometimes it is also easier to handle multidimensional arrays using the new[] and delete[] operators than with vectors of pointers or vectors of vectors.

(1 thread, GNU Compiler, -O0 flag, Intel Core i7-3rd 2.1GHz, DDR3-1600)

/* author: */
using namespace std;

struct A{
    int a;

void*operator new[](size_t s){
    return malloc(s);
void operator delete[](void*p){
void operator delete[](void*p,size_t s){
    if(realloc(p,s)!=p)throw 0;

int main(){
    unsigned int a=0,v=0,s,S=1024,z=time(0)+1;
        b=new A[S]();
        //Suggested syntax:
        for(s=S-1;s>0;--s)operator delete[](b,s*sizeof(A));
        c=new vector<A>(S);
        delete c;
    cout<<"Total arrays: "<<a<<endl;
    cout<<"Total vector: "<<v<<endl;
    return 0;