malloc and gcc optimization 2
c, gcc, malloc, optimization
Solution
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++;
}
Problem
``` 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?