Why aren't local variables set to zero?

Since global and static variables are initialized by default to 0, why aren't local variables initialized by default?

+3
source share
7 answers

Because such zero initializations take up runtime. This will make your program much slower. Each time you call a function, the program must execute meaningless utility code that sets the variables to zero.

Static variables are preserved for the entire program life cycle, so you can afford the luxury of zero initialization, because they only initialize once. Although the locales are initialized at runtime.

, . , .

+10

, , .

  • .bss, , , , , "main"

  • ,

  • , . , , (, ).

+9

, , , .

( , ) , , .

, ( ) , , .

/ , , , , , "".

+5

. [ 0 ], .

0

. , , OS, , . - ( Unix Windows). -, ; , , , , , . , , , , . C , .

0

, , .data( ), .bss( ).

.bss .obj, (, - ).

, exe, .bss, (exec). .bss .

, . , exe , . , .

0

, 100 , , 0 ... oops . , . 0.

0

All Articles