I created this program. It is of no interest, but uses computing power.
Looking at the output with help objdump -d, I see three calls randand corresponding instructions movtowards the end, even when compiling with O3.
Why does the compiler not understand that the memory will not be used, and just replace the bottom half with while(1){}? I use gcc, but what interests me most is what the standard requires.
#include <cstdlib>
#include <unistd.h>
int getRand(int max) {
return rand() % max;
}
int main() {
for (int thread = 0; thread < 5; thread++) {
fork();
}
int len = 1000;
int *garbage = (int*)malloc(sizeof(int)*len);
for (int x = 0; x < len; x++) {
garbage[x] = x;
}
while (true) {
garbage[getRand(len)] = garbage[getRand(len)] - garbage[getRand(len)];
}
}
source
share