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

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.

主站蜘蛛池模板: 永善县| 霍林郭勒市| 鲁山县| 伊金霍洛旗| 潜山县| 长岛县| 新宁县| 荣成市| 阳新县| 青浦区| 津南区| 康马县| 五莲县| 行唐县| 凤翔县| 长顺县| 贵溪市| 深泽县| 会昌县| 民乐县| 基隆市| 浮梁县| 乌兰县| 肇州县| 兰州市| 图片| 西平县| 清水河县| 无棣县| 简阳市| 科尔| 齐齐哈尔市| 赫章县| 得荣县| 北京市| 烟台市| 枣阳市| 广饶县| 通城县| 古交市| 启东市|