Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++: How does int array[10]={0} work? [duplicate]

I am wondering how does int array[10]={0} really work?

Does it go all the way through the entire array like this?

for(int i=0;i<10;i++) array[i]=0;

Or is it more efficient?

like image 804
Robert Avatar asked Sep 15 '17 13:09

Robert


1 Answers

Depends on the scope of your variable.

  1. Global scope - array will be placed in the .bss segment and zeroed before call to the main function. Is it faster? It is definitely different as zeroing takes place before the main start
  2. Local scope - the {0} initialisation will be IMO faster as those internal routines are well optimised for the particular hardware. I have tested with gcc & VS and it is quicker - but of course there is no guarantee that your compiler will do it the same way. https://godbolt.org/g/JdTPHJ
like image 75
0___________ Avatar answered Sep 28 '22 15:09

0___________