Initialize undefined size arrays

Sometimes it is not possible to know the size of an array before allocating it.

If the programmer wants to avoid the waste of memory using non-shrinkable arrays, it would be required to create a new array with the correct size and delete the old array after copying its elements.

Note that it is also possible to use other implementations, such as linked lists or some others that allow dynamic size.

On some cases, such as those, on which the arrays have a defined maximum size, its elements must be sequentially aligned and its real size is unknown before the allocation, it would be useful for the programmer to have standard shrinkable arrays.




/* author: ncomputers.org */
#include<cstdlib>
#include<iostream>
#include<time.h>
using namespace std;

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

int main(){
    typedef unsigned int A;
    A S=25,*s=new A[S];
    A**aa=new A*[S];
    srand(time(0));
    for(A i=0;i<S;i++){
        A*a=new A[S],*b=a;
        for(A j=0;j<S;j++){
            if(rand()%2)*a++=j;
        }
        s[i]=a-b;
        if(a==b){
            aa[i]=0,delete[]b;
        }
        else{
            aa[i]=b;
            //Suggested syntax:
            //delete[S-s[i]]a;
            operator delete[](b,s[i]*sizeof(A));
        }
    }
    for(A i=0;i<S;i++){
        cout<<i<<':';
        for(A j=0;j<s[i];j++){
            cout<<aa[i][j]<<',';
        }
        cout<<endl;
        delete[]aa[i];
    }
    delete[]s,delete[]aa;
    return 0;
}