IL series articles four: array in il (continued)

zhaozj2021-02-16  53

IL series articles four:

Array In il (continued)

The last time I talked about the one-dimensional array of Array, this time I have to talk about the multi-dimensional array and a zigzag array.

The multi-dimensional array is actually an array of arrays (it is not as good as Array of Array). Last time I said that we can do something impossible in C #, such as defining an array, which is not from 0 but any value. Gossip, let's take a picture of a two-dimensional array.

.assembly matrix {}

// IT is a matrix with 2 dimensions

// DIM1 from 3 to 5 and Dim2 from 4 to 5

// The Matrix Like this Could Not Be defined in C #

.class private auto Ansi Beforefieldinit Matrix

Extends [mscorlib] system.Object

{

Public Hidebysig static void

Main

() CIL Managed

{

.entrypoint

.MAXSTACK 4

.locals init (int32 [,] matrix)

LDC.I4.3 // Load The Lower Bound for Dimension 1

LDC.I4.5 // Load The Upper Bound for Dim 1

LDC.I4.4 // Load The Lower Bound for Dim 2

LDC.I4.5 // Load the Upper Bound for Dim 2

Newobj Instance Void Int32 [,] ::. CTOR (INT32, INT32, INT32, INT32)

// ctor means constructor

STLOC Matrix

LDloc Matrix

LDC.I4.3 // Index of 1st Dimension

LDEx of 2nd Dimension of ldc.i4.4 //

ldc.i4.s 34 // the value

Call instance void int32 [,] :: set (int32, int32, int32) // matrix [3, 4] = 34

// a section of error code "Matrix [1,1] = 11"

/ / -------------------------------------------------------------------------------------------- ------------------

// ldloc matrix

//ldc.i4.1

//ldc.i4.1

//ldc.i4.s 11

// Call Instance Void Int32 [,] :: Set (INT32, INT32, INT32)

/ / -------------------------------------------------------------------------------------------- ------------------

// IT Will Course An Exception "System.indexOutofrangeException"

LDloc Matrix

LDC.I4.3

LDC.I4.4

Call Instance Int32 & Int32 [,] :: Address (int32, int32)

// Generate A 32 Bit Integer Instance of Matrix Element Matrix [3,4]

// in Order to Conver It to string for Output

Call instance string [mscorlib] system.int32 :: toString ()

Call void [mscorlib] system.console :: WriteLine (String) RET

}

}

In this example, there are both two-dimensional array of ordinary methods, and there is a definition method of less than 0. In fact, I don't have much good saying, most of you want to say it in the comment. The definition of the multi-dimensional array is complex than a one-dimensional array, and the special constructor needs to be invoked "Void INT32 [,] ::. CTOR (INT32, INT32, INT32, INT32)" is constructed. There is also something to pay attention to, using "newobj" here, not "newarr" used last time. In the case of our general two-dimensional array, the lower boundary starts from 0, so I actually use such a constructor "Void INT32 [0 ..., 0 ...] ::. CTOR (INT32, INT32)" In this constructor, we only need to provide two dimensions, and the lower bound is starting from 0 by default. In the middle of the example, there is a code that comes off, I use this code to verify that the non-0 lower bound array defined in IL does not start from 0. You can try this code. Once it runs, the system will throw a "system.indexoutofrangeexception", it seems that IL doesn't use to lie to us, it is indeed starting from our definition.

In general, although the definition of the two-dimensional array is complicated than a one-dimensional array, it is still a comparison. The next thing we have to see is the unregulated stuff-a jagged array. Write an article inseparable example, I still only start from the example.

//jagged.cs

Class jagged

{

Public static void

Main

()

{

int [] [] jagged;

Jagged = new int tent [3] [];

Jagged [0] = new int rt [1];

Jagged [1] = new int in

Jagged [2] = jagged [1];

Jagged [1] [1] = 11;

System.console.writeline (jagged [2] [1] .tostring ());

}

}

Through its output, it can be seen that the 3rd dimension of this zigzag array (jagged [2]) and 2nd dimension (jagged [1]) are actually the same one-dimensional array, the treatment of the second dimension is the 3rd deal with.

Some friends may already have seen it, my first example - Patrix, in fact, is also saw tooth shape, but we don't call it as a zigzag array. why? Referior to compile the above code to see how the real zigzag array is achieved. (As before, for everyone to see this convenience, I made some modifications to the reconstructed code.)

.assembly jagged {}

.class private auto Ansi beforefieldinit jagged

Extends [mscorlib] system.Object

{

Public Hidebysig static void

Main

() CIL Managed

{

.entrypoint

.MAXSTACK 4

.locals init (int32 [】 [] jagged)

LDC.I4.3

NEWARR INT32 [] // the jagged array HAS 3 DIMENSIONS

// Create a Array of int Pointer

STLOC Jagged // jagged = new int [3] [];

LDLOC Jagged

LDC.I4.0 // Dimension

LDC.I4.1 // Length of Dimension

NEWARR [Mscorlib] system.int32 // jagged [0] = new int [1]; switch.ref // set the int [] by reference

LDLOC Jagged

LDC.I4.1

LDC.I4.2

NEWARR [Mscorlib] system.int32

STELEM.REF // IT is like the first dimension

LDLOC Jagged

LDC.I4.2

// {loading pionter of 2nd Dimension by Reference

LDLOC Jagged

LDC.I4.1

LDELEM.REF

//}

Stelem.ref // set the Pointer of 3rd Dimension

LDLOC Jagged

LDC.I4.1

LDelem.ref // load jagged [1] []

LDC.I4.1 // jagged [1] [1]

ldc.i4.s 11

STELEM.I4 // jagged [1] [1] = 11

LDLOC Jagged

LDC.I4.2

LDELEM.REF

LDC.I4.1

LDELEMA [Mscorlib] System.Int32

Call instance string [mscorlib] system.int32 :: toString ()

Call void [mscorlib] system.console :: writeline (String)

RET

}

}

In a serrated array, we first define a three-line Pointer array, each Pointer pointing to an Array instance (of course, there is no). Its processing mode and one-dimensional array are very similar (essentially a collection of one-dimensional array), all of which use IL basic instructions LDELEM and STELEM, but here is passed in the referenced manner. The structure of the zigzag array is as shown in the figure below.

The zigzag array has a pointer array that plays a role in the operation of the array (I want to be the addressing of the number of components). C # Language hides the details of this column of Pointer when implementing the zigzag array, and the user does not feel its existence when using. Of course, this is to think for users, and users will not be limited to certain technical details when using a zigzag array. So far, everyone is also very clear, I will here.

转载请注明原文地址:https://www.9cbs.com/read-27969.html

New Post(0)