Left value and right value

zhaozj2021-02-16  46

Left value and right value

2003-5-3

In programming, the left value and the right value are a basic problem. Any variable and function name have left values ​​and right values. Simplely said that the left value is the address represented by the content represented by the variable; the right value is the content of the variable. The image is given the image and the right value of the left value and the right value.

In the above figure, the variable X represents a character 'A' stored in the memory, which is the right value of the variable X, and the left value is as shown. During actual use, the variable X is used directly, and the right value-character 'a' is active. To obtain the left value of the variable X, you need to use the address symbol '&', that is, the left value of X is obtained by & X.

The process of execution of the program is the process of constantly changing the left and right values ​​of the variable.

From the moment of declared variables, the system must set the initial value for the variable - including the left value and right. The setting of the left value - that is, the number of variables in memory is determined according to the number of variables, declaration methods, and the position of the program when declared. Global variables and static variables are stored with the "global variable" area, see the figure below. (Note: This figure is taken from the "C language Daquan (fourth edition)" (Herbert ● Hilter), I suspect that the picture is too simple, painting is not very good. Suspicion is from the example.)

All local variables are stored in the stack area. Although there is a pile allocated during the program execution, the variables pointing to allocated memory is still stored on the stack.

The initial right value of the local variable is uncertain, and the global variable and the initial right value of the static variable are automatically set to zero.

Understanding the left value and right value of the variable, the declaration, initial value, and how the use method is greatly helpful.

one example:

#include float f; void main () {float * x, * y; static float * z; const float g = 1.2; register int R; int * n = new int =;

Printf ("The Address of * x Viz. The Value of Variable X IS:% D / N", X); Printf ("The Address of * Y IS:% D / N", Y PRINTF ("The Address of * Z Viz. The Value of Variable Z IS:% D / N", Z); Printf ("The Address of * N Viz. The Value of Variable N IS:% D / N" , N);

Printf ("THE ADRESS OF X IS:% D / N", & X); Printf ("The Address of Y IS:% D / N", & Y); Printf ("THE Address of Static Z IS:% D / N ", & z); Printf (" THE ADDRESS OF NEW N IS:% D / N ", & N); Printf (" THE VALUE OF F I:% D / N ", F); Printf (" The Address of Global F IS:% D / N ", & f);

Printf ("THE VALUE OF G IS:% F / N", G); Printf ("The Address of Const G IS:% D / N", & g); Printf ("The Value of R IS:% D / N ", R); Printf (" The Address of Register R IS: D / N ", & R);

}

translater:

G (DEV-C 4.9.6.0)

Running platform: Win2000

operation result:

The address of * x viz. The value of variable x is: 2293600the address of * y Viz. The value of variable y is: 2the address of * z viz. The value of variable z is: 0the address of * n viz. the value of variable n is: 4598752the address of x is: 2293592the address of y is: 2293588the address of static z is: 4227080the address of new n is: 2293580the value of f is: 0the address of global f is: 4227072the value of g is : 1.200000The Address of Const G IS: 2293584The Value of R IS: 4598752THE Address of Register R IS: 2293564

Some analysis:

1. The global variable F and the static variable z are stored in the same area, which is located in the high apartment.

2, local variables x, y, r, N stored in the same area.

3, the right value of global variables and static variables is automatically initialized to zero. For pointer z this means it point to NULL.

4. The automatic right value of local variables is uncertain, such as X, Y, and R.

5. Changes to the memory image diagram of the variable, as shown below:

2003-5-14 Modified:

The example is basically unchanged, and only the address display format is changed to 16.

The hardware environment is unchanged (the same computer).

translater:

GCC (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

Operating platform:

Linux 2.4.20-8 (Red Hat 9.0)

operation result:

the address of * x viz the value of variable x is:. 40015360the address of * y viz the value of variable y is:.. 42130a14the address of * z viz the value of variable z is:. 0the address of * n viz the value of variable n is: 80498c8the address of x is: bffff524the address of y is: bffff520the address of static z is: 8049870the address of new n is: bffff514the value of f is: 0the address of global f is: 80498d4the value of g is : 1.200000The Address of Const G IS: Bfff51cthe Value of R IS: -1073744600The Address of Register R IS: bffff518

Thinking:

1. In the Linux platform, the memory image of the variable seen from the results is consistent with Herbert's description.

2, in the Linux platform, each running the result is inconsistent, that is, the address of the same variable will change when executed each time the program is executed. In contrast, in the Windows platform, the procedure is uniform (the author has executed 10 times), and the result is consistent.

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

New Post(0)