[英]about static array inside operator new
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. 我参加了使用C ++进行编程竞赛。众所周知,那些竞赛对执行时间有严格的限制,并且使用
malloc
或new
从堆进行分配很慢,因此我尝试在程序中重载operator new
。
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
一切正常,直到今天我试图解决一个需要约700MB内存的问题,我在在线评审中提交并遇到了
complie error
: Compiled file is too large
Then I checked my local .exe
file,and shocked to find that it's about 30MB!! 然后我检查了我的本地
.exe
文件,震惊地发现它大约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!!!! 经过长时间的调试,我发现了原因:我在
operator new
声明了一个静态数组,似乎.exe
文件的大小随该数组的大小而变化! 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. .exe
大小随MAXN
增加而增加。 Can anyone explain this to me? 有人可以向我解释吗? Thanks in advance ^_^
在此先感谢^ _ ^
Environment: 环境:
Windows 7 x32 Windows 7 x32
TDM-GCC 5.1.0 TDM-海湾合作委员会5.1.0
What's to explain? 有什么要解释的? You said you wanted to avoid taking memory from the target machine's free store, and that's exactly what you've done — the memory is now found in the static data area of your program instead.
您说过要避免从目标计算机的免费存储区中获取内存,而这正是您要做的事-现在可以在程序的静态数据区域中找到内存。 The only other place to go would be "the stack", which is typically far too small for this amount of data.
唯一要走的地方是“堆栈”,对于这个数据量而言,它通常太小了。 Basically, this is what happens when you try to go against conventional wisdom.
基本上,这就是您试图违背传统观念时发生的事情。
It was completely pointless anyway; 无论如何,这是完全没有意义的。 sure, there's a small amount of overhead in dynamic allocation, but a small amount that will not tax a modern system.
当然,动态分配中有少量的开销,但是少量的开销不会对现代系统造成负担。 If you need to allocate 700MB in one go, that will not take more time than allocating 1MB.
如果您需要一次性分配700MB,那将不会比分配1MB花费更多的时间。 So just do it.
所以就做吧。
If you need to allocate 700MB in lots of little pieces then you may have a reason to use a pre-allocated memory pool, but that can still come from new
. 如果您需要大量分配700MB,则可能有理由使用预分配的内存池,但这仍然可以来自
new
。
The real way to beat "strict execution time limits" in competitive programming is not through "tricks", but through writing an efficient algorithm that scales at the proper rate and does not perform needless data copies everywhere. 克服竞争性编程中“严格执行时间限制”的真正方法不是通过“技巧”,而是通过编写一种有效算法,该算法以适当的速率进行缩放,并且不会在所有地方执行不必要的数据复制。 Or, better yet, stop with this "competitive [broken] programming" and instead practice getting good at writing real software... with the optimisation switches turned on .
或者,更好的,停止这种“竞争力[破]编程”,而是在练习写作真正的软件越来越好......与优化开关开启 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.