问题
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.
回答1:
Yes, this is allowed, and should initialize the array to zero. C99, §6.7.8 p10:
If an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these rules.
and p21:
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.
Report the bug to your compiler vendor.
回答2:
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:
/* IAR, global variable, initialized to 0 upon program start */
int Counter;
However, the TI CCS compiler does not pre-initialize these variables; therefore, it is up to the application to fulfill this requirement:
/* CCS, global variable, manually zero-initialized */
int Counter = 0;
回答3:
The C standard says (6.7.8.21):
If there are fewer initializers in a brace-enclosed list than there are element of members of an aggregate [...], the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
In 6.2.5.21:
Array and structure types are collectively called aggregate types.
In other words, your code is OK.
回答4:
static uint8_t array[2] = {0};
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.
回答5:
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.
来源:https://stackoverflow.com/questions/8138589/does-static-extern-uint8-t-array2-0-conform-to-the-ansi-c-specification