views:

2077

answers:

7

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.

Thanks in advances. Looking more such sorts of tricks.

+6  A: 

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
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
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
@qrdl you are right
mahesh
For C++ an empty initializer list for a bounded array default-initializes all elements.
dalle
dalle, I'm not into c++ so my response was about c.
qrdl
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
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
+16  A: 

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 default-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 default-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

bk1e
A: 

Also note that some compilers (Pelles C,...) will translate

char array[10000] = {0};

into 10000 zero's in your executable.

Tolle
+2  A: 

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

lakshmanaraj
Thank you lakshmanaraj for such infamous initialization tricks.
mahesh
Welcome! since you asked for Looking more such sorts of tricks, I had provided
lakshmanaraj
You certainly can do this if you choose, but there are obvious disadvantages to relying on compiler-specific extensions like this one.
Dan Olson
@Dan Olson his question himself is asking about compiler specific and hence posted this. If you feel it is useless, i will delete.
lakshmanaraj
It's not useless, it's interesting. The caveat just deserves to be noted.
Dan Olson
It's stuff like this keeps me coming back to SO and reading more than the top few answers...
timday
+4  A: 

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(100, 0, sizeof array);
....
}

and if you omit the initialiser 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.

A: 

Note that if it's global or static it will (probably) be in the .bss segment

Liran Orevi