144

How does the compiler fill values in char array[100] = {0};? What's the magic behind it?

I wanted to know how internally compiler initializes.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
Priyanka Mishra
  • 10,400
  • 18
  • 62
  • 75

4 Answers4

170

It's not magic.

The behavior of this code in C is described in section 6.7.8.21 of the C specification (online draft of C spec): for the elements that don't have a specified value, the compiler initializes pointers to NULL and arithmetic types to zero (and recursively applies this to aggregates).

The behavior of this code in C++ is described in section 8.5.1.7 of the C++ specification (online draft of C++ spec): the compiler aggregate-initializes the elements that don't have a specified value.

Also, note that in C++ (but not C), you can use an empty initializer list, causing the compiler to aggregate-initialize all of the elements of the array:

char array[100] = {};

As for what sort of code the compiler might generate when you do this, take a look at this question: Strange assembly from array 0-initialization

Dinei
  • 4,494
  • 4
  • 36
  • 60
bk1e
  • 23,871
  • 6
  • 54
  • 65
35

Implementation is up to compiler developers.

If your question is "what will happen with such declaration" - compiler will set first array element to the value you've provided (0) and all others will be set to zero because it is a default value for omitted array elements.

qrdl
  • 34,062
  • 14
  • 56
  • 86
  • I don't have a source, but I'm pretty sure that I read somewhere that there is no default value for array declarations; you get whatever garbage was already there. There's no sense in wasting time setting these values when you're likely to overwrite them anyway. – Ryan Fox Mar 10 '09 at 05:56
  • 11
    Ryan, if you don't set a value for the first element that the whole array is uninitialised and indeed contains garbage, but if you set a value for at least one element of it the whole array becomes initialised so unspecified elements get initialised implicitly to 0. – qrdl Mar 10 '09 at 06:05
  • 1
    For C++ an empty initializer list for a bounded array default-initializes all elements. – dalle Mar 10 '09 at 06:54
  • dalle, I'm not into c++ so my response was about c. – qrdl Mar 10 '09 at 08:00
  • qrdl, i think dalle (and me too, btw) read it as you would say "char a[100] = {}" would leave it uninitialized. but actually it is only valid in C++, and invalid syntax in C. – Johannes Schaub - litb Mar 10 '09 at 09:29
  • Well, I didn't mean that. {} is an empty initialiser (invalid in C, as you pointed out) so quite obviously it will initialise the array. – qrdl Mar 10 '09 at 10:22
  • This answer is incorrect. Implementation of `char array[100] = {0}` is *not* undefined. See the above answer by @bk1e. – Natan Yellin Apr 02 '12 at 05:35
  • 2
    @NatanYellin Where did I say that this is undefined? Please read the full answer before commenting and downvoting. – qrdl Apr 02 '12 at 06:27
  • 1
    @qrdl You're right. I misunderstood your comment about the implementation. Unfortunately, I can't change my vote now. – Natan Yellin Apr 02 '12 at 09:06
28

If your compiler is GCC you can also use following syntax:

int array[256] = {[0 ... 255] = 0};

Please look at http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Designated-Inits.html#Designated-Inits, and note that this is a compiler-specific feature.

Numeri
  • 1,027
  • 4
  • 14
  • 32
lakshmanaraj
  • 4,145
  • 23
  • 12
19

It depends where you put this initialisation.

If the array is static as in

char array[100] = {0};

int main(void)
{
...
}

then it is the compiler that reserves the 100 0 bytes in the data segement of the program. In this case you could have omitted the initialiser.

If your array is auto, then it is another story.

int foo(void)
{
char array[100] = {0};
...
}

In this case at every call of the function foo you will have a hidden memset.

The code above is equivalent to

int foo(void)
{ 
char array[100];

memset(array, 0, sizeof(array));
....
}

and if you omit the initializer your array will contain random data (the data of the stack).

If your local array is declared static like in

int foo(void)
{ 
static char array[100] = {0};
...
}

then it is technically the same case as the first one.

LPs
  • 16,045
  • 8
  • 30
  • 61