`
buliedian
  • 浏览: 1202044 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

内存: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的很多内容都与操作系统原理有关,我们不特指定是那一个操作系统,也不介绍任何底层技术细节。不然本书会变得累赘,并且这样会喧宾夺主。这一节有关内存的内容是理解本书接下来的内容的基础,非常重要。
分享到:
评论

相关推荐

    高质量C/C++编程指南(PDF)

    高质量C++/C 编程指南,v 1.0 2001 Page 4 of 101 6.1 参数的规则. 6.2 返回值的规则. 6.3 函数内部实现的规则. 6.4 其它建议. 6.5 使用断言. 6.6 引用与指针的比较. 第7 章 内存管理 7.1 内存分配方式 7.2 常见...

    嵌入式C/C++语言开发精华文章集锦(嵌入式C/C++开发者必看,很有价值的文章)

    本压缩包文件集锦了嵌入式C/C++开发编程技巧,高效编程技术探索,C/C++语言相关技术问题的深层探索,并且对嵌入式C/C++程序员应聘常见面试题进行了深入剖析,对嵌入式C/C++开发者编程修炼,提升自身的相关能力具有很...

    高质量C++编程指南.PDF

    高质量C/C++编程指南.PDF 作者:林锐 目录: 前 言 第1章 文件结构 1.1 版权和版本的声明 1.2 头文件的结构 1.3 定义文件的结构 1.4 头文件的作用 1.5 目录结构 第2章 程序的版式 2.1 空行 2.2 代码行 2.3 代码行内...

    高质量C/C++编程指南.pdf

    《高质量C/C++编程指南》,作者:林锐,pdf 格式,大小 327KB。 目录: 前 言 ..................................................................................................................................

    C/C++程序员面试指南.杨国祥(带详细书签).pdf

    本书以流行的面试题讲解为主要内容,介绍了C、C++语言基本概念,包括保留字、字符串、指针和引用、结构体、库函数等各个方面的基础知识,介绍了面向对象编程基本概念,包括如何实现继承、多态和封装等。还介绍了排序...

    高质量C++/C编程指南

    目 录 前 言 6 第1章 文件结构 11 1.1 版权和版本的声明 11 1.2 头文件的结构 12 1.3 定义文件的结构 13 1.4 头文件的作用 13 1.5 目录结构 14 第2章 程序的版式 15 ...附录C :C++/C试题的答案与评分标准 97

    完结5章C/C++音视频实战-gb28181系列-pjsip-sip栈-h264安防流媒体服务器

    C/C++编程语言作为音视频开发的主要工具之一,要深入了解其底层工作原理及相关知识是至关重要的。以下是一些重要的方面: 1. 计算机系统底层工作原理:了解计算机硬件、内存管理、寄存器等基本概念,有助于更好地...

    嵌入式C/C++语言精华文章集锦(免费下载)

    C 语言嵌入式系统编程修炼之三:内存操作..........................................................30 C 语言嵌入式系统编程修炼之四:屏幕操作..........................................................36 C ...

    高质量C/C++编程(高清扫描版)

    高质量C++/C 编程指南,v 1.0 2001 Page 4 of 4 6.1 参数的规则.............................................................................................................. 36 6.2 返回值的规则..............

    高质量C、C++编程指南

    林锐博士在编程质量上的心得体会,文章不长,近一百页,叙述恰当,可作参考。 下附目录 前 言 第1 章 文件结构 1.1 版权和版本的声明 1.2 头文件的结构 1.3 定义文件的结构 ...附录C :C++/C 试题的答案与评分标准

    绝对经典的C++编程资料

    本来应该免费的,因个人需要下载文件需要资源分,特收一定的资源分,谅为上 目 录 前 言 6 第1章 文件结构 11 1.1 版权和版本的声明 11 1.2 头文件的结构 12 ...附录C :C++/C试题的答案与评分标准 97

    高质量C++编程指南(林锐) DOC和PDF合集

    高质量C++编程指南(林锐)DOC版和pdf的合集 目 录 前 言 6 第1章 文件结构 11 1.1 版权和版本的声明 11 1.2 头文件的结构 12 1.3 定义文件的结构 13 1.4 头文件的作用 13 1.5 目录结构 14 第2章 程序的版式 15 2.1...

    高质量C++-C编程指南

    高质量C++-C编程指南 目 录 前 言... 6 第1章 文件结构... 11 1.1 版权和版本的声明... 11 1.2 头文件的结构... 12 1.3 定义文件的结构... 13 1.4 头文件的作用... 13 1.5 目录结构... 14 第2章 程序的版式... 15 ...

    [价值百元]c/c++/c#程序员实用大全 第十章 内存管理

    本书对Oracle的WebDB 的基本概念、结构、组件,利用WebDB 创建应用程序系统和Web 站点,以及管理和维护WebDB 安全性等内容进行了详细介绍。如果喜欢请购买正版图书,请不要把此书用作任何商业用途。

    C++函数间动态内存的传递----论文

    所以如果你想深入C/C++编程,你必须静下心来,好好苦一番。  现在我们将讨论C/C++里我认为哪一本书都没有完全说清楚,也是涉及概念细节最多,语言中最难的技术之一的动态内存的传递.并且在软件开发中很多专业人员...

    C语言深层探索+ARM linux移植

    C语言嵌入式系统编程修炼之三:内存操作 31 C语言嵌入式系统编程修炼之四:屏幕操作 37 C语言嵌入式系统编程修炼之五:键盘操作 45 C语言嵌入式系统编程修炼之六:性能优化 49 C/C++语言void及void指针深层探索 52 C/C++...

    高质量C++编程指南

    高质量C++编程指南 前 言 6 第1章 文件结构 11 1.1 版权和版本的声明 11 1.2 头文件的结构 12 1.3 定义文件的结构 13 1.4 头文件的作用 13 1.5 目录结构 14 第2章 程序的版式 15 2.1 空行 15 2.2 代码行 16 2.3 代码...

Global site tag (gtag.js) - Google Analytics