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.

9
  • 3
    You're not initializing foo[4] anywhere in either code snippet. Thus printing foo[4] is UB and you're not guaranteed any particular result. Commented Oct 23, 2019 at 12:45
  • @Blaze Yes, i know. But why does it cause undefined behavior either? I´ve defined the array proper. Commented Oct 23, 2019 at 12:46
  • 3
    Unless you explicitly initialize local variables (including arrays) their values and contents will be indeterminate. In C++ even reading indeterminate values is undefined behavior (unlike C where it only might be, so please pick the language you're really programming in). Commented Oct 23, 2019 at 12:47
  • 4
    Yes, you've defined an array, but you never defined it's value. That means it has an undefined valued. Commented Oct 23, 2019 at 12:47
  • 1
    Yes, it's the same for all local non-static variables. Global variables or local static variables will be initialized though. Commented Oct 23, 2019 at 13:14

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

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?
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?. There, you'll find references to the Standard too. Hope that helps!
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.
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.

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.