Detailed explanation of simulation array out of bounds under. Net

Time:2019-10-18

Preface

The previous article mentioned array overrun behavior, although the compiler did a lot of checking for us to avoid these errors.

But I think it’s still necessary to simulate the array out of bounds and feel the error.

So for. Net, how can we simulate array overrun?

I. [vs] item – > right click – > Property – > generate – > check to allow unsafe code

II. Test code


unsafe private static void OutOfIndexMini()
  {
   int* i = stackalloc int[1];

   i[0] = 0;
   //i[0] = 1;

   //double* d = stackalloc double[1];
   //d[0] = 0.01;

   int* a = stackalloc int[3];

   for (; i[0] <= 3; i[0]++)
   {
    a[i[0]] = 0;

    Console.WriteLine($"int* i = {i[0]}");

    Console.WriteLine($"int* a[{i[0]}] = {a[i[0]]}");
   }

  }

Briefly:

Unsafe keyword — supports unsafe code, which means we can use pointers.

Stackalloc keyword — allows memory to be requested from the stack.

Here are two sentences: I applied the array int [1] to memory, with the capacity of an int, and assigned the value to 1 at the same time.


int* i = stackalloc int[1];

i[0] = 0;

Next: I immediately applied for the array int [3] to memory, with a capacity of 3 ints.


int* a = stackalloc int[3];

Then note: my for loop array is out of bounds I [0] < = 3


for (; i[0] <= 3; i[0]++)
{
  a[i[0]] = 0;

  Console.WriteLine($"int* i = {i[0]}");

  Console.WriteLine($"int* a[{i[0]}] = {a[i[0]]}");
}

Initial value I [0] = 0, so enter for loop

i[0] = 0 >>> a[ i[ 0 ] ] =0 >>>a[ 0 ] =0

i[0] = 1 >>> a[ i[ 0 ] ] =0 >>>a[ 1 ] =0

i[0] = 2 >>> a[ i[ 0 ] ] =0 >>>a[ 2 ] =0

i[0] = 3 >>> a[ i[ 0 ] ] =0 >>>a[ 3 ] =0 ?

At this time, a [3] for us, the array is out of bounds, but it can still be assigned normally here. So who are we giving this to?

Let’s start with an answer:

A [3] actually crossed the boundary to I [0], that is, a [3] = I [0] = 0.

This causes that when I [0] = 3, a [3] changes I [0] to 0, and the program enters a dead cycle…

Three, validation

If I change the initial value of I [0] to 1


int* i = stackalloc int[1];

//i[0] = 0;
i[0] = 1; 

Then a [3] = I [0] = 0 after crossing the boundary, print out I [0] = 0 to indicate that the array is indeed out of the boundary and modify the value.

Then, I will declare an array that is not of type int in the middle, so that the out of bounds is not a modified I [0], so there will be no dead cycle.


int* i = stackalloc int[1];

//i[0] = 0;
i[0] = 1;

double* d = stackalloc double[1];
d[0] = 1;

int* a = stackalloc int[3];

IV. principle analysis

Because arrays are the same data type, continuous memory.

First time: I = 0, a [0] memory address 1000

The second time: I = 1, a [1] memory address 1004

The third time: I = 2, a [2] memory address 1008

The fourth time: I = 3, a [3] memory address 1012, that is to say, I [0] is crossed.

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.