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

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.

主站蜘蛛池模板: 达孜县| 桑日县| 马关县| 吴川市| 马关县| 北碚区| 宜兰市| 台湾省| 奈曼旗| 乌鲁木齐县| 德格县| 上蔡县| 曲靖市| 三明市| 乌拉特中旗| 唐山市| 贡嘎县| 阿巴嘎旗| 长治县| 基隆市| 穆棱市| 桐梓县| 常州市| 绥滨县| 花莲县| 毕节市| 界首市| 平阳县| 集安市| 甘肃省| 青浦区| 富平县| 托克托县| 孟州市| 偃师市| 平阴县| 府谷县| 广西| 富裕县| 霍山县| 博兴县|