Initializing an array using {0} fails, except for the first element in some compilers

Possible duplicate:
C and C ++: partial initialization of the automatic structure

For a long time I used

char array[100] = {0}; 

to initialize all array elements to 0. However, I recently stumbled upon a compiler (Code Composer Studio from Texas Instruments) where this did not work. The statement caused only the first element to be initialized to 0.

Could this behavior be a C vs C ++ difference, a compiler difference, or is it an implementation error?

+4
source share
3 answers

This is just a mistake. In C and C ++, an array must be filled with zeros.


Since this is a small answer, it may also go overboard:

C ++ 11 ยง8.5.1 / 7:

If there are fewer initializer sentences in the list than in the aggregate, then each member that is not explicitly initialized should be initialized from an empty initializer list (8.5.4). [Example:
struct S { int a; const char* b; int c; };
S ss = { 1, "asdf" };
initializes ss.a with 1 , ss.b with "asdf" and ss.c with the value of an expression of the form int() , that is, 0 . -end example]

C99 ยง6.7.8 / 21 (sorry, he does not have C11):

If the list enclosed in brackets contains fewer initializers than the element or elements of the collection or fewer characters in the string literal used to initialize an array with a known size than in the array, the rest of the aggregate must be initialized implicitly in the same way as objects, having a static storage duration.

+11
source

There is no partial initialization in C (see 6.7.9 / 19 in C11 Standard ). An object is either fully initialized (all its bytes) or completely uninitialized.

Your compiler is not C-compatible.

Initialization must be performed in the order of the initializer list, each initializer provided for a particular subobject redefining any previously specified initializer for the same subobject; all subobjects that are not explicitly initialized must be implicitly initialized the same as objects that have a static storage duration.

+5
source

Check the compiler documentation. The appropriate C or C ++ compiler is needed to initialize all elements to 0 , but some compilers, especially in the built-in world, have parameters other than ISO (sometimes set by default!) To tune the implementation for performance reasons.

In particular, for your compiler, check if you are in COFF ABI mode:

6.13 Initializing Static and Global Variables in COFF ABI Mode

The ANSI / ISO C standard indicates that global (external) and static variables without explicit initializations should be initialized to 0 before starting the program. This task is usually performed when the program is loaded. Since the boot process is highly dependent on the specific environment of the target application system, in COFF ABI mode, the compiler itself does not provide initialization to 0; otherwise, uninitialized variables of the static storage class at run time. This requirement depends on your application.

Initialize global objects

NOTE. You must explicitly initialize all global objects that you expected to be set to zero by default by the compiler. In C6000 EABI mode, uninitialized variables are automatically initialized to zeros.

from "Optimization of the compiler TMS320C6000 v7.4 User Guide"

http://www.ti.com/lit/ug/spru187u/spru187u.pdf

+3
source

All Articles