Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the relationship between array, pointer and memory in C language?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the C language array and pointer, what is the relationship between memory, I believe that most people do not know much, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!

First of all, we demonstrate the relationship between the one-dimensional array and the first-level pointer. We often use the first-level pointer to access the one-dimensional array. Only when we have a good understanding of memory can we understand their direct relationship.

1. The array name is the first address of the array

two。 The first address of the array is still obtained by taking the address of the array name.

3. The access mode of the array is actually the addressing access of the first address + offset.

We will define many variables in the program, including basic types and custom types.

When I am developing, my access to memory is to read and write memory through variable name assignment.

But if you see the symbolic names of the direct variables, you will not be able to understand memory.

Each type has a byte width

Char 1 byte short 2 byte int byte float 4 double 8, other custom types also have a corresponding size, for reading and writing memory through variable names, we need to automatically form a mapping relationship in our mind.

Int / write 4 bytes of data to the memory of a certain section of memory address bit x 10intp = (int) 20; / / write 4 bytes of data to the memory of a certain section of memory address bit x1 20intact * p = (int**) 30 burst / write 4 bytes of data to the memory of a section of memory address bit x2 30int b = a / / read a section of memory with address x read 4 to byte data value write a section of memory with the first address b with a width of 4

For assignment reading and writing, you need to automatically divide the brain into blocks of memory and then compare the variable symbol name to the first address (and width), ignoring the concept of data type, and only focusing on the first address + data width for a variable.

For using pointers to access the array, as shown below

Basically, you can see that using array arr directly is less than using pointer p.

Arr is the first address + offset and then read or write directly to this address.

The operation mode of the pointer first needs to get the value stored in the space occupied by the p object (here is the first address of the array), and then read and write memory by the stored value + offset.

Assign p this object to the first address of the array, any pointer type 32 bits under 4 bytes 64 bits under 8 bytes, p this object requires 4 bytes of space to store the arr value, because p is a variable, accounting for 4 bytes int* pendant arr; that is, to store the arr value header address in the 4 bytes occupied by p

When accessing through the p [_ I] subscript, you first need to take out the four byte values y stored in p

Then access read or write the pointer to the contents of the array in the way of y + offset

From the above results, we can see that there is only one more layer of pointer variable addressing before the first-level pointer operation one-dimensional array and array direct access, but the meaning has completely changed.

As some books say, arrays degenerate into pointers as arguments.

Lea eax, [arr] is the first address of the array saved to the eax register

When the push eax parameter is put into the stack, the value of the esp register minus 4 is equivalent to what is the esp push eax? Equivalent to creating a 00:00 object occupies 4 bytes of the pointer, passed over, the first address value to access a piece of 4-byte memory after the 4-byte memory after the compiler that this is a pointer, so funtion inside the use of sizeof can see occupy 4 bytes (can be privately tested), so the compiler to the type removed.

Int a = (int) arr; writes the first address of the array to the 4 bytes of memory occupied by a. From the point of view of assembly, does it feel familiar? yes, the following two are no different, or even exactly the same, except for some type restrictions attached by the compiler. We can use an int a; manipulate any basic and abstract type objects.

Int a = (int) arr;int* p = arr

Function passes first-level pointer and second-level pointer

As can be seen in the following figure, the first pass level is to push the contents stored in the address ebp-0x3c of p to the stack.

When passing the secondary pointer, lea gets the address of the value p of ebp-0x3c and passes it in.

So for secondary pointers, it's easy to change what p points to in the called function in the funtion2 function.

(this method is similar to the design of some libraries, such as some function designs of ffmpeg, passing the address of a first-level pointer into the function (pointing to NULL). Some objects can be created in the function, and then the pointer of the tuned function can be changed, which is the same as when it is released, which can reduce the work done by the developer.

You can use a first-level pointer to refer to a bit array. When I was just learning, I used a second-level pointer to reference a two-dimensional array, and found that it was not allowed.

The memory of a process is a linear 32-bit addressable 00000000-0xffffffff 4gb. From the perspective of memory, there is neither the concept of a two-dimensional array nor the concept of a multidimensional array. For any variable, the focus should be on the first address and width.

Int arr [4] [3]; can be int arr [12] in memory layout; it is exactly the same without any difference

Design knowledge of two-dimensional array or multi-dimensional array for convenience, for example, the use of two-dimensional array can be more intuitive representation of the state of a nm map, using one-dimensional words is not so intuitive. Three-dimensional arrays allow us to express the concept of a space more intuitively, in fact, their memory is a linear one-dimensional array.

Int arr [4] [3] We access through arr [_ I] [_ j]

Mov eax, [_ I]; first get the value of _ I and save it in the eax register

Lea ecx,arrtow [eax4]; lea instruction is to get the address in [] arrtow first address + _ i41 (char occupies 1 byte if other types need corresponding size)

Mov edx, [_ j]; the edx register holds the value of _ j

Movsx eax.byte ptr [ecx+edx] means that arrtow+_i41+_j1 takes a byte of the value from this memory number and puts it in the eax register. Char to int has a conversion.

Mov [a], eax puts four bytes in the address of the a variable

You can see that a two-dimensional array is actually an one-dimensional array with the first address + _ im width + J* width.

3D array is the same.

Second-level pointers cannot reference two-dimensional arrays because second-level pointers operate on memory in a completely different way from two-dimensional arrays, and first-level pointers can reference one-dimensional arrays because they operate memory in the same way.

Lea eax, [arrtow] mov dword ptr [pp], eax / / these two lines give the value of char** pp = (char**) arrtow arrtow to the pp variable mov eax,dword ptr [_ I]; the value of / / _ I is stored in the eax register mov ecx.dword ptd [pp] / / the value stored in pp is the Arrtowvalue (first address of the array) mov edx,dword ptr [ecx+eax4] / / the value pointer width of the first address of the array + _ i4 occupies 4 bytes, and reads four bytes of the arrtow+_i 4-bit first address into the edx register (addressed once here) mov eax.dword ptr [_ j] / / read the value of _ j in the eax register movsx ecx,byte ptr [edx+eax] / / take the four bytes of data taken at the arrtow+_i4 address as the first address + _ j again as the first address to take a byte as such as the ecx register mov dword ptr [a], ecx, put it in the a variable

Through the two-level pointer and two-dimensional array access to memory in different ways, you can see why you can not use the second-level pointer to access the two-dimensional array directly, the same third-level pointer addressing 3 times, four-level pointer 4 times, no matter how many dimensional arrays are the same as one-dimensional array, so if I continue to point to the line, I should hang up to access the unknown address.

Once again, if we move the argument to memory, all we do is access to memory, and illegal access to memory will occur if we are not careful, so it is very important to understand memory, while pointers are just a flexible way to access operational memory. As mentioned before, any pointer occupies, so any 4-byte memory unit the size of an int can access memory in any way of pointer, extremely flexible (if there is not enough understanding of memory, it will also lead to extreme insecurity)

These are all the contents of the article "what is the relationship between arrays and pointers and memory in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report