# 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;

i = 0;
//i = 1;

//double* d = stackalloc double;
//d = 0.01;

int* a = stackalloc int;

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

Console.WriteLine(\$"int* i = {i}");

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

}``````

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  to memory, with the capacity of an int, and assigned the value to 1 at the same time.

``````
int* i = stackalloc int;

i = 0;``````

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

``````
int* a = stackalloc int;``````

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

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

Console.WriteLine(\$"int* i = {i}");

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

Initial value I  = 0, so enter for loop

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

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

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

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

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

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

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

Three, validation

If I change the initial value of I  to 1

``````
int* i = stackalloc int;

//i = 0;
i = 1; ``````

Then a  = I  = 0 after crossing the boundary, print out I  = 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 , so there will be no dead cycle.

``````
int* i = stackalloc int;

//i = 0;
i = 1;

double* d = stackalloc double;
d = 1;

int* a = stackalloc int;`````` IV. principle analysis

Because arrays are the same data type, continuous memory.

First time: I = 0, a  memory address 1000

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

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

The fourth time: I = 3, a  memory address 1012, that is to say, I  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.

## Pilot for Web

First of all, thank you @ Qibing @ zujianguo for their investment in FFW’s pre research! background Google launched the flutter for web on the latest Google I / O, aiming to further solve the problem of one-time code and multi-terminal operation. Flutter for web is still in the early pilot version and is not […]