Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are local variables not set to zero?

Tags:

c++

c

Since global and static variables are initialized to 0 by default, why are local variables not initialized to 0 by default as well?

like image 326
Dinesh Avatar asked Feb 21 '14 10:02

Dinesh


People also ask

Are local variables initialized to zero?

Local variables, just like instance fields, are initialized to 0 behind the scenes, by default. So they could have allowed you to use uninitialized variables.

Why local variables do not have default values?

No, local variables do not have default values. Once we create a local variable we must initialize it before using it. Since the local variables in Java are stored in stack in JVM there is a chance of getting the previous value as default value. Therefore, In Java default values for local variables are not allowed.

Are local variables initialized to zero by default C?

Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is assigned a memory location by the compiler, the default value of that variable is whatever (garbage) value happens to already be in that memory location!

Why local variables Cannot be declared as static?

In Java, a static variable is a class variable (for whole class). So if we have static local variable (a variable with scope limited to function), it violates the purpose of static. Hence compiler does not allow static local variable.


3 Answers

Because such zero-initializations take execution time. It would make your program significantly slower. Each time you call a function, the program would have to execute pointless overhead code, which sets the variables to zero.

Static variables persist for the whole lifetime of the program, so there you can afford the luxuary to zero-initialize them, because they are only initialized once. While locals are initialized in runtime.

It is not uncommon in realtime systems to enable a compiler option which stops the zero initialization of static storage objects as well. Such an option makes the program non-standard, but also makes it start up faster.

like image 135
Lundin Avatar answered Sep 29 '22 22:09

Lundin


This is because global and static variables live in different memory regions than local variables.

  • uninitialized static and global variables live in the .bss segment, which is a memory region that is guaranteed to be initialized to zero on program startup, before the program enters `main'

  • explicitly initialized static and global variables are part of the actual application file, their value is determined at compile-time and loaded into memory together with the application

  • local variables are dynamically generated at runtime, by growing the stack. If your stack grows over a memory region that holds garbage, then your uninitialized local variables will contain garbage (garbage in, garbage out).

like image 34
Andreas Grapentin Avatar answered Sep 29 '22 23:09

Andreas Grapentin


Because that would take time, and it's not always the case that you need them to be zero.

The allocation of local variables (typically on the CPU's hardware stack) is very fast, much less than one instruction per variable and basically independent of the size of the variables.

So any initialization code (which generally would not be independent of the size of the variables) would add a relatively massive amount of overhead, compared to the allocation, and since you cannot be sure that the initialization is needed, it would be very disruptive when optimizing for performance.

Global/static variables are different, they generally live in a segment of the program's binary that is set to 0 by the program loader anyway, so you get that "for free".

like image 22
unwind Avatar answered Sep 29 '22 21:09

unwind