I'm trying to figure out why initializing an array with int buckets[AS] = { 0 };
is not setting all the elements to zero. Perhaps it is a compiler optimization, in which case would volatile
be acceptable? volatile int buckets[AS] = { 0 };
.
Second question, why is only the first element initialized to 1
here? Doesn't this fall under:
C99 [$6.7.8/21]
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
Problem code:
#include <stdio.h>
#define AS 100
int buckets[AS] = { 1 };
int main()
{
int i;
for(i = 0; i < AS; i++) {
printf("%d", buckets[i]);
}
return 0;
}
EDIT:
Changing optimization level from -o0 to default eliminates the issue. Working with STM32 Kiel IDE micro-controllers.
EDIT EDIT:
This is the code causing trouble. Compiler optimizing away for loop
?
// Initialize this to 1 as when initializing to 0 and clearing some elements are non-zero
// Possibly a compiler bug? Changing optimization level from -o0 to default eliminates the issue
uint16_t pulse_time_hist[NUM_BUCKETS] = {1};
// Resets all values stored in the histogram
void clearHist() {
unsigned int i;
for (i = 0; i < NUM_BUCKETS; i++) {
pulse_time_hist[i] = 0;
}
}
EDIT EDIT EDIT:
I'm not a compiler guy at all btw. Here is my compiler control string
-c -cpu Cortex-M4.fp -D__EVAL -g -O0 -apcs=interwork -split_sections ...
Running without -c99 currently.