I've a question regarding the following code:
#include "all_needed.h"
static uint8_t array[2] = {0};
void main(void)
{
...
}
Is a (module) global array allowed to be initialized as above for having each member set to zero while being ANSI C conform?
I've got a problem in Code Composer 5 (MSP430 Project) where I had to change it into
static uint8_t array[2] = {0, 0};
for a correct initialization of the 2nd member of the array.
I work with PIC micros, so your mileage may vary...
There are different startup libraries I can link in. One will not initialize any RAM. One will clear all RAM to 0. Another with initialize the variables normally.
Take a look at the linker file and see what it is doing.
The C standard says (6.7.8.21):
In 6.2.5.21:
In other words, your code is OK.
Yes, this is allowed, and should initialize the array to zero. C99, §6.7.8 p10:
and p21:
Report the bug to your compiler vendor.
Thanks for all your answers! I've further investigated and found out that the Compiler is non-compliant as described in http://www.ti.com/lit/pdf/SLAU157 in section B.5.1 "Initializing Static and Global Variables":
Appendix B: IAR 2.x/3.x/4.x to CCS C-Migration
B.5 Other Differences
B.5.1 Initializing Static and Global Variables
The ANSI/ISO C standard specifies that static and global (extern) variables without explicit initializations must be pre-initialized to 0 (before the program begins running). This task is typically performed when the program is loaded and is implemented in the IAR compiler:
However, the TI CCS compiler does not pre-initialize these variables; therefore, it is up to the application to fulfill this requirement:
According to the C Standard, this will initialize both members of
array
to 0. If your compiler doesn't zero them, then it is in violation.