Free data as soon it becomes useless

On some cases, when a program ends to compute some data, it becomes useless and to free it, could allow other applications to use their memory blocks.

For example:

A program requires a billion of sequentially aligned objects to find a solution to a hard problem.

As the code finds a new and better solution, some objects will become useless.

Due to complexity, the code starts to require more time to process the useful data and find better solutions.

When the program has more useful objects in memory, it requires a few seconds to find solutions to the problem, because they are easier to find.

When the program searches better solutions, it requires less objects and more time.

For example:

When a program tries to color a graph with ten thousand of vertex, each color, which is represented by an object, will become useless as the program finds a new solution.

Supposing that this graph can be colored with 100 colors: 99,900,000 objects will become useless, if the program created ten thousand of colors for each vertex.

Supposing that each color requires 16 bytes (color number, pointers, etc), the program would waste 1.5GB.

We wrote the following partial solution for standard shrinkable arrays using an explicit call to an overloaded delete[] operator.




/* author: ncomputers.org */
#include<cstdlib>
#include<iostream>
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){
    //It doesn't support objects with destructor.
    if(realloc(p,s)!=p)throw 0;
}

int main(){
    typedef int A;
    A*a=new A[100];
    //Suggested syntax:
    //delete[90]a;
    operator delete[](a,10*sizeof(A));
    delete[]a;
    return 0;
}