while(count < 30000000){
malloc(24);
count++;
}
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?
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++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With