How does C++ proceed for += with a variable without value? -


this question has answer here:

i learning c++ , had question "weird" things noticed while testing have learnt.

so use :

int marc[9]; int sum = 0;  (int x =0; x < 9; x++) {     marc[x] = x*4;     sum += marc[x]; } cout << sum << endl; 

and normal result 144

however, if changed

int sum = 0; 

to

int sum; 

i got crazy result 19557555002

so wondering happening in second case?

thanks answer :)

you operating on uninitialized memory in second case. non-static local variables not initialized 0 runtime in other languages, if need sum have defined value, must initialize yourself. 19557555002 integer interpretation of bytes present @ memory address allocated sum.

further reading: what happens declared, uninitialized variable in c? have value?


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -