When I think I see a bug in pocc it's usually me misunderstanding something. But this one has enough weirdness that I'll risk submitting it.
// This uses a function to put some values into a local array, via a global array.
// Then it prints out the same cell from both. They should contain the same values.
// When compiled with the default "optimizations: maximize speed",
// the global array is bypassed and retains its initial values.
#include <stdio.h>
#include <string.h>
float global_values[5] = {1, 2, 3, 4, 5}; // Effect doesn't happen if array is size 4 or less.
// Not sure if it has to be floats, haven't tried with ints.
static void optim_bug_test(float *);
int main(void)
{
float local_values[5];
optim_bug_test(local_values);
printf("%f\n", global_values[0]);
printf("%f\n", local_values[0]);
return 0;
}
static void optim_bug_test(float *o)
{
float whut;
//whut = global_values[0]; // uncomment this to make things work properly.
// Compiling with "optimizations: none" also makes things work properly.
global_values[0] = 999;
global_values[1] = 998;
global_values[2] = 997;
global_values[3] = 996;
global_values[4] = 995;
// Remove the following line, and global_values gets values 999 and 998, etc., as above,
// presumably because without memcpy there's no optimization,
// but with memcpy, the step where the values get assigned is optimized away.
memcpy(o, global_values, 5 * sizeof(float));
}
// Note: I don't think this is anything specific to do with globals,
// because I first observed it with two local arrays.