malloc和gcc优化2 [英] malloc and gcc optimization 2
本文介绍了malloc和gcc优化2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
while(count < 30000000){
malloc(24);
count++;
}
以上代码在我用gcc -O0编译的计算机上运行约170毫秒.但是,使用x> 0的-Ox进行编译时,优化器巧妙地指出所请求的内存将永远不会被使用,因此将其从优化的可执行文件中排除.它是如何做到的?
the above code runs in about 170 ms on my computer compiled with gcc -O0. However, compiling with -Ox where x > 0, the optimizer cleverly figures out that the memory being requested will never be used and so it is excluded from the optimized executable. How does it do this?
推荐答案
编译器发现从未使用malloc
返回值,因此对其进行了优化.如果您想即使在-O3
中也无法优化malloc
调用,则可以使用volatile
限定符:
Well the compiler sees malloc
return value is never used so it optimizes it out. If you want to prevent malloc
call to be optimzed out even in -O3
you can use the volatile
qualifier:
while(count < 30000000){
void * volatile p = malloc(24);
count++;
}
这篇关于malloc和gcc优化2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文