4

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?

Community
  • 1
  • 1
Gustavo Litovsky
  • 2,457
  • 1
  • 29
  • 41

3 Answers3

11

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.

GManNickG
  • 494,350
  • 52
  • 494
  • 543
  • 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 08 '13 at 23:03
5

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.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • "Your compiler is not C-conformant." I have another word for it but It might not be acceptable here :D – Gustavo Litovsky Jan 08 '13 at 23:26
  • hehe, `gcc` without `-pedantic` is not a C-conformant compiler either. – pmg Jan 08 '13 at 23:49
  • @pmg You can select the standard it is conformant to with the -std flag. – fuz Jan 09 '13 at 09: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 09 '13 at 16:39
  • @pmg Can you give me an example? – fuz Jan 09 '13 at 17:24
  • @FUZxxl: even [ideone accepts a invalid program](http://ideone.com/h5XyFL), but if we ask for "C99 strict" for the same program, [ideone gives an error](http://ideone.com/vzhGsV). I tested the `-std=c89`, `-std=c99`, `-std=c11` and `-pedantic` flags on my machine with gcc 4.7.2. – pmg Jan 09 '13 at 18:51
4

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"

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

ouah
  • 142,963
  • 15
  • 272
  • 331