Explain the Array (Continued 1)

Source: Internet
Author: User

1. Preface

In the previous article, I mainly introduced some knowledge about arrays and hope to deepen your understanding of arrays. However, I have read ivony... After my reply, I found that I still had a lot to do with it, so I had the followingArticle. In this article, I mainly focus on ivony... Some questions raised by the students are described as follows:

A. How are arrays stored in the managed heap? Is the position of the array element continuous?
B. Can a non-zero base array be converted to a zero base array?
Is the relationship between C, int [] and system. array the same type? Or is the relationship between the base class and the derived class?
D. Do ldelem not check whether the subscript is out of bounds?
E. Can multi-dimensional arrays be converted to zero-base arrays?
F, array. Copy, and copyto differ in the performance of manual copy?
G. How does the array covariant work?
H. How does an array implement generic interfaces (such as ilist <t>?
I. Must the length of each dimension of a multi-dimensional array be equal? Must it be zero-key?
J. what is indicated by the Length attribute of the array?

2. Array Memory explanation

Here, we still divide arrays into zero-base arrays and Non-zero-base arrays.

First, let's look at the memory allocation of the zero-base array.Code:

Static unsafe voidMain (String[] ARGs ){Int[] Intarr =New int[3]; intarr [0] = 1; intarr [1] = 2; intarr [2] = 3 ;}

The code itself is very simple. Next, let's take a look at it in one step. First, let's take a look.Source codeAssembly code:

 int [] intarr = new int [3]; 00000035 mov edX, 3 10000003a mov ECx, 61cd4192h 2017003f call fffb2140 00000044 mov dword ptr [ebp-44h], eax 00000047 mov eax, dword ptr [ebp-44h] 116004a mov dword ptr [ebp-40h], eax intarr [0] = 1; 116004d mov eax, dword ptr [ebp-40h] 00000050 cmp dword ptr [eax + 4], 0 00000054 ja 0000005b 00000056 call 624b6b29 0000005b mov dword ptr [eax + 8], 1 intarr [1] = 2; 00000062 mov eax, dword ptr [ebp-40h] 00000065 cmp dword ptr [eax + 4], 1 00000069 ja 00000070 running 006b call 624b6b29 00000070 mov dword ptr [eax + 0ch], 2 intarr [2] = 3; 00000077 mov eax, dword ptr [ebp-40h] 2017007a cmp dword ptr [eax + 4], 2 10000007e ja 00000085 00000080 call 624b6b29 00000085 mov dword ptr [eax + 10 h], 3} 

Here, we can clearly find that in 0x0000005b, 0x00000070 and 0x00000085, the target address of the mov operation is separated by four bytes, that is, an integer. Next we will further confirm.

After we allocate a memory address to the array, open the window to view the memory address of the array.

Next, open the memory window and view the data of the 0x015cc790 memory block:

The above is the case before assigning values to arrays. The memory data after assigning values is as follows:

Here we can clearly see that the difference between array elements is exactly four bytes, that is, an integer. Therefore, we can draw a conclusion. Elements of the zero-base array are consecutively arranged in the memory.

Next, let's take a look at the non-zero base array:

Due to space limitations, the process is as above and will not be sent again, proving:

In short, when we allocate memory for the array in the managed heap, the array occupies a continuous memory space.

We know that when we Initialize an object in the managed heap, each object needs to maintain a pointer, which directs to the next idle memory space, since the operations on arrays are usually loop traversal and other operations, the following two advantages apply to allocating arrays to a continuous memory space:

A. Reduce memory fragments

B. Memory saving, no need to maintain pointers

C. The base address does not need to be changed. You only need to change the offset, which also improves the access efficiency to a certain extent.

Next, we need to supplement the memory allocation of arrays on the stack:

Remember the keyword mentioned above, stackalloc. In my reply, someone asked if the memory allocated in the stack space was also recycled by the garbage collector? The stack space here is the same as the stack space in C language. There is no garbage collector, and each variable has its own scope. When the scope exists, the variable is automatically destroyed. The specific function execution process, see Understanding computer systems in depth.

3. Describe zero-base array and Non-zero-base array.

Let's take a look at the following code:

 
Static voidMain (String[] ARGs ){Int[,] Intarr = (Int[,]) (Array. Createinstance (Typeof(Int32),New int[] {3, 4 },New int[] {1, 1}); intarr [2, 3] = 1 ;}

There is no problem with this code. We explicitly convert the array into a strong two-dimensional array, and then directly access the index to copy it.

However, we know that for a weak array, we cannot access its elements through its subscript, but can only obtain values through setvalue and getvalue, however, we can see that the access and setting values of setvalue and getvalue are all object types, which means we need to pack or unpack them once. Is there a way to generate a strongly typed non-zero base array?

As mentioned above, we have mentioned several array types of. NET Framework:

One-dimensional zero-base array: system. int32 []. One-dimensional non-zero base array: system. int32 [*]. Multi-dimensional array: system. int32 [,].

That is to say, can we use this code to convert an array into a one-dimensional non-zero-base array?

 
Static voidMain (String[] ARGs ){Int[*] Intarr = (Int[*]) (Array. Createinstance (Typeof(Int32),New int[] {3 },New int[] {1 }));}

It turns out to be wrong. In CLR via C #, Jeffery has the following sentence:

"C # does not allow you to declare a variable of Type string [*], and therefore it is not possible to user C # syntax to access a single-dimenble, non-zero-based array."

C # cannot declare a variable of the string [*] type. Therefore, we can use the C # syntax to access a non-zero-Base One-dimensional array.

Through the above explanation, we may have an extra understanding of whether the array is an array type or a base class of the array type?

Through the above Code, we may wish to reclassify the array into "strong array" and "weak array ". Array is a weak array. Why is array a base class of all array types? I have no idea how to prove it. I just saw Jeffery say this:

"All arrays are implicitly derived from system. array ".

I think this sentence can be used to explain the problem, but I still hope that you can prove it.

Not complete ............

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.