内存:C/C++编程的重要概念

内存:C/C++编程的重要概念

刘建文略译( http://www.semi-translate.com
这是一本好书,或者说是一本目前非常适合我胃口的书。看了一下好象国内还没有中文翻译,故想边自己翻译自己学习,写点笔记并与观客“共享”这本书。正如作者自己所说,这是一本边缘知识的书,写有关操作系统、计算机体系结构、编译器和程序语言之间“胶水”知识,也就是说想看懂这本书的前提是对这四大科目的知识有一定的了解,这是作者开宗明义的读者群。由于版权以及时间关系,我将有选择的扼要翻译。
Memory as a Programming Concept in C and C++
by Frantisek Franek   
Cambridge University Press ? 2004 (272 pages)
ISBN:052181720X

This methodical text aspires to present various information relevant to operating systems, computer architecture, compilers, principles of programming languages, and C and C++ programming, specifically.
 
 
 
Table of Contents  
 Memory as a Programming Concept in C and C++  
 Chapter 1 - Introduction
 Chapter 2 - From Source File to Executable File
 Chapter 3 - Variables and Objects; Pointers and Addresses
 Chapter 4 - Dynamic Allocation and Deallocation of Memory
 Chapter 5 - Functions and Function Calls
 Chapter 6 - One-Dimensional Arrays and Strings
 Chapter 7 - Multi-Dimensional Arrays
 Chapter 8 - Classes and Objects
 Chapter 9 - Linked Data Structures
 Chapter 10 - Memory Leaks and Their Debugging
 Chapter 11 - Programs in Execution: Processes and Threads
 Appendix One - Hanoi Towers Puzzle
 Appendix Two - Tracing Objects in C++
 Appendix Three - Tracing Objects and Memory in C++
 Appendix Four - Thread-Safe and Process-Safe Reporting and Logging Functions

Chapter 1: Introduction

The motivation for this book came from years of observing注意观察 computer science students at universities as well as professional programmers working in software development. I had come to the conclusion that there seemed to be a gap in their understanding of programming. They usually understood the syntax of the programming language they were using and had a reasonable grasp掌握 of such topics as algorithms and data structures. However, a program is not executed in a vacuum真空; it is executed in computer memory. This simple fact exerts发挥影响 a powerful influence on the actual behavior of the program - or, expressed more precisely, a subtle难以捉摸的 yet powerful influence on the semantics of the particular programming language. I had observed that many students and programmers did not fully understand how memory affected the behavior of the C and C++ programs they were designing. This book is an attempt to fill this gap and provide students and programmers alike以同样的方式 with a text that is focused on this topic.

写这本的动机来自多年来对计算机科学的学生和专业的程序员的观察。我发现他们对编程的理解存在不足。他们一般都能理解程序设计语言的语法,并且也相当熟悉算法和数据结构,但是,程序并不是运行在真空中的;它们运行在计算机的内存中。这么一个简单的规则却大大的影响了程序的实际行为,或者更准确的说,它以一种难以捉摸的方式影响了程序语言的语义。我发现很多人并不完全觉察到这一影响。这本书就要补充这方面教材的不足。

In a typical computer science curriculum, it is expected that students take courses in computer architecture, operating systems, compilers, and principles of programming languages - courses that should provide them with a "model" of how memory matters in the behavior of programs. However, not all students end up taking all these courses, and even if they do, they may not take them in the right order. Often the courses are presented in a disjointed way, making it difficult for students to forge a unified view of how memory affects the execution of programs. Additionally, not all programmers are graduates of university or college programs that feature a typical computer science curriculum. Whatever the reasons, there seems to be a significant number of computer science students and professional programmers who lack a full understanding of the intricate错宗复杂 relationship between programs and memory. In this book we will try to pull together the various pieces of knowledge related to the topic from all the fields involved (operating systems, computer architecture, compilers, principles of programming languages, and C and C++ programming) into a coherent连贯紧密 picture. This should free the reader from searching various texts for relevant information. However, in no way should this book be viewed as a programming text, for it assumes that the reader has at least an intermediate level of programming skills in C or C++ and hence simple programming concepts are not explained. Nor should this book be viewed as an advanced C/C++ programming text, for it leaves too many topics - the ones not directly related to memory - uncovered (e.g., virtual methods and dynamic binding in C++). Moreover, it should not be seen as an operating system book, for it does not delve深究 into the general issues of the discipline and only refers to facts that are relevant to C and C++ programmers.

在计算机科学的课程里,学生一般都学习体系结构、操作系统、编译器和程序设计原理,这些课程只是给他介绍了程序行为在内存中的一个“模型”。再者,不是所有的学生都完成这些课程,即使完成也不一定按照合理的学习顺序。这些课程常常安排得脱节,从而不能让学生对内存作用于程序运行有一个清晰一致的了解。没接受大学正式课程教育更不用说了。无论原因如何,现实是有相当一部分的学生和专业程序员对程序与内存之间那错宗复杂的关系缺乏全面性的了解。在这本书里,我们将试图将这些不同领域(包括操作系统、计算机体系结构、编译器和程序语言)的知识捆绑在一起来确立一幅完整的画面。这样子省了读者到处查找相关的资料。不过,不要把本书作为编程教材,我们假设读者是一名中级程序员;也不要把本作为高级C++编程教材,我们只关注与内存相关的知识,像C++的虚方法与动态绑定这样的高级编程技术我们不会讨论的。此外本书也不是关于操作系统的书,因为我们不深究操作系统理论的一般问题,我们只涉及那些与C++程序有关系的内容。

Unfortunately, there seems to be no curriculum at any university or college covering this topic on its own. As a result, students usually end up with three or four disjointed views: programming syntax and (an incomplete) C/C++ semantics; algorithms and data structures, with their emphasis on the mathematical treatment of the subject; operating systems; and possibly compilers. Although my ambition志向 is to fill the gaps among these various views - at least from the perspective of C/C++ programming - I hope that the book proves to be a valuable supplement to any of the topics mentioned.

很不幸,似乎还没有大学有这方面的课程开设。因此学生毕业时对四大课程(操作系统、数据结构和算法{只关注数据对象数学特性}、编译器和程序语言)的了解产生脱节。我是想填补这方面欠缺,希望本书能提供一些有价值的补充信息。

 

My own experience with software development in the real world shows that an overwhelming压倒性的 number of computer program bugs and problems are related to memory in some way. This is not so surprising, since there are in fact few ways to "crash" a program and most involve memory. For instance, a common problem in C/C++ is accessing an array item with an index that is out of range (see Chapter 6). A program with such a simple bug can exhibit totally erratic behavior during different executions, behavior that ranges from perfect to incorrect, to crashing at the execution of an unrelated instruction with an unrelated message from the operating system, to crashing at the execution of the offending instruction with a message from the operating system that signals an invalid memory access.

多年的实际软件开发经验告诉我,大多数的程序错误或多或少都是与内存有关。这并不奇怪,因为当掉一个程序的原因不多,内存是最大的原因。数组下标越界是一个很好的例子。就那么一个内存越界的错误却会有稀奇古怪的现象和错误出现。

 

With the advent of object oriented programming and the design and development of more complex software systems, a peculiar奇特的 problem has started to manifest表明 itself more frequently: so-called memory leaks (see Chapter 10). In simple terms, this is a failure to design adequate足够 house-cleaning facilities for a program, with the result that unneeded earlier allocated memory is not deallocated. Such undeallocated and ultimately unused memory keeps accumulating积聚 to the point of paralyzing瘫痪 the execution of the program or the performance of the whole computer system. It sounds almost mystical神秘的 when a programmer's explanation of why the system performs so badly is "we are dealing with memory leaks", as if it were some kind of deficiency缺乏 of the memory. A more concrete (and accurate) explanation would be "we did not design the system properly, so the unneeded but undeallocated memory accumulates to the point of severely degrading the performance of the system". The troubles that I have witnessed目击 in detecting and rectifying矫正 memory leaks strongly indicate that many students and programmers lack a fundamental appreciation of the role and function of memory in programming and program behavior.

当OO被发明并用于开发大型复杂软件系统后,一种奇特的与内存有关错误出现了--内存泄漏!内存泄漏简单的说就是程序没有设计一个很好的内存回收机制,导致一些已分配的内存在没有用后没有得到回收。这些内存碎片的积聚最终使程序瘫痪,并且影响主机性能。我亲眼目击过这种内存泄漏的结果,并且意识到这是程序员对内存于程序的重要性的缺乏重视和了解的原因。

 

We are not really interested in technical, physical, or engineering characteristics of memory as such (how it is organized, what the machine word is, how the access is organized, how it is implemented on the physical level, etc.); rather, we are interested in memory as a concept and the role it plays in programming and behavior of C/C++ programs. After finishing this book, the reader should - in addition to recognizing superficial differences in syntax and use -
be able to understand (for example) the deeper differences between the "compile-time index range checking" philosophy used in C/C++ and the "run-time index range checking" philosophy used in Pascal (Chapter 6) or between the "recursive procedure calls" philosophy used in C/C++ and the "nonrecursive procedure calls" philosophy used in FORTRAN (Chapter 5).

As another example, the reader of this book should come to appreciate why Java requires garbage collection whereas C/C++ does not (and in general cannot);
why C/C++ cannot be interpreted in a manner similar to Java; and why Java does not (and cannot) have pointers whereas C/C++ does (Chapter 3) -
because all these aspects are related in some way to memory and its use. The reader should understand the issues concerning memory during object construction and destruction (Chapter 8); learn how to compact or serialize linked data structures so they can be recorded to a disk or transmitted across a network (Chapter 9);
and learn how to design programs that allow monitoring of memory allocation/deallocation to detect memory leaks (Chapter 10).
The reader will also be exposed to important concepts not exclusively related to C/C++, concepts that are usually covered in courses on operating systems but included here by virtue of由于 being related to memory: for example, concepts of process and thread and interprocess communication (Chapter 11) facilitated by memory (shared memory segments, pipes, messages). Of course, as always, our interest will be on the memory issues concerning both the processes and the threads.

 

我们不关心内存物理结构,我们只关心内存作一种概念或角色,作用于软件开发和对C/C++程序行为的影响。学习完这本书后,读者应该不仅仅停留在对语法和应用的简单肤浅了解,读者可以:
(Chapter 6)能够理解两种哲学不同:C/C++的“编译时下标越界检查”哲学与Pascal的“运行时下标越界检查”哲学;
(Chapter 5)C/C++使用的“递归过程调用”哲学与FORTRAN的“非递归过程调用”哲学的不同;
(Chapter 3) 为什么Java需要垃圾回收机制而C++不用;为什么Java可解释运行而C++不行;为什么Java没有指针而C++有;这都是因为内存的使用方式的不同。
(Chapter 8)理解对象被构建和解构时与内存有关的问题;
(Chapter 9)学习如何压缩或序列化链表( linked data structures),使用链表数据能够保存到磁盘或在网络上传输;
(Chapter 10)学习如何设计程序监视内存的分配与释放,从而检测内存泄漏;
(Chapter 11)学习内存与操作系统有关的内容和重要概念,像进程、线程和进程交流(共享内存段、管道和消息)。

 

The book is divided into eleven chapters. Chapter 2 deals with the process of compilation, linking, and loading in order to explain how the behavior of programs can be discussed and examined as if they were executing in the source form, how the static and the dynamic parts of memory are assigned to a program, and how the abstract address space of the program is mapped to the physical memory.

Most of the topics in Chapter 2 are drawn from the field of the principles of operating systems. We cover the topics without referring to any particular operating system or any low-level technical details. Otherwise, the text would become cumbersome累赘 and difficult to read and would distract the reader from focusing on memory and its role in C/C++ programming. However, knowledge of the topics covered in Chapter 2 is essential to almost all discussions of the role of memory in the subsequent chapters.

Chapter 2是有关于编译、链接和加载的过程的内容,目的是为了讲解程序行为是如何反映到源代码上的;程序所使用的动态的和静态的内存是如何分配的;程序的逻辑地址又是如何映射到物理地址的。Chapter 2的很多内容都与操作系统原理有关,我们不特指定是那一个操作系统,也不介绍任何底层技术细节。不然本书会变得累赘,并且这样会喧宾夺主。这一节有关内存的内容是理解本书接下来的内容的基础,非常重要。 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值