官术网_书友最值得收藏!

Chapter 3. Memory Management and Exception Safety

C++ has a great deal of compatibility with the C programming language. C++ retains pointers for representing and accessing specific memory addresses and provides manual memory management primitives via the new and delete operators. You can also seamlessly access from C++, the C Standard Library functions and C system calls or platform APIs of most major operating systems. Naturally, C++ code often deals with handles to various OS resources, like heap memory, open files, sockets, threads, and shared memory. Acquiring such resources and failing to release them could have undesirable consequences for your programs, showing up as insidious bugs, including memory leaks and deadlocks.

In this chapter, we look at ways of encapsulating pointers to dynamically-allocated objects using smart pointers to ensure that they are automatically deallocated when they are no longer needed. We then extend these techniques to non-memory resources. In the process, we develop an understanding of what is meant by exception-safe code and use smart pointers to write such code.

These topics are divided into the following sections:

  • Dynamic memory allocation and exception safety
  • Smart pointers
  • Unique ownership semantics
  • Shared ownership semantics

For some sections of this chapter, you will need access to a compiler with C++11 support. This will be called out with additional instructions in individual sections.

主站蜘蛛池模板: 长宁区| 罗田县| 千阳县| 桦川县| 广河县| 怀仁县| 嘉祥县| 三江| 兴山县| 龙南县| 江川县| 图木舒克市| 嘉禾县| 板桥市| 万年县| 舞阳县| 康乐县| 延庆县| 大荔县| 丰镇市| 长葛市| 宜都市| 佛冈县| 大港区| 柳林县| 龙泉市| 建湖县| 抚顺市| 车致| 巴塘县| 灌云县| 临沭县| 封丘县| 大石桥市| 赣榆县| 元氏县| 呈贡县| 永新县| 长顺县| 体育| 广东省|