Pointers on C——8 Arrays.11

来源:互联网 发布:淘宝达人怎么认证大v 编辑:程序博客网 时间:2024/05/17 22:28

8.1.8 Initialization

Just as scalar variables can be initialized in their declarations, so too can arrays. The only difference is that a series of values is needed to initialize an array. A series is easily specified: the values are written as a comma‐separated list enclosed in braces, as in this example:

就像标量变量可以在它们的声明中进行初始化一样,数组也可以这样做。唯一的区别是数组的初始化需要一系列的值。这个系列是很容易确认的:这些值位于一对花括号中,每个值之间用逗号分隔。如下面的例子所示:


int vector[5] = ( 10, 20, 30, 40, 50 );


The values given in the initializer list are assigned to the elements of the array one by one, so vector[0] gets the value 10, vector[1] gets the value 20, and so forth.

初始化列表给出的值逐个赋值给数组的各个元素,所以vector[0]获得的值是10 ,vector[l]获得的值是20 ,其他类推。


Static and Automatic Initialization

The way array initialization takes place is analogous to the way scalars are initialized—it depends upon their storage class. Arrays stored in static memory are initialized once, before the program begins to execute. No instructions are executed to put the values in the proper places, they just start out there. This magic is accomplished by having the linker initialize the array elements to their proper values in the file containing the executable program. If the array was not initialized, the initial values will be zero. When this file is loaded into memory for execution, the initialized array values are loaded in exactly the same way as the program instructions. Thus when execution begins, static arrays are already initialized.

数组初始化的方式类似于标量变量的初始化方式——也就是取决于它们的存储类型。存储于静态内存的数组只初始化一次,也就是在程序开始执行之前。程序并不需要执行指令把这些值放到合适的位置,它们一开始就在那里了。这个魔术是由链接器完成的,它用包含可执行程序的文件中合适的值对数组元素进行初始化。如果数组未被初始化,数组元素的初始值将会自动设置为零。当这个文件载入到内存中准备执行时,初始化后的数组值和程序指令一样也被载入到内存中。因此,当程序执行时,静态数组己经初始化完毕。


The situation is not as rosy with automatic variables, however. Because they reside on the runtime stack, automatic variables may use different memory locations each time the block in which they are declared is entered. There isnʹt any way that the compiler can initialize these locations before the program begins, so automatic variables are uninitialized by default. If initial values are given, the variables are initialized with implicit assignment statements each time execution enters the scope in which they were declared. The implicit assignment statement take up space and take time to execute‐just as ordinary assignments would. The problem for arrays is that there may be many values in the initializer list, resulting in just as many assignments.For very large arrays, the initialization could take considerable time.

但是,对于自动变量而言,初始化过程就没有那么浪漫了。因为自动变量位于运行时堆栈中,执行流每次进入它们所在的代码块时,这类变量每次所处的内存位置可能并不相同。在程序开始之前,编译器没有办法对这些位置进行初始化。所以,自动变量在缺省情况下是未初始化的。如果自动变量的声明中给出了初始值,每次当执行流进入自动变量声明所在的作用域时,变量就被一条隐式的赋值语句初始化。这条隐式的赋值语句和普通的赋值语句一样需要时间和空间来执行。数组的问题在于初始化列表中可能有很多值,这就可能产生许多条赋值语句。对于那些非常庞大的数组,它的初始化时间可能非常可观。


Here is the tradeoff, then. When initializing an array that is local to a function(or a block), think carefully about whether it should be reinitialized each time the function (or block) is entered. If the answer is no, then declare the array static so that the initialization can be performed once before the program begins.

因此,这里就需要权衡利弊。当数组的初始化局部于一个函数(或代码块)时,你应该仔细考虑一下,在程序的执行流每次进入该函数(或代码块)时,每次都对数组进行重新初始化是不是值得。如果答案是否定的,你就把数组声明为static,这样数组的初始化只需在程序开始前执行一次。

上一章 Pointers on C——8 Arrays.10

原创粉丝点击