说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C2-C_depth/scope_memory.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
2.1. Parts of Program Memory and Scope
下面的 C 程序展示了函数、参数、局部变量和全局变量的例子(为缩短代码清单,省略了函数注释):
The following C program shows examples of functions, parameters, and local and global variables (function comments are omitted to shorten this code listing):
1 | /* An example C program with local and global variables */ |
这个例子展示了作用域各不相同的程序变量。变量的作用域(scope)决定了它的名字在什么时候有意义。换句话说,作用域定义了一组程序代码块:在这些代码块中,变量被绑定到(关联到)某个程序内存位置,并且可以被程序代码使用。
This example shows program variables with different scope. A variable’s scope defines when its name has meaning. In other words, scope defines the set of program code blocks in which a variable is bound to (associated with) a program memory location and can be used by program code.
在任何函数体之外声明变量,就创建了一个全局变量(global variable)。全局变量永远处于作用域内,程序中的任何代码都可以使用它,因为它始终绑定在一个特定的内存位置上。每个全局变量都必须有唯一的名字——在程序的整个运行期间,它的名字唯一地标识程序内存中的一个特定存储位置。
Declaring a variable outside of any function body creates a global variable. Global variables remain permanently in scope and can be used by any code in the program because they’re always bound to one specific memory location. Every global variable must have a unique name — its name uniquely identifies a specific storage location in program memory for the entire duration of the program.
局部变量和参数只在定义它们的函数内部处于作用域内。例如,参数 amt 只在 change 函数内部处于作用域内。这意味着只有 change 函数体内的语句才能访问参数 amt,而且 amt 参数的一个实例只在函数的某一次具体执行期间才被绑定到一个特定的内存存储位置。存放参数值的空间在函数被调用时在栈上分配,在函数返回时从栈上释放。函数的每一次活动(activation)都有自己的一套参数和局部变量绑定。因此,对于递归函数调用,每次调用(或活动)都会得到一个独立的栈帧,其中包含存放其参数和局部变量的空间。
Local variables and parameters are only in scope inside the function in which they are defined. For example, the
amtparameter is in scope only inside thechangefunction. This means that only statements within thechangefunction body can access theamtparameter, and an instance of theamtparameter is bound to a specific memory storage location only within a specific active execution of the function. Space to store a parameter’s value is allocated on the stack when the function gets called, and it is deallocated from the stack when the function returns. Each activation of a function gets its own bindings for its parameters and local variables. Thus, for recursive function calls, each call (or activation) gets a separate stack frame containing space for its parameters and local variables.
由于参数和局部变量只在定义它们的函数内部处于作用域内,不同的函数可以为局部变量和参数使用相同的名字。例如,change 和 max 函数都有一个名为 val 的局部变量。当 max 函数中的代码引用 val 时,它引用的是自己的局部变量 val,而不是 change 函数的局部变量 val(后者在 max 函数内部不在作用域内)。
Because parameters and local variables are only in scope inside the function that defines them, different functions can use the same names for local variables and parameters. For example, both the
changeand themaxfunctions have a local variable namedval. When code in themaxfunction refers tovalit refers to its local variablevaland not to thechangefunction’s local variableval(which is not in scope inside themaxfunction.)
虽然偶尔有必要在 C 程序中使用全局变量,但我们强烈建议你尽可能避免用全局变量编程。只使用局部变量和参数,写出的代码更模块化、更通用,也更容易调试。此外,由于函数的参数和局部变量只在函数处于活动状态时才在程序内存中分配,这样的程序可能更节省空间。
While there may occasionally be times when using global variables in C programs is necessary, we strongly recommend that you avoid programming with global variables whenever possible. Using only local variables and parameters yields code that’s more modular, more general-purpose, and easier to debug. Also, because a function’s parameters and local variables are only allocated in program memory when the function is active, they may result in more space-efficient programs.
启动一个新程序时,操作系统会为这个新程序分配地址空间。程序的地址空间(address space,或称内存空间)代表程序执行所需的一切的存储位置,也就是存放其指令和数据的存储空间。可以把程序的地址空间看作一个由可寻址字节组成的数组;程序地址空间中每个被使用的地址,存放着某条程序指令或某个数据值的全部或一部分(或者程序执行所需的其他一些状态)。
Upon launching a new program, the operating system allocates the new program’s address space. A program’s address space (or memory space) represents storage locations for everything it needs in its execution, namely storage for its instructions and data. A program’s address space can be thought of as an array of addressable bytes; each used address in the program’s address space stores all or part of a program instruction or data value (or some additional state necessary for the program’s execution).
程序的内存空间被划分为几个部分,每个部分用来存放进程地址空间中不同种类的实体。图 1 展示了程序内存空间的各个组成部分。
A program’s memory space is divided into several parts, each of which is used to store a different kind of entity in the process’s address space. Figure 1 illustrates the parts of a program’s memory space.

图 1. 程序地址空间的组成部分
Figure 1. The parts of a program’s address space.
译注 · 图中内容与真实布局
图的顶部是地址较小(接近 0)的一端,依次是操作系统、代码(指令)、数据(全局变量)和堆(动态分配的内存);另一端(最大地址处)是存放局部变量和函数参数的栈。
这是一张教学用的简化图。以 x86_64 Linux 为例,真实的划分是:用户程序使用低端约 128 TB 的虚拟地址(0000000000000000–00007fffffffffff),内核位于高端(以 ffff8... 开头)的地址范围;地址 0 附近的一小段通常故意不映射,好让空指针解引用立刻触发段错误。Linux 内核文档:x86_64 内存映射
程序内存的顶部保留给操作系统使用,其余部分可供正在运行的程序使用。程序的指令存放在内存的代码段(code section)中。例如,上面列出的程序把 main、max 和 change 函数的指令存放在这一内存区域。
The top of a program’s memory is reserved for use by the operating system, but the remaining parts are usable by the running program. The program’s instructions are stored in the code section of the memory. For example, the program listed above stores instructions for the
main,max, andchangefunctions in this region of memory.
局部变量和参数位于内存中栈(stack)所在的部分。由于栈空间的大小会随着函数的调用和返回在程序执行过程中增长和收缩,栈所在的内存部分通常被分配在内存底部附近(即最高的内存地址处),为它的变化留出空间。局部变量和参数的栈存储空间只在函数处于活动状态时才存在(位于该次函数活动在栈上的栈帧中)。
Local variables and parameters reside in the portion of memory for the stack. Because the amount of stack space grows and shrinks over the program’s execution as functions are called and returned from, the stack part of memory is typically allocated near the bottom of memory (at the highest memory addresses) to leave space for it to change. Stack storage space for local variables and parameters exists only when the function is active (within the stack frame for the function’s activation on the stack.)
全局变量存放在数据段(data section)中。与栈不同,数据区不会增长或收缩——全局变量的存储空间在程序的整个运行期间一直存在。
Global variables are stored in the data section. Unlike the stack, the data region does not grow or shrink — storage space for globals persists for the entire run of the program.
最后,内存中的堆(heap)部分是程序地址空间中与动态内存分配相关的部分。堆通常位于离栈内存很远的地方,随着运行中的程序动态分配更多空间,它会向更高的地址增长。
Finally, the heap portion of memory is the part of a program’s address space associated with dynamic memory allocation. The heap is typically located far from stack memory, and grows into higher addresses as more space is dynamically allocated by the running program.