`
buliedian
  • 浏览: 1234775 次
  • 性别: 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++编程指南

    《林锐-高质量C/C++编程指南》不仅为C/C++开发者提供了一套全面而细致的编程规范,还深入讲解了许多核心概念和技术细节,对于提升编程水平和编写高质量的C/C++程序具有重要的指导意义。无论是初学者还是有一定经验的...

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

    《高质量C/C++编程指南》是由上海贝尔实验室的著名计算机科学家林锐博士撰写的一部深度解析C/C++编程技巧和最佳实践的文档。这部指南旨在帮助程序员避免常见错误,提升代码质量和可维护性,深入理解语言的核心概念。...

    高质量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/C++编程" C++是一个功能强大且复杂的编程语言,对于提高软件质量非常重要。以下是从文件的标题、描述、标签和...通过本文,读者可以学习到高质量C/C++编程的重要性和必要性,以及如何编写高质量的C/C++代码。

    高质量C/C++ 编程指南(pdf,清晰)

    在C/C++编程中,理解和掌握内存管理是至关重要的。书中可能会深入讲解指针的使用,包括如何安全地分配和释放内存,避免内存泄漏和悬挂指针等问题。此外,还可能探讨作用域规则、引用以及动态类型,这些都是C++特有的...

    高质量c/c++编程学习视频01

    本视频课程将深入讲解C/C++编程中的关键概念、最佳实践和常见陷阱,帮助开发者写出更高效、更稳定、更易于维护的代码。 在C/C++编程中,有几个核心知识点是不容忽视的: 1. **基本语法与数据类型**:C/C++的基础...

    编程高手之路:vb/C/C++/API/批处理 系列教程汇总

    C++是在C语言基础上扩展的,引入了面向对象编程的概念,如类、对象、继承、多态等。C++的强项在于它的性能和灵活性,常用于游戏开发、图形界面和系统编程等领域。学习C++需要理解面向对象编程的核心原则,并掌握STL...

    嵌入式c/c++编程

    以下是关于"嵌入式C/C++编程"的详细知识点: 1. **嵌入式C编程语言入门与深入**: - C语言基础:理解基本数据类型、变量、运算符、控制结构(如if-else、循环)、函数、指针等。 - 内存管理:学习如何手动分配和...

    林锐博士的高质量C/C++编程指南

    2. **错误处理**:C/C++编程中,错误处理是确保程序健壮性的重要环节。书中会讲解如何有效地使用异常处理和错误码,以及如何避免常见的运行时错误。 3. **内存管理**:C++的内存管理是其强大但复杂的一部分,书中会...

    高质量C/C++编程(林锐)&C语言深度剖析

    《高质量C/C++编程》与《C语言深度剖析》这两本书是C/C++程序员的宝贵资源,它们分别从不同角度深入探讨了这两种编程语言的核心概念和技术。以下是对这两个主题的详细解析。 首先,《高质量C/C++编程》由林锐编写,...

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

    《高质量C/C++编程指南》是针对C和C++编程语言的学习者和开发者编写的,旨在指导读者如何编写高质量的代码。本书不仅仅是对C/C++语言语法的介绍,而是更加强调编程质量和代码的健壮性、可读性和可维护性。软件质量是...

    Windows下的c/c++高级编程 源代码

    如何创建和使用DLL,以及如何解决依赖问题,都是Windows C/C++编程中的重要内容。 7. **异常处理**:Windows提供了一套完整的异常处理机制,如try-except-finally块,以及结构化异常处理(SEH)。 8. **调试技术**...

    高质量编程C/C++(第三版)

    《高质量编程C/C++(第三版)》是针对C++和C语言编程的一本权威指南,旨在提升程序员的代码质量和专业素养。这本书不仅涵盖了编程的基本规范,还特别关注了在面试过程中可能会遇到的问题,帮助读者在技术面试中...

    达内 C/C++课件 适合初学者

    《C/C++编程初学者指南——达内科技课件解析》 C/C++是计算机科学领域中的基础且强大的编程语言,对于任何想要深入理解计算机工作原理或者追求高性能应用开发的人来说,学习C/C++是必不可少的步骤。达内科技作为...

    C/C++编程面试题大总结

    在C/C++编程领域,面试通常会涉及到许多关键知识点,这些知识点不仅测试候选人的基础知识,还考察其实际问题解决能力和编程技巧。以下是对"标题"和"描述"中提到的面试题大总结的详细解析: 1. **基础语法**:C/C++...

Global site tag (gtag.js) - Google Analytics