Home > Back-end >  Same array giving garbage value at one place and an unrelated value at the other place
Same array giving garbage value at one place and an unrelated value at the other place

Time:01-13

In the following code:

 #include<iostream>
    using namespace std;
    int main()
     {
           int A[5] = {10,20,30,40,50};

                     // Let us try to print A[5] which does NOT exist but still
      cout <<"First A[5] = "<< A[5] << endl<<endl;

                    //Now let us print A[5] inside the for loop

    for(int i=0; i<=5; i  )
    {
      cout<<"Second A["<<i<<"]"<<" = "<<A[i]<<endl;
    }

     }

Output:

output of the above code

The first A[5] is giving different output (is it called garbage value?) and the second A[5] which is inside the for loop is giving different output (in this case, A[i] is giving the output as i). Can anyone explain me why?

Also inside the for loop, if I declare a random variable like int sax = 100; then A[5] will take the value 100 and I don't have the slightest of clue why is this happening.

I am on Windows, CodeBlocks, GNUGCC Compiler

CodePudding user response:

Well you invoke Undefined Behaviour, so behaviour is err... undefined and anything can happen including what your show here.

In common implementations, data past the end of array could be used by a different element, and only implementation details in the compiler could tell which one.

Here your implementation has placed the next variable (i) just after the array, so A[5] is an (invalid) accessor for i.

But please do not rely on that. Different compilers or different compilation options could give a different result. And as a compiler is free to assume that you code shall not invoke UB an optimizing compiler could just optimize out all of your code and only you would be to blame.

TL/DR: Never, ever try to experiment UB: anything can happen from a consistent behaviour to an immediate crash passing by various inconsistent outputs. And what you see will not be reproduced in a different context (context here can even be just a different run of same code)

CodePudding user response:

In your Program, I think "there is no any syntax issue" because when I execute this same code in my compiler. Then there is no any issue likes you.

It gives same garbage value at direct as well as in loop. enter image description here

CodePudding user response:

The problem is that when you wrote:

cout <<"First A[5] = "<< A[5] << endl<<endl;//this is Undefined behavior

In the above statement you're going out of bounds. This is because array index starts from 0 and not 1.

Since your array size is 5. This means you can safely access A[0],A[1],A[2],A[3] and A[4].

On the other hand you cannot access A[5]. If you try to do so, you will get undefined behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.

So the output that you're seeing is a result of undefined behavior. And as i said don't rely on the output of a program that has UB.

So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.

For the same reason, in your for loop you should replace i<=5 with i<5.


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

  •  Tags:  
  • Related