I am taking part in programming contests using C++.As is known to all,those contests have strict limits on execution time,and allocating from heap using malloc
or new
is slow,so I tried overloading operator new
in my program.
It worked fine,until today I was trying to solve a problem which requires about 700MB memory,I submitted on the online judge and got an complie error
:Compiled file is too large
Then I checked my local .exe
file,and shocked to find that it's about 30MB!!
After debugging for a long time,I found the cause:I declared a static array inside operator new
,and it seems that the size of the .exe
file varies to the size of that array!!!!
Here is my testing code:
#include <cstdlib>
const int MAXN=1e6;
struct A {
int x;
void *operator new(size_t) {
static char Pool[MAXN*sizeof(A)];
//static A Pool[MAXN];
//static A *Pool=(A*)calloc(MAXN,sizeof(A));
static A *Me=(A*)Pool;
return Me++;
}
};
int main() {
A *null=new A;
return 0;
}
the .exe
size increases as the MAXN
increases.
Can anyone explain this to me?
Thanks in advance ^_^
Environment:
Windows 7 x32
TDM-GCC 5.1.0