Note: Most of these blogs are for my personal reference and at a given time, some of those might just be unpolished drafts.

Safely Indexing an Array (C to Java)

Arrays

Arrays are contiguous locations of memory set aside during compilation. (memory allocated is virtual address which is later mapped to actual memory address during run-time)

In the most simple form you need a base address and you can read starting from there. (there’s a limit how far you can go even with languages like C)

Simple Integer Array in C:

int arr[] = {1,2};
for (int i=0;i<10;i++){
printf("%d\n",arr[i]);
}

C doesn’t have a way to have a check on how far to go. This is the responsibility of the programmer. Above code runs just fine:

1
2
-713359281
646311790
-412498288
32766
1786678473
32767
0
0

Of course the values are garbage values from the memory. So it looks like in C, you can just keep reading the memory content. Not quite so.

Whenever OS schedules memory for a program it does so wisely and creates a clear boundary otherwise it would be a serious security breach. (Remember Spectre and Meltdown?)

Whenever program tries to violate it, it gives the infamous ‘segmentation fault’ error.

int arr[]={1,2};
printf("%d\n",arr[2000]); //2000 is arbitrary (basically trying to cross the limit)

would result in something like

68898 segmentation fault  ./a.out

Towards Java

This is handled in a straight forward way in java. Solution is simple but this is a super huge improvement and reduces the chances of errors.

  • When you initialize an array in java (It internally creates an object of class Class[X where X could be one of the following)

                 	[Z = boolean
                  [B = byte
                  [S = short
                  [I = int
                  [J = long
                  [F = float
                  [D = double
                  [C = char
                  [L = any non-primitives(Object)
    
  • these classes have a static final member called ‘length’ which store the length or how many memory locations are the ‘valid ones’

Example:

jshell> int[] arr = new int[]{1,2,4}; //this creates an object internally of type 'Class[I'
arr ==> int[3] { 1, 2, 4 }

jshell> arr[0]
$25 ==> 1

jshell> arr[3]
|  java.lang.ArrayIndexOutOfBoundsException thrown: 3
|        at (#26:1)

jshell> arr.getC
jshell> arr.getClass()
$27 ==> class [I

jshell> arr.length; //final static field initialized when creating the array (object)
$28 ==> 3

Something to think about

How do you find the size of an array in C?

  • Something like

      int a[] = {2,3,4,5};
      int size;
      size = sizeof(a)/sizeof(a[0])
    

If C can figure out the size this way, how come it doesn’t bother when you are trying to access something beyond its size?

Written on April 30, 2020