6.1.1 Address Versus Contents
Here is another example, this time showing the contents of five words in memory.
这里有另外一个例子,这次它显示了内存中5个字的内容
Five integer values are shown, each in its own word. If you remember the address where you stored some value, you can use that address later to retrieve the value.It is cumbersome to remember all of those addresses, though, so one of the things that high‐level languages provide is the ability to refer to memory locations by name rather than by address. Here is the same picture, this time with names substituted for addresses.
这里显示了5个整数,每个都位于自己的字中。如果你记住了一个值的存储地址,你以后可以根据这个地址取得这个值。但是,要记住所有这些地址实在是太笨拙了,所以高级语言所提供的特性之一就是通过名字而不是地址来访问内存的位置。下面这张图与上圈相同,但这次使用名字来代替地址。
Of course, these names are what we call variables. It is important to remember that this association of names with locations is not provided by the hardware, it is something that the compiler does for us. All variables give us is a more convenient way of remembering the addresses—the hardware still accesses memory locations using addresses.
当然,这些名字就足我们所称的变量。有点非常重要,你必须记住,名字与内存位置之间的关联并不是硬件所提供的,它是由编译器为我们实现的。所有这些变量给了我们一种更方便的方法记住地址——硬件仍然通过地址访问内存位置。
6.2 Values and Their Types
Now letʹs look at the values stored in these locations. The first two locations appear to contain integer values. The third is a very large integer, and the fourth and fifth are also integers. Or so it seems. Here are the declarations for these variables:
现在让我们来看一下存储于这些位置的值。头两个位置所存储的是整数。第三个位置所存储的是一个非常大的整数,第4,5个位置所存储的也是整数。下面是这些变量的声明
int a = 112, b = -1;
float c = 3.14;
int *d = &a;
float *e = &c;
The declarations confirm that the variables a and b do indeed contain integer values, but claim that c ought to contain a floating‐point value. But c was shown in the illustration as an integer. Well, which is it, an integer or a floating‐point number?
在这些声明中,变量a和b确实用于存储整型值。但是,它声明c所存储的是浮点值。可是,在上图中c的值却是一个整数。那么到底它应该是哪个呢?整数还是浮点数?
The answer is that the variable contains a sequence of bits, 0ʹs and 1ʹs. They could be interpreted either as an integer or as a floating‐point number, depending on the manner in which they are used. If integer arithmetic instructions are used, the value is interpreted as an integer. If floating‐point instructions are used, it is a floating‐point number.
答案是该变量包含了一序列内容为0或者1的位。他们可以被解释为整数,也可以被解释为浮点数,这取决于他们被使用的方式。如果使用的是整型算术指令,这个值就被解释为整数,如果使用的是浮点型指令,他就是个浮点数。
This fact leads to an important conclusion: the type of a value cannot be determined simply by examining its bits. To determine its type (and therefore its value) you must look at the way the value is used in the program. Consider this 32‐bit value, shown in binary:
这个事实引出了一个重要的结论:不能简单地通过检查一个值的位来判断它的类型。为了判断值的类型(以及它的值),你必须观察程序中这个值的使用方式。考虑下面这个以二进制形式表示的32 位值:
01100111011011000110111101100010
Here are a few of the many ways that these bits might be interpreted. These values were all obtained from a Motorola 68000‐based processor. Another system with different data formats and instructions would interpret the same bits differently.
下面是这些位可能被解释的许多结果中的几种。这些值都是从一个基于Motorola 68000 的处理器上得到的。如果换个系统,使用不同的数据格式和指令,对这些位的解释将又有所不同。
Here is a single value that could be interpreted as five different types. Clearly,the type of a value is not something inherent in the value itself but depends on how it is used. Thus to get correct answers, it is important to use the values correctly.
这里,一个单-的值可以被解释为5 种不同的类型。显然,值的类型并非值本身所固有的一种特性,而是取决于它的使用方式。因此,为了得到正确的答案,对值进行正确的使用是非常重要的。
Of course, the compiler helps us avoid these errors. Declaring c to be a floa causes the compiler to generate floating‐point instructions when accessing it and to complain when we attempt to access it in a way that is not appropriate for a float. It is now obvious that the diagram showing the values was misleading, because it showed the integer representation of c. The floating‐point value is really 3.14 .
当然,编译器会帮助我们避免这些错误。如果我们把c 声明为float 型变量,那么当程序访问它时,编译器就会产生浮点型指令。如果我们以某种对float 类型而言不适当的方式访问该变量时,编译器就会发出错误或警告信息。现在看来非常明显,图中所标明的值是具有误导性质的,因为它显示了c 的整型表示方式。事实上真正的浮点值是3.14 。