Take the 2-minute tour ×

Possible Duplicate:
C and C++ : Partial initialization of automatic structure

For a long time I have been using

char array[100] = {0};

to initialize all the elements of the array to 0. However, I recently stumbled on a compiler (Code Composer Studio by Texas Instruments) where this didn't work. The statement had the effect of initializing only the first element to 0.

Could this behavior be a C vs C++ difference, a compiler difference, or is this a bug in the implementation?

share|improve this question

marked as duplicate by Alok Save, Anoop Vaidya, stusmith, AProgrammer, Björn Kaiser Jan 9 '13 at 12:04


add comment

3 Answers

up vote 11 down vote accepted

It's just a bug. In both C and C++, array should be filled with zeroes.

Since this is a small answer, might as well go overboard:

C++11 §8.5.1/7:

If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall 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, don't have C11 off-hand):

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.

share|improve this answer
Thanks. I'm amazed this would be the case for something so obvious, especially given that the compiler has been out for so long. I stumbled upon it when I was looking at some arrays and was wondering why didn't they initialize properly. –  Gustavo Litovsky Jan 8 '13 at 23:03
add comment

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

Your compiler is not C-conformant.

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject; all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.

share|improve this answer
"Your compiler is not C-conformant." I have another word for it but It might not be acceptable here :D –  Gustavo Litovsky Jan 8 '13 at 23:26
hehe, gcc without -pedantic is not a C-conformant compiler either. –  pmg Jan 8 '13 at 23:49
@pmg You can select the standard it is conformant to with the -std flag. –  FUZxxl Jan 9 '13 at 9:24
@FUZxxl: the -std= option is not enough to make gcc a standard compliant compiler. Without -pedantic there's some things it accepts that it shouldn't. You need both -std and -pedantic. –  pmg Jan 9 '13 at 16:39
@pmg Can you give me an example? –  FUZxxl Jan 9 '13 at 17:24
show 1 more comment

Check your compiler documentation. A conforming C or C++ compiler is required to initialize all elements to 0 but some compilers, particularly in the embedded world, have non-ISO options (sometimes set by default!) to tweak the implementation for performance reasons.

Specifically 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 specifies that global (extern) and static variables without explicit initializations must be initialized to 0 before the program begins running. This task is typically done when the program is loaded. Because the loading process is heavily dependent on the specific environment of the target application system, in COFF ABI mode the compiler itself makes no provision for initializing to 0 otherwise uninitialized static storage class variables at run time. It is up to your application to fulfill this requirement.

Initialize Global Objects

NOTE: You should explicitly initialize all global objects which you expected the compiler would set to zero by default. In C6000 EABI mode the uninitialized variables are zero initialized automatically

from "TMS320C6000 Optimizing Compiler v7.4 User's Guide"


share|improve this answer
Thanks, will do. –  Gustavo Litovsky Jan 8 '13 at 23:06
@GustavoLitovsky what is the MCU target? –  ouah Jan 8 '13 at 23:09
C6000 DSP, not one of the MSP430 –  Gustavo Litovsky Jan 8 '13 at 23:09
add comment
