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?
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?
Depends on the scope of your variable.
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{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/JdTPHJUsing {0} is one of the most misleading things in C++.
int array[10]={n1, n2, n3};
This will fill the first three elements with the values in {}. The rest of the array will be initialized with the default value - 0 for int.
int array[10]={1}; // [1, 0, 0, 0...]
It's better to use
int array[10]={}; // [0, 0, 0, 0...]