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

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.

主站蜘蛛池模板: 灵武市| 浦县| 云南省| 方山县| 尼勒克县| 伊宁市| 佛山市| 礼泉县| 遂川县| 九台市| 南康市| 喀喇沁旗| 绵竹市| 随州市| 七台河市| 清远市| 图片| 井冈山市| 资中县| 措美县| 海阳市| 琼海市| 清原| 逊克县| 罗江县| 芦溪县| 北流市| 鹤山市| 寿光市| 永康市| 白城市| 天长市| 芮城县| 班玛县| 托克逊县| 石泉县| 大竹县| 兰考县| 榆树市| 海南省| 平武县|