-2
int main()
{     
   int a=10, b=5, c, i, s[4], t[4], u=0;

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

    s[i] = i;
    t[i] =i;
} 

     printf("s:t\n");
     for (i=0; i<=4; i++)
     printf("%d:%d\n", s[i], t[i]);
     printf("u = %d\n", u);
     c=a+b;
     printf("c=%d",c);
}

In the above code why does s[4]'s value stored in c and t[4]'s value in s[0] and not anyplace else.How does the memory allocation occur when out of bound condition appears in c?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Why should there be a special treatment for elements of an array that aren't even legally part of that array? The element will be stored right after the previous element. No magic here. Whatever is located next to your array will get changed. Or a SEGFAULT may happen or anything else. That's the funny thing with UB (Undefined Behaviour) – Gerhardh Jun 15 '18 at 08:43
  • The compiler arranges objects in memory any way it wants to, in a sense. It may rearrange objects to serve alignment purposes or for other reasons. Failing any particular need for one arrangement or another, it might arrange them in order of definition, alphabetically, or effectively randomly according to some hash function it uses to organize names. For practical purposes, you should not expect or rely on any pattern. Any program that indexes outside an array is violating the rules of C and is broken. – Eric Postpischil Jun 15 '18 at 08:47
  • If you know that `s[4]` and `t[4]` are out of bounds, why are you asking where they go? What difference does it make if they go one wrong place, versus another wrong place? It's still a wrong place. I appreciate your curiosity, and that you're trying to understand how things work. But in this case, it's like asking, "I ran across a busy street. Why did the red car run over me, instead of the blue truck?" – Steve Summit Jun 15 '18 at 13:35

1 Answers1

1

It is undefined behaviour when array index is out of bound.

C11 Standard:

J.2 Undefined behavor

An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6).

Addition or subtraction of a pointer into, or just beyond, an array object and an integer type produces a result that points just beyond the array object and is used as the operand of a unary * operator that is evaluated (6.5.6).

msc
  • 33,420
  • 29
  • 119
  • 214
  • For future reference, this is not how you properly quote the standard. Annex J is an informative summary of the standard, it is not normative text. It points to 6.5.6 which is the relevant text to cite, in this case 6.5.6/8 `If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.` Optionally, you could also add a reference to where the C standard says that subscripting is equivalent to using addition, from 6.5.2.1/2. – Lundin Jun 15 '18 at 08:13