title: In C, why can I see a value written past the end of an array in a different variable?
body: <p>I've spent my spare time doing some test, fun things and implementing some various things like simple algorithm, data-structure for my personal joy in C these days....</p>
  
  <p>But, I ended up finding out something interesting to me. I do not know why this result is happening until now..</p>
  
  <p><code>max_arr_count_index</code> is assigned depending on <code>arr[5]</code> value, which is past the end of the array +1.</p>
  
  <p>Is there someone who can explain this to me? I know it should not be. I assigned the value the past one index of the array (here, arr[5] = 30 in the problem case)
  and it's not safe, and it is <strong>undefined behavior</strong> as defined by the standard.</p>
  
  <p>I am not gonna do the same thing in the real field, But, I just want to get more under the hood here.</p>
  
  <p>LLVM and GCC have given me the same result.</p>
  
  <p>Code and result is below:</p>
  
  <p><strong>[No Problem case: I do not assign the value past end of the index]</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int arr[] = {11,33,55,77,88};
  int max_arr_count_index = (sizeof(arr) / sizeof(arr[0]));
  
  // print all
  void print_all_arr(int* arr)
  {
      // just print all arr datas regarding index.
      for(int i = 0; i &lt; max_arr_count_index; i++) {
          printf("arr[%d] = %d \n", i, arr[i]);
      }
  }
  
  int main(int argc, const char * argv[]) {
      // insert code here...
      printf("[before]max_arr_count_index : %d\n", max_arr_count_index);
      printf("[before]The original array elements are :\n");
      print_all_arr(arr);
      arr[0] = 1;
      arr[1] = 2;
      arr[2] = 3;
      arr[3] = 4;
      arr[4] = 5;
      // arr[5] = 1000;
      printf("[after]max_arr_count_index : %d\n", max_arr_count_index);
      printf("[after]The array elements after :\n");
  
      print_all_arr(arr);
  
      return 0;
  }
  </code></pre>
  
  <p>No problem result is below:</p>
  
  <pre><code>[before]max_arr_count_index : 5
  [before]The original array elements are :
  arr[0] = 11 
  arr[1] = 33 
  arr[2] = 55 
  arr[3] = 77 
  arr[4] = 88 
  [after]max_arr_count_index : 5
  [after]The array elements after :
  arr[0] = 1 
  arr[1] = 2 
  arr[2] = 3 
  arr[3] = 4 
  arr[4] = 5 
  Program ended with exit code: 0
  </code></pre>
  
  <p><strong>[Problem case: I assigned the value past end of the index]</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int arr[] = {11,33,55,77,88};
  int max_arr_count_index = (sizeof(arr) / sizeof(arr[0]));
  
  // print all
  void print_all_arr(int* arr)
  {
      // just print all arr datas regarding index.
      for(int i = 0; i &lt; max_arr_count_index; i++) {
          printf("arr[%d] = %d \n", i, arr[i]);
      }
  }
  
  int main(int argc, const char * argv[]) {
      // insert code here...
      printf("[before]max_arr_count_index : %d\n", max_arr_count_index);
      printf("[before]The original array elements are :\n");
      print_all_arr(arr);
      arr[0] = 1;
      arr[1] = 2;
      arr[2] = 3;
      arr[3] = 4;
      arr[4] = 5;
  
      /* Point is this one. 
         If I assign arr[5] 30, then, max_arr_count_index is changed also as            
         30. if I assign arr[5] 10000 max_arr_count_index is assigned 10000.
      */
  
      arr[5] = 30;
  
      /* Point is this one. 
         If I assign arr[5] 30, then, max_arr_count_index is changed also as            
         30. if I assign arr[5] 10000 max_arr_count_index is assigned 10000.
      */
  
      printf("[after]max_arr_count_index : %d\n", max_arr_count_index);
      printf("[after]The array elements after arr[5] is assigned 30 :\n");
  
      print_all_arr(arr);
  
      return 0;
  }
  </code></pre>
  
  <p><strong>Result is below :</strong></p>
  
  <pre><code>[before]max_arr_count_index : 5
  [before]The original array elements are :
  arr[0] = 11 
  arr[1] = 33 
  arr[2] = 55 
  arr[3] = 77 
  arr[4] = 88 
  [after]max_arr_count_index : 30
  [after]The array elements after arr[5] is assigned 30 :
  arr[0] = 1 
  arr[1] = 2 
  arr[2] = 3 
  arr[3] = 4 
  arr[4] = 5 
  arr[5] = 30 
  arr[6] = 0 
  arr[7] = 0 
  arr[8] = 0 
  arr[9] = 0 
  arr[10] = 0 
  arr[11] = 0 
  arr[12] = 0 
  arr[13] = 0 
  arr[14] = 0 
  arr[15] = 0 
  arr[16] = 0 
  arr[17] = 0 
  arr[18] = 0 
  arr[19] = 0 
  arr[20] = 0 
  arr[21] = 0 
  arr[22] = 0 
  arr[23] = 0 
  arr[24] = 0 
  arr[25] = 0 
  arr[26] = 0 
  arr[27] = 0 
  arr[28] = 0 
  arr[29] = 0 
  Program ended with exit code: 0
  </code></pre>