Access to nonexistent array index

It was necessary to rewrite the code from one language to another, and found that when accessing a nonexistent array index, different languages ​​behave differently. Details under the cut.


image

Here is a sample code in the Structured Text language that is used for programming industrial controllers. Programs in this language can perform various actions, from controlling a traffic light to monitoring processes at a nuclear power plant. The specificity of the language and platform is observed, so that the code can look quite unusual.


VAR
   i: INT;
   OUT: INT;
   IN: ARRAY [0..4] ofINT:= 1, 2, 3, 4, 5;
END_VAR
OUT := 0;
FOR i:= 0TO4DOOUT := OUT + IN[i];
END_FOR;

What does this program do? Reads data from input signals and writes a control action to the OUT variable. The value of this variable will be 15. Now we will introduce some error into the program, specifically taking an element at a nonexistent index in the array and look at the result.


VAR
   i: INT;
   OUT: INT;
   IN: ARRAY [0..4] ofINT:= 1, 2, 3, 4, 5;
END_VAR
OUT := 0;
FOR i:= -1TO4DOOUT := OUT + IN[i];
END_FOR;

Value in variable OUT = 15

That is the same as before the error. The program did not throw an exception, but simply ignored a non-existent element. This behavior is quite acceptable for us if we do not accidentally process the exception and control the emergency rods of a nuclear reactor. The control action of the OUT variable will be the same as before an erroneous index change. If this is a short-term failure, then the system will not respond to the error and will continue to work steadily. Now imagine that the same task is performed by different programs in other programming languages ​​and compare the results.


The code without an erroneous index is executed the same way in all programming languages ​​and the OUT variable always has the value 15. We will consider only the code where the error crept in with the starting index value equal to -1.


Javascript


var IN = [ 1, 2, 3, 4, 5 ];
var OUT = 0;
for (var i = -1; i <= 4; i++) {
  OUT += IN[i];
}
  console.log('OUT = '+ OUT);

OUT = NaN

Go


package main
import"fmt"funcmain() {
    IN := [5]int{ 1, 2, 3, 4, 5 }
    OUT := 0for i := -1; i <= 4; i++ {
        OUT += IN[i]
        }
     fmt.Printf("OUT = %d", OUT)
}

panic: runtime error: index out of range

Java


publicclassMyClass{
    publicstaticvoidmain(String args[]){
    int[] IN = {1, 2, 3, 4, 5};  
    int OUT = 0;
    for (int i = -1; i <= 4; i++)
         OUT += IN[i];
     System.out.printf("OUT = %d", OUT);     
    }
}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

Php


<?php
$IN = [1, 2, 3, 4, 5];
$OUT = 0;
for ($i=-1; $i<=4; $i++) {
    $OUT += $IN[$i];
}
echo('OUT = '.$OUT);

OUT = 15
PHP Notice: Undefined offset: -1

Python 3


IN = [ 1, 2, 3, 4, 5 ];
OUT = 0;
for i in range(-1, 5):
  OUT += IN[i];
print('OUT = {0:1d}'.format(OUT));

OUT = 20

In Python, negative indexes are supported and numbering comes from the end of the array.


C / C ++


#include<stdio.h>intmain(){
   int IN[]= {1,2,3,4,5};
   int OUT=0;
   int i; 
   for (i=-1; i<=4; i++) {
    OUT += IN[i];
   } 
   printf("OUT = %i", OUT);
   return0;
}

With C ++ a separate story. If you want to check this example on popular sites, you will get the following results:


http://codepad.org


OUT = -143484461

https://ideone.com/ et al.


OUT = 15

https://www.jdoodle.com


OUT = 14

In this article, I will not go into details of which C / C ++ compilers on which platforms these sites use. I would be very happy if you share your opinions in the comments.


C sharp


usingSystem;
class Program
{
    static void Main()
    {
        int[] IN = newint[] { 1, 2, 3, 4, 5 };
        intOUT = 0;
        for (int i = -1; i <= 4; i++)
        {
            OUT += IN[i];
        }
        Console.Write("OUT of IN + y = "+ OUT);
    }
}

Unhandled Exception:
System.IndexOutOfRangeException: Index was outside the bounds of the array.

findings


When writing code in any programming language, it is necessary to take into account the specifics of working with data structures in the language. Also consider all the places in the code where the program can "fall." Often, after an exception, the program code can no longer be returned to the error line, and below the code important actions can be performed. Therefore, not all languages ​​are suitable for solving certain tasks or require additional checks and code branching. In the languages ​​of the IEC standard, there is protection against primitive software errors, since it often happens that there is no one to overload the controller's program when there is a failure, and this operation can also be fatal in some situations.


Also popular now: