0

I made some tests in VSC to check the behaviors of arrays. I´ve encountered in the output of one test the issue, that there was apparently happend undefined behavior despite the array element was defined proper, but just not initialized (with proper i mean the array element was defined with the array itself, not additionally over the bounds of the array which causes well-known undefined behavior).

Here is my code, and the output of it below it:

The issue is about the output of foo[4] which is 8 instead of 0.

#include <stdio.h> 

int main()
{
    int foo[12];
    int i;

    foo[5] = 6; 
    foo[6] = 7;
    foo[7] = 8;
    foo[8] = 9;
    foo[9] = 10;
    foo[10] = 11;
    foo[11] = 12;

    for(i=0 ; i<=11 ; i++)
    {
        printf("foo[%d] = %d\n",i,foo[i]);
    }
}

Output:

foo[0] = 0
foo[1] = 0
foo[2] = 0
foo[3] = 0
foo[4] = 8
foo[5] = 6
foo[6] = 7
foo[7] = 8
foo[8] = 9
foo[9] = 10
foo[10] = 11
foo[11] = 12

Thereafter i tried it else and wanted to see if foo[5] might is influenced also, if i do not initialise it as well, but it wasn´t the case. foo[4] still had the wrong value btw:

#include <stdio.h> 

int main()
{
    int foo[12];
    int i;

    // foo[5] = 6; 
    foo[6] = 7;
    foo[7] = 8;
    foo[8] = 9;
    foo[9] = 10;
    foo[10] = 11;
    foo[11] = 12;

    for(i=0 ; i<=11 ; i++)
    {
        printf("foo[%d] = %d\n",i,foo[i]);
    }
}

Output:

foo[0] = 0
foo[1] = 0
foo[2] = 0
foo[3] = 0
foo[4] = 8
foo[5] = 0
foo[6] = 7
foo[7] = 8
foo[8] = 9
foo[9] = 10
foo[10] = 11
foo[11] = 12

My Question is: Why is happening here undefined behavior at foo[4]? The array is defined proper with 12 elements.

2 Answers2

2

Why is happening here undefined behavior at foo[4]? The array is defined proper with 12 elements

It is because of out of luck in one word.

That is, when you define local array as below.

int foo[12];

each element of foo will be having indeterminate values until explicitly initialized.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • I did not know this, thank you very much. Do you have any documentation reference to this? Maybe in the Standards? Does the same thing apply to normal variables, when i do not initialise them? – RobertS supports Monica Cellio Oct 23 '19 at 13:06
2

The issue is about the output of foo[4].

Not only.

The issue is with every uninitialized element of your array, which you later access - those elements have indeterminate (garbage) values.

You don't initialize the 5 first elements of your array, but you access them in the for loop, which invokes Undefined Behavior (UB).

You were just (un)lucky that the first four elements of you array happened to be, today, in your machine, at this time, initialized to the value that you would like them to be.

Here is the output I got by running your code online:

foo[0] = 0
foo[1] = 0
foo[2] = 4195741
foo[3] = 0
foo[4] = 0
foo[5] = 6
foo[6] = 7
foo[7] = 8
foo[8] = 9
foo[9] = 10
foo[10] = 11
foo[11] = 12
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Thank you very much for this information. I did not know that this is causing undefined behavior. Does the same thing apply to not initialised (normal) variables? – RobertS supports Monica Cellio Oct 23 '19 at 13:03
  • There I was a time I wasn't aware of that either @RobertS, and that's why I upvoted your question! :) As for the local (the normal as you say) variables, the same philosophy applies too! Check this [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value). There, you'll find references to the Standard too. Hope that helps! – gsamaras Oct 23 '19 at 14:56
  • In C89, an indeterminate value was defined as either an unspecified value or a trap representation, and the notion that accessing indeterminate values invoked UB was confined to a non-normative annex describing *general* constructs that may invoke UB. Without knowing whether `int` has trap representations, one couldn't know whether the above would have defined behavior. On platforms where `int` has no trap representations, however, the above would output Unspecified values. – supercat Oct 23 '19 at 15:19