Pointers on C——6 Pointers.1

本文深入探讨了指针的概念及其在计算机内存中的运作原理。通过形象的比喻,文章解释了内存如何被组织成一系列的字节,每个字节都有其唯一的地址,并能够存储特定的数据值。此外,还介绍了如何通过组合多个字节来存储更大的数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Itʹs time we talk in detail about pointers, because we will be using them constantly throughout the rest of the book. You may already be familiar with some or all of the background information discussed in this chapter. If you arenʹt, study it carefully because your understanding of pointers will rest on this foundation.

是详细讨论指针的时候了,因为在本书的剩余部分,我们将会频繁地使用指针。你可能已经熟悉了本章所讨论的部分或全部背景信息。但是,如果你对此尚不熟悉,请认真学习,因为你对指针的理解将建立在这个基础之上。

6.1 Memory and Addresses

As mentioned earlier, we can view the computerʹs memory as a row of houses on a long street. Each house is capable of holding data and is identified by a house number.

我在前面提到过,我们可以把计算机的内存看作是一条长街上的一排房屋。每座房子都可以容纳数据,并通过一个房号来标识。

This analogy is useful but limited. Computer memories are composed of millions of bits, each capable of holding either the value 0 or the value 1. This limited range of values is not terribly useful, so bits are usually grouped together and treated as a unit in order to store a wider range of values. Here is a picture of a few memory locations on a real machine.

这个比喻颇为有用,但也存在局限性。计算机的内存由数以亿万计的位(bit)组成,每个位可以容纳值0或l。由于一个位所能表示的值的范围太有限,所以单独的位用处不大,通常许多位合成一组作为一个单位,这样就可以存储范围较大的值。这里有一幅图,展示了现实机器中的一些内存位置。


Each of these locations is called a byte, and each contains as many bits as necessary to store a single character. On many modern machines, each byte contains eight bits,which can store unsigned integers from 0 to 255 or signed integers from ‐128 to 127.The previous diagram does not show the contents of the locations, though every location in memory always contains some value. Each byte is identified by an address,which is represented in the diagram by the numbers above each box.

这些位置的每一个都被称为字节( byte),每个字节都包含了存储一个字符所需要的位数。在许多现代的机器上,每个字节包含8个位,可以存储无符号值O至255,或有符号值-128至127。上面这张图并没有显示这些位置的内容,但内存中的每个位置总是包含一些值。每个字节通过地址来标识,如上图方框上面的数字所示。

To store even larger values, we take two or more bytes and treat them as if they were a single, larger unit of memory. For example, many machines store integers in words, each composed of two or four bytes. Here are the same memory locations, this time viewed as four‐byte words.

为了存储更大的值,我们把两个或更多个字节合在一起作为一个更大的内存单位·例如,许多机器以字为单位存储整数,每个字一般由2个或4个字节组成。下面这张图所表示的内存位置与上面这张图相同,但这次它以4个字节的字来表示。


Because they contain more bits, each word is capable of holding unsigned integers from 0 to 4,294,967,295 (232 – 1) or signed integers from –2,147,483,648 (–231) to 2,147,483,647 (231 – 1).

由于它们包含了更多的位,每个字可以容纳的无符号整数的范围是从0至4294967295(232 – 1),可以容纳的有符号整数的范围是从-2147483648(–231)至2147483647(231 – 1)。

Note that even though a word contains four bytes, it has only one address. It is machine‐dependent whether the address of a word is the address of the leftmost byte in the word or the rightmost. Boundary alignment is another hardware issue. On machines with this requirement, integers can only begin with certain bytes, usually those whose addresses are multiples of two or four. But these issues are the hardware designerʹs problem, and they rarely affect C programmers. We are interested in only two issues:

注意,尽管一个字包含了4个字节,它仍然只有一个地址。至于它的地址是它最左边那个字节的位置还是最右边那个字节的位置,不同的机器有不同的规定。另一个需要注意的硬件事项是边界对齐(boundary alignment)。在要求边界对齐的机器上,整型值存储的起始位置只能是某些特定的字节,通常是2或4的倍数。但这些问题是硬件设计者的事情,它们很少影响C程序员。我们只对两件事情感兴趣

1. Each location in memory is identified by a unique address.

内存中的每个位置由一个独一无二的地址标识。

2. Each location in memory contains a value.

内存中的每个位置都包含一个值。


Origins of the Book This book stems from an introductory course that we developed at Carnegie Mellon University in the fall of 1998, called 15-213: Introduction to Computer Systems (ICS) [14]. The ICS course has been taught every semester since then. Over 400 students take the course each semester. The students range from sophomores to graduate students in a wide variety of majors. It is a required core course for all undergraduates in the CS and ECE departments at Carnegie Mellon, and it has become a prerequisite for most upper-level systems courses in CS and ECE. The idea with ICS was to introduce students to computers in a different way. Few of our students would have the opportunity to build a computer system. On the other hand, most students, including all computer scientists and computer engineers, would be required to use and program computers on a daily basis. So we decided to teach about systems from the point of view of the programmer, using the following filter: we would cover a topic only if it affected the performance, correctness, or utility of user-level C programs. For example, topics such as hardware adder and bus designs were out. Topics such as machine language were in; but instead of focusing on how to write assembly language by hand, we would look at how a C compiler translates C constructs into machine code, including pointers, loops, procedure calls, and switch statements. Further, we would take a broader and more holistic view of the system as both hardware and systems software, covering such topics as linking, loading,翻译上述英文为中文
08-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值