-2

Line 1:

int temp2 [4];
for(j=0;j<=4;j++){
    for(i=0;i<=4;i++) {   
        temp2[j] = temp2[j] + election[i][j];
    }   
 }
 printf("%d",temp2[3]);

In this above example, the nested for loops sums up the columns of a 5x5 table. However, the last column is always summed up incorrectly.

When I changed Line 1 to:

int temp2[4] = {0};

All of a sudden the calculations came out perfectly! What exactly happened between the initialization of the array?

If an array is uninitialized, does that mean its last element will always contain some garbage value?

maskacovnik
  • 3,080
  • 5
  • 20
  • 26
JackW
  • 49
  • 6

5 Answers5

3

If an array is uninitialized, does that mean its last element will always contain some garbage value?

Whether they contain a garbage value or any value at all is a matter of interpretation, because any attempt to read from such uninitialized variables is undefined behaviour (UB)1. So, you can't even check what is stored in those variables. In practice, UB may manifest itself as "garbage" values being printed out, but technically anything could happen.

Also note that you are accessing the array out of bounds. That is also UB.

for(j=0;j<=4;j++){ /* Oops! Should be j < 4 */

[1] This is a simplification. In practice, implementations can assign unspecified values to uninitialized variables, or use trap representations. This means the results or reading an uninitialized variables could simply be unspecified. But they could also do whatever a given implementation does when a trap value is read. I find it easier to lump everything under UB. See related question: What happens to a declared, uninitialized variable in C? Does it have a value?

Community
  • 1
  • 1
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
2

Yes, an uninitialized array will contain unpredictable garbage. You must initialize it.

Georg P.
  • 2,785
  • 2
  • 27
  • 53
0

If an array is uninitialized, does that mean its last element will always contain some garbage value?

If the array is not global or static, yes it will contain the garbage value. The BSS initializes the static or globalvariable or memory location to default values unless the variable is initially assigned some value.

Thus, the information at the memory location is overwritten by compiler the program may crash.

Now, when you are accessing that memory what you get is undefined behavior.

Also, note that the snippet is accessing the array out of bounds. So, please use:

int temp2 [4];
for(j=0;j<=3;j++){
   for(i=0;i<=3;i++) {

or

int temp2 [4];
for(j=0;j<4;j++){
   for(i=0;i<4;i++) {
TryinHard
  • 4,078
  • 3
  • 28
  • 54
0

First, As Jonathan Leffler mentioned, You are looping too much - You initialized an array of 4 but looping 5 times. Try changing your outer loop to j<4 and inner loop to i<4:

Line 1:  int temp2 [4];
for(j=0;j<4;j++){
  for(i=0;i<4;i++) {
    temp2[j] += election[i][j];
  } 
}
printf("%d",temp2[3]);

You should also initialize your array, as you can't predict what is in memory at the point of creation (also depends on what language you're using)

ZackZ
  • 21
  • 4
-1

An uninitialized array will contain garbage data. I've notice that in the last version of visual studio if the array is of simple data types such as int than the compiler/ide automatically initialize it to zeros, but I wouldn't rely on it. As a rule, I recommend you initialize your arrays before you start doing operations like summing etc.

Zachi Shtain
  • 826
  • 1
  • 13
  • 31