Array allocation and access

Array: aggregate data into scalar data types larger

C language array of simple, easy to translate into machine code

In machine code, these pointers pointing to the array element address calculation will be translated into

Optimizing compilers are very good address to simplify the calculation used array index , which makes it difficult to understand the correspondence between the C language code and machine code

 

A basic principle

One-dimensional array declaration: TA [N] (T is a data type, N is a constant)

Two effects: the continuous area in memory allocated L * N bytes; import identifiers A, with A as a pointer to the beginning of the array, the value of X A

I is stored in the array element X A + i * L where

Memory reference instructions may be used to simplify the array access

movl (% rdx,% rcx, 4),% eax perform address calculation xA + 4i, and store the result in% eax register

Stretching factor of 1, 2, 4, 8 covers all sizes of data types

Knowledge Point Review :

Memory reference is an operand

Operand There are 3 kinds:

  1. Immediate (notation: '$' followed by an integer representation of a method using a standard C, e.g. $ -577, $ 0x1F),
  2. Register (the register name notation ra, the operation is actually the value of register R [ra], the register set as an array R, the identifier as an index register),
  3. Memory references (according to the calculated address to access a memory location Mb [Addr] Addr, a reference memory address Addr represents a starting value of b bytes in memory)
    1. Calculated address is called addressing modes , specifically in the form of: Imm (rb, ri, s ) Imm represents an immediate, called RB base register , ri referred to as the index register , s is called scale factor (s belong {1, 2, 4, 8} )
    2. Calculated: Imm + R [rb] + R [ri] * s

 

 

II. Pointer arithmetic

C language pointer arithmetic allows, the calculated value is a reference pointer according to the data size of the telescopic type , for example, the expression p + i value xp + L * i

Expression 2, 3, 6 returns an array of values ​​of type int type array elements, thus relates to 4-byte operations, for example, instructions and movl% eax register

Expression 1, 4, 5 returns a pointer of type int *, so 8 bytes involves operations such as instructions and the register% rax leaq

Note ⚠️: Expression 7 may calculate the difference between the two types of data pointer to a data structure of the same, the result is long, the address value equal to the difference of two divided by the size of the data type

 

III. Nested arrays

Declare a two-dimensional array A

int A [5] [3]; // declared directly
Equivalent to
typedef int row3_t [3]; // declare nested
row3_t A [5]; // can be used A [5] to replace immediately row3_t row3_t [3] in int

The focus of this section is only one: 

Declare arrays: TD [R] [C];

It elements D [i] [j] is the memory address & D [i] [j] = the xD + L (C + I * J) , L is the size of the data type of T

Multi-dimensional array with row-index will be mapped to a one-dimensional array

EXAMINATION METHOD: Reverse Engineering, R and C values ​​determined according to the code compilation

 

IV. Fixed-length arrays

Declare fixed-length array

#define N 16
typedef int fix_matrix [N] [N]; // declare the fix_matrix integer array of 16 * 16

This part is mainly about the optimizing C compiler for fixed-length multi-dimensional arrays

Calculated, for example two matrix product of a certain element, the compiler will optimize away the loop variable for loop used i, is converted to calculate the pointer, left matrix Aptr ++, right matrix Bptr + = N, and for loop becomes do-while loop, until Bptr point to stop outside the matrix

For example, the diagonal element assignment a matrix is ​​val, the compiler optimizes the for loop out of the loop variable used i, is converted to calculate a pointer, each ptr + = N + 1, and the for loop becomes do- while loop, until the outer matrix ptr points to stop

 

V. variable length arrays

Historically, C language supports only multi-dimensional array size can be determined at compile time (with some exceptions for first dimension). At that time, programmers need to have variable length arrays when using malloc, calloc allocate an array of functions such as storage space, and requires the programmer to speak multidimensional arrays mapped to a one-dimensional array, a program written in C language.

Introduction of functional ISO C99: dimensions of the array can be an expression, when the array is calculated to be dispensed

It differs in that fixed-length array: must imulq of n i telescopic fold multiplication instruction , not by a series of shifts and adds

Using the access patterns of regularity to optimize the calculation of the index

Back to the earlier example, one calculates the matrix product of two elements, steps identified access array elements, the generated code multiplication is used to avoid direct memory address calculation formula (multiplication calculations because more time-consuming, less to one of the multiplication computing will significantly improve program performance)

 

Guess you like

Origin www.cnblogs.com/louisekou666/p/11665820.html