Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 140 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
140
Dung lượng
5,48 MB
Nội dung
[...]...Preface Astute readers such as yourself may be wondering whether the title of this book, Safe C++, presumes that the C++ programming language is somehow unsafe Good catch! That is indeed the presumption The C++ language allows programmers to make all kinds of mistakes, such as accessing memory beyond the bounds of an allocated array, or reading memory... foot while programming in C++, and everything will proceed happily along until the program abruptly crashes, or produces an unreasonable result, or does something that in computer literature is referred to as “unpredictable behavior.” So yes, in this sense, the C++ language is inherently unsafe This book discusses some of the most common mistakes made by us, the programmers, in C++ code, and offers recipes... Audience If you have never programmed in C++, this book is not for you It is not intended as a C++ primer This book assumes that you are already familiar with C++ syntax and have no trouble understanding such concepts as the constructor, copy-constructor, assignment operator, destructor, operator overloading, virtual functions, exceptions, etc It is intended for a C++ programmer with a level of proficiency... made my best effort to ensure that the code of SafeC++ library provided in the Appendices is correct, and to the best of my knowledge it does not contain any bugs Still, you use it at your own risk All the C++ code and header files we discuss are available both at the end of this book in the Appendices, and on the website https: //github.com/vladimir-kushnir/SafeCPlusPlus We have here outlined a road map... Bug-Hunting Strategy for C++ This part of the book offers a classification of the kinds of errors that tend to creep into C++ programs I show the value of catching errors during compilation instead of testing, and offer basic principles to keep in mind when pursuing the specific techniques to prevent or catch bugs discussed in later chapters CHAPTER 1 Where Do C++ Bugs Come From? The C++ language is unique... language, you’d need not to do any work of porting the code: you’d just install the new C++ compiler, and it would compile your old C code and everything would work the same way You might even think that you’d completed a transition to C++ While this last thought would be far from the truth—the code written in real C++ looks very different from the C code —this still gives an option of a gradual transition... while C++ inherited the whole of C with its philosophy of efficiency, it inherited all its problems as well So part of the answer to the question of where the bugs come from is “from C.” However, this is not the end of the story In addition to the problems inherited from C, C++ introduced a few of its own For instance, most people count friend functions and multiple inheritance as bad ideas And C++ has... with brackets and another without If you mess up here, you can wreak havoc on the memory heap So to summarize, the bugs in C++ mostly came from C, but C++ added this new method for programmers to shoot themselves in the foot, and we’ll discuss it in Part II 4 | Chapter 1: Where Do C++ Bugs Come From? CHAPTER 2 When to Catch a Bug Why the Compiler Is Your Best Place to Catch Bugs Given the choice of catching... debug the code later In Part II, we’ll consider some of the most common mistakes in C++ code and learn how to deal with them—one at a time 16 | Chapter 3: What to Do When We Encounter an Error at Runtime PART II Bug Hunting: One Bug at a Time This section gives detailed advice, along with directions for using the SafeC++ library I created, for catching particular bugs before your code goes out in production... gradual transition That is, you could start with existing C code that still compiles and runs, and gradually introduce some pieces of new code written in C++, mixing them as much as you want and eventually switching to pure C ++ So the layered design of C++ was an ingenious marketing move However, it also had some implications: while the whole syntax of C was grandfathered into the new language, so was . title of this book, Safe
C++, presumes that the C++ programming language is somehow unsafe. Good catch!
That is indeed the presumption. The C++ language allows. class="bi x0 y0 w0 h1" alt=""
Safe C++
Vladimir Kushnir
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Sebastopol
•
Tokyo
Safe C++
by Vladimir Kushnir
Copyright