Bobby
Bobby

Reputation: 121

Automatic initalization of local variables

According to the C standard, the local variables declared in the local scope would carry garbage value until they are explicitly initialized. In contrast to that, if the variable is declared in the global scope, it's implicitly defined to 0.

All of that is being holding true only when I'm declaring one or two variables, such as int w, x. But when I'm declaring three or more variables, few are automatically being defined to 0. Which is completely against the standard, I believe.

Note: I ran it multiple times and it does that every single time as if it's deliberate.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int w, x, y, z;
    printf("%d %d %d %d\n", w, x, y, z);

    return EXIT_SUCCESS;
}

GCC's Output: 0 0 1623616696 32764

Clang's Output: 849564872 0 0 29759

Upvotes: 0

Views: 110

Answers (2)

Lundin
Lundin

Reputation: 212921

What the standard says is that the values of uninitialized local variables are indeterminate. This means that the program cannot make any assumptions about what value they might hold. It is unspecified behavior - you might get any value. Zero is "any value", so that is as expected. In practice you are likely just reading some left-overs from the part of the program that used that part of the stack before you did.

Accessing the variable multiple times might even result in different values each time, why such values are sometimes referred to as "wobbly values".

On some systems, certain variable types (not int but things like pointers or floating point) may also hold trap representations = forbidden values. In which case accessing an indeterminate value would invoke reading a trap representation, which is undefined behavior and could result in anything, normally some signal or hardware exception being raised.

Note that the formal terms unspecified vs undefined behavior are different: unspecified means "anything within the given premises might happen and the compiler need not document how", whereas undefined means "literally anything can happen including programming crashes or the program going haywire".

Some compilers for example have the bad habit to zero out the stack during debug release. Nothing in the C standard prevents them from doing so however, since the values are indeterminate and how/if they are written to is unspecified behavior.

A bigger concern here is that your program is also invoking undefined behavior for accessing an uninitialized local variable that never had its address taken. When is using an uninitialized variable undefined behavior? Since it is undefined behavior, unlike accessing an indeterminate value, this could cause things like crashes or abnormal program behavior.

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109482

The variables rightfully so contains undetermined values.

Compare it with the language design in java: object fields are zeroed (or null or false) and local variables are also uninitialized. Here the compiler gives a message when some variable might not be initialized.

That is intentional. So the programmer does not erringly work with default values when they forget setting a value.

Now in C one should set a correct warning level too. And declaration at first use (and possibly immediate initialisation) I grew fond of too.

The effect you saw might be zeroed heap, and previous calls messing up local variables with prior return addresses and other garbage. Keep to lint quality.

Upvotes: 4

Related Questions