Different values between variable without value and variable with null value?

Asked

Viewed 66 times

2

#include<stdio.h>


int main ( void ){
int die1[7];
int die2[7];
int sortedDie, i;


srand(time(NULL));
printf("Rolling die 1\n");

for( i = 0; i < 6; i++){


    die1[i] = 1 + rand()%6;



}
printf("Rolling die 2\n");

for( i = 0; i < 6; i++){

    int dieValue2;
    dieValue2 = 1 + rand()%2;
    die2[i] = dieValue2;

}


printf("The numbers sorted in die 1 are: \n");
for( i = 0; i < 6; i++){

    printf("%d\n", die1[i]);
}
printf("The numbers sorted in die 2 are: \n");
for( i = 0; i < 6; i++){
    printf("%d\n", die2[i]);

}
printf("The sum of those numbers are: \n");
int sum = 0;  /* essa parte aqui */
for( i = 0; i < 6; i++){


    sum = sum + die1[i] + die2[i];

}
printf("%d", sum);
}

Guys, when I run this program with the variable sum only stated as int without having value attributed to it, that is to say: int sum; the program returns a sum that has more values. However, when I assign a null value to sum as it was done in the commented part of the code, it returns me the right sum. Why it happens?

1 answer

4


The name of this is initialization. So you initialize the variable:

int sum = 0;

That is, initialization occurs when you declare a variable already assigning it an initial value.

Already this way, you declare it without initializing:

int sum;

In his for, you have it:

    sum = sum + die1[i] + die2[i];

That is, it modifies the value of sum based on the previous value. But, in the first iteration of this for, if you stated sum without initializing, what would be her first value?

The answer is that its initial value is anything. Often this "anything" is zero, but may not be. Often this value is called by the affectionate name of "rubbish".

Okay, but maybe you’re thinking, why does C have this bizarre behavior? The answer is in the way that memory is organized.

In C, each variable is allocated somewhere in memory. When a program is running, a certain part of it can use some portion of the memory to do something and as soon as it finishes, it frees up that portion of memory. It happens that releasing is not cleaning. The old values of the function/procedure/some-other-thing that has ended get dropped there in memory. After some time, another part of the program allocates that memory region (which is dirty) again and starts using it.

That is, what happens is that the original content of the variable sum is dirty, and contains information that is remnant of some other part of something else that was using that piece of memory for some other purpose.

And then comes the question: Why is it not clean when the variable sum is allocated? The answer is because this is almost always unnecessary. That’s what booting is for. The initialization is not only to define what is the initial value of the variable, but it is also to delete/overwrite any junk that is there. And then you ask, why isn’t he clean anyway? The answer is because it would mean that it could end up being cleaned twice, one automatically and one by initialization or by first assigning a value to variable, which means that if the compiler generated code to wipe the variable automatically before using, the result would be a waste of performance.

And why didn’t whoever used it last leave it clean? The answer is because this code, whatever it is, didn’t have it cleaned! And cleaning should be unnecessary, since anyone who uses that part of the memory then would probably/should overwrite its content with something else.

In conclusion, using dirty values of memory is a bad programming practice, it is considered a bug. In practice, you can’t know for sure what the old value is, and whatever it is, it probably wouldn’t be any useful value. This value is garbage, dirt, something about which we know nothing and which we cannot control and which is therefore something useless. Never use dirty memory values.

Finally, several code analysis tools will generate alerts warning that the variable sum may not be properly initialized - Maybe the compiler itself is giving you a warning/warning (Warning) about this problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.