Array and struct from the perspective of pointer usage
Source: Internet
Author: User
What are the differences and relationships between arrays and struct arrays from the perspective of pointer usage? If we want to find their essential connection, how can we use it? The following analyzes the relationship between the two by using several instances of multi-dimensional arrays and struct. (1) One-dimensional array and struct 1. define a one-dimensional array: main () {int IntValue; // temporarily store the value int a [3] = {1, 2, 3}; int * p; p = a; IntValue = * (p + 2);} For array a, the system opens 3*4 bytes of continuous space in the memory to store. access to one-dimensional arrays is usually indicated by subscript. for example, a [0] = 1; a [2] = 3. However, to establish a connection with the following struct, we use pointers to access it. We know that for a one-dimensional array, the array name is the first address of the array. It points to the memory block of a certain data type. for example, if int a [3], address a points to a memory block of 4 bytes. obviously, moving the unit volume indicates that each unit volume is moved 4 bytes. if you use a pointer to access the table, the value is int * p; p = a. Then, * p = 0 (equivalent to a [0]). * (p + 2) = 3 (equivalent to a [2]); the memory structure is as follows: ├ ── ─ ┤ ← p
│ A [0] │
├ ─ ┄ ┤ ← P + 1
│ A [1] │
─ ┄ ┤ P + 2 │ a [2] │
├ ── IntValue; 6: int a [3] = {1, 2, 3}; 0040E9C8 mov dword ptr [ebp-10h], 1 // note [1] 0040E9CF mov dword ptr [ebp-0Ch], 20040E9D6 mov dword ptr [ebp-8], 37: int * p; 8: p = a; 0040E9DD lea eax, [ebp-10h] 0040E9E0 mov dword ptr [ebp-14h], eax9: IntValue = * (p + 2); 0040E9E3 mov ecx, dword ptr [ebp-14h] // note [2] 0040E9E6 mov edx, dword ptr [ecx + 8] 0040E9E9 mov dword ptr [ebp-4], edx 2. define a struct main () {int IntValue; struct student {int num; char sex; int score ;}; struct student jesse = {2316124029, 'M', 98 }; struct student * p; p = & jesse; IntValue = * (int *) p + 2); // description 2} the struct defines three member variables, the numbers of bytes are 4, 1, and 4, respectively. Like arrays, they also occupy a set of continuous storage space in the memory, however, it should be noted that the space they occupy is not 4 + 1 + 4 = 9. Here the "alignment principle" is involved, and the compiler provides the memory access speed, to ensure that the structure address meets the 4-byte alignment requirements, the system uses 4 bytes to store the char type, which wastes 3 bytes of space. Therefore, the total space occupied here is 12 bytes. similarly, the access to struct is generally jesse. num = 2316124029 or jesse. sex = M. similarly, we can define a pointer to access the member variable struct student * p; p = & jesse; // assign the first address of the struct to the pointer Variable p: * (int *) p) = 2316124029 (equivalent to jesse. num); * (int *) p + 2) = 98 (equivalent to jesse. score) the memory structure is as follows: ├ ── ─ ┤ ← p
│ Num │
├ ─ ┄ ┤ ← P + 1
│ Sex │
─ ┄ ┤ P + 2 │ score │
├ ── IntValue; 6: struct student7: {8: 9: int num; 10: char sex; 11: int score; 12: 13: 14 :}; 15: struct student jesse = {2316124029, 'M', 98}; 0040E9C8 mov dword ptr [ebp-10h], 8A0D3F7Dh/Note [3] 0040E9CF mov byte ptr [ebp-0Ch], 4Dh0040E9D3 mov dword ptr [ebp-8], 62h16: 17: struct student * p; 18: p = & jesse; 0040E9DA lea eax, [ebp-10h] 0040E9DD mov dword ptr [ebp-14h], eax19: IntValue = * (int *) p + 2); 0040E9E0 m Ov ecx, dword ptr [ebp-14h] // note [4] 0040E9E3 mov edx, dword ptr [ecx + 8] 0040E9E6 mov dword ptr [ebp-4], note [1] and [3] respectively correspond to the disassembly Code allocated by the system to the array and struct memory. It can be seen that, apart from the specific values assigned to the register, the Allocation Structure of the Data Types in the memory is the same. let's take a look at note [2] and note [4]. They correspond to the disassembly code used to access the two data structures. What is surprising is that they are the same, although they are different data types, there is no difference in the memory structure. note: 1. in this case, the size of the array and struct we define has some special characteristics, so that we can understand the associations between them, which does not affect the understanding of other structures. note 2. intValue = * (int *) p + 2); Is to forcibly convert the first address of the struct to the int type, in this way, you can access the members in the struct by adding or subtracting the pointer. For details, see the previously written <learn to use pointer-pointer forced conversion>. What is the relationship between multi-dimensional arrays and struct? (2) multi-dimensional array and struct array 1. defines a multidimensional array main () {int IntValue_1, IntValue_2; int x [4] [3] = {1, 2, 3}, {5, 6, 7}, {9, 10, 11 },{ 13, 14, 15 }}; int (* p) [3]; p = x; IntValue_1 = * (int *) p ); intValue_2 = * (int *) (p + 2) + 2 );}. we can understand multi-dimensional arrays in this way: a one-dimensional array of int x [4], each member is embedded with three int Type Sub-members. however, compared with a one-dimensional array, its structure in the memory is not much different. It still opens up 12*4 bytes of continuous space for storage. for multi-dimensional array access, the subscript is generally used. for example, a [0] [0] = 1; a [2] [2] = 11 we still use pointers to access members: int (* p) [3]; // indicates that P is a one-dimensional array pointing to four int types. Here, p is the first address of this one-dimensional array, as shown in the blue section in the figure. p = x; then: * (int *) p) = 1 (equivalent to a [0] [0] = 1); * (int *) (p + 2) + 2) = 11 (d is equivalent to a [2] [2] = 11) memory structure: ── ─ ┤ ← p
│ A [0] [0] │
─ ┄ ─ ┤
│ A [0] [1] │
─ ── ┄ ── ┤ │ A [0] [2] │ ── ─ ┤ ← p + 1
│ A [1] [0] │
─ ┄ ─ ┤
│ A [1] [1] │
─ ── ┄ ── ┤ │ A [1] [2] │ ── ─ ┤ ← p + 2
│ A [2] [0] │
─ ┄ ─ ┤
│ A [2] [1] │
─ ── ┄ ── ┤ │ A [2] [2] │ ← * (int *) (p + 2) + 2) ├ ── ┤ ← p + 3
│ A [3] [0] │
─ ┄ ─ ┤
│ A [3] [1] │
─ ── ┄ ── ┤ │ A [3] [2] │
├ ── ─ ┤ Disassembly code: 6: int IntValue_1, IntValue_2; 7: int x [4] [3] ={{ 1, 2, 3 },{ 5, 6, 7 }, {9,10, 11 },{ 13,14, 15 }}; 0040E9C8 mov dword ptr [ebp-38h], 10040E9CF mov dword ptr [ebp-34h], 20040E9D6 mov dword ptr [ebp-30h], 3 0040E9DD mov dword ptr [ebp-2Ch], 50040E9E4 mov dword ptr [ebp-28h], 60040E9EB mov dword ptr [ebp-24h], 70040E9F2 mov dword ptr [ebp-20h], 90040E9F9 mov dword ptr [ebp-1Ch], 0Ah0040EA00 mov dword ptr [ebp-18h], 0Bh0040EA07 mov dword ptr [ebp-14h], 109mov dword ptr [ebp-10h], 0Eh0040EA15 mov dword ptr [ebp-0Ch], 0Fh8: int (* p) [3]; 9: p = x; 10: IntValue_1 = * (int *) p); 0040EA1C lea eax, [ebp-38h] 0040EA1F mov dword ptr [ebp-3Ch], eax11: IntValue_2 = * (int *) (p + 2) + 2); 0040EA22 mov ecx, dword ptr [ebp-3Ch] 0040EA25 mov edx, dword ptr [ecx] 0040EA27 mov dword ptr [ebp-4], edx12: 0040EA2A mov eax, dword ptr [ebp-3Ch] 0040EA2D mov ecx, dword ptr [eax + 20 h] 0040EA30 mov dword ptr [ebp-8], ecx 2. define a struct array main () {int IntValue_1, IntValue_2; struct student {int num; char sex; int score ;}; struct student stu_1 [4] ={{ 00001, 'M', 506}, {00002, 'M', 125}, {00003, 'F', 405}, {00004, 'D', 758 }}; struct student * p; p = stu_1; IntValue_1 = * (int *) p); IntValue_2 = * (int *) (p + 2) + 2 );} the memory structure is as follows: ├ ── ─ ┤ ← p
│ 00001 │
─ ┄ ─ ┤
│ M │
─ ── ┄ ┤ ── ┤ ← P + 1
│ 00002 │
─ ┄ ─ ┤
│ M │
─ ── ┄ ┤ ── ┤ ← P + 2
│ 00003 │
─ ┄ ─ ┤
│ F │
─ ── ┄ ── Sampled │ 405 │ ← * (int *) (p + 2) + 2) ├ ── ─ ┤ ← p + 3
│ 00004 │
─ ┄ ─ ┤
│ D │
─ ── ┄ ── ┤ │ 758 │
├ ── ─ ┤ Disassembly; 5: int IntValue_1, IntValue_2; 6: struct student7: {8: 9: int num; 10: char sex; 11: int score; 12 :}; 13: struct student stu_1 [4] = {12345, 'M', 506}, {22222, 'M', 125}, {333333, 'F', 5063333 }, {44444, 'D', 5064444 }}; 0040E9C8 mov dword ptr [ebp-38h], 3039h0040E9CF mov byte ptr [ebp-34h], 6Dh0040E9D3 mov dword ptr [ebp-30h], 1FAh0040E9DA mov dword ptr [ebp-2Ch], 56CEh0040E9E1 mov byte ptr [ebp-28h], 4Dh0040E9E5 Mov dword ptr [ebp-24h], 7Dh0040E9EC mov dword ptr [ebp-20h], 51615h0040E9F3 mov byte ptr [ebp-1Ch], 46h0040E9F7 mov dword ptr [ebp-18h], mov dword ptr [ebp-14h], 0AD9Ch0040EA05 mov byte ptr [ebp-10h], 44h0040EA09 mov dword ptr [ebp-0Ch], 4D46FCh14: struct student * p; 15: p = stu_1; 0040EA10 lea eax, [ebp-38h] 0040EA13 mov dword ptr [ebp-3Ch], eax16: IntValue_1 = * (int *) p); 0040EA16 mov ecx, dwor D ptr [ebp-3Ch] 0040EA19 mov edx, dword ptr [ecx] 0040EA1B mov dword ptr [ebp-4], edx17: IntValue_2 = * (int *) (p + 2) + 2); 0040EA1E mov eax, dword ptr [ebp-3Ch] 0040EA21 mov ecx, dword ptr [eax + 20 h] 0040EA24 mov dword ptr [ebp-8], ecx we can see, the allocation structure of multi-dimensional arrays and struct arrays is the same in the memory. conclusion first, we can see from the above examples that there is no essential difference between arrays and struct at the memory level, and the access can also be attributed to the same method-pointer. I think the same language provides us with different data types and rules, just as you can easily use arrays to operate on the same data structure, the struct satisfies your requirements for different data operations, but in the final analysis At the underlying layer, their operations are the same. Computers only provide us with hardware platforms, such as memory, the rules you want to use depend on the mechanism provided by the advanced language. here, we can boldly imagine how structures and classes are connected? When talking about this, I think of what everyone loves to talk about at school-"The Language of XX is very popular now, so I am going to learn it." when talking about this, I am not even awake with the C language, as a result, I spent four years in college. to be precise, we entered a small circle on "language rules.
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.