欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++ RAII特性

程序员文章站 2024-02-01 17:31:34
C++作为一门Native Langueages,在C++98/03时代,资源管理是个大问题。而内存管理又是其中最大的问题。申请的堆内存需要手动分配和释放,为了确保内存正确释放,一般原则是"谁分配谁负责释放",但软件工程的复杂性、程序员的编码水平参差不齐等仍然导致内存泄漏、空悬指针等问题。严重的内存 ......

c++作为一门native langueages,在c++98/03时代,资源管理是个大问题。而内存管理又是其中最大的问题。申请的堆内存需要手动分配和释放,为了确保内存正确释放,一般原则是"谁分配谁负责释放",但软件工程的复杂性、程序员的编码水平参差不齐等仍然导致内存泄漏、空悬指针等问题。严重的内存泄漏可能很快导致服务器内存耗光而运行崩溃。

托管语言为了解决这种问题引入了gc,把内存管理交给机器处理。而c++解决办法一个是手动重启,一个就是今天的主角raii。

raii全称(resource acquisition is initialization),即对象构造时所需资源应在构造函数中初始化,对象析构时释放这些资源。这种范式意味着应该用类来封装和管理资源。现代c++提供的智能指针,正是用于实现raii手法。智能指针是存储指向动态分配对象指针的类,用于生命期控制,能够确保智能指针离开作用域时,自动正确地销毁动态分配地对象,防止内存泄漏。正确地使用智能指针后,理论上程序中应该不会再出现delete,也不用担心内存泄漏问题了。

关于scopeguard技术的讨论见*:

does scopeguard use really lead to better code?

某老外激情答复:

if there is one single piece of c++ code that i could recommend every c++ programmer spend 10 minutes learning, it is scopeguard (now part of the freely available loki library).

i decided to try using a (slightly modified) version of scopeguard for a smallish win32 gui program i was working on. win32 as you may know has many different types of resources that need to be closed in different ways (e.g. kernel handles are usually closed with closehandle(), gdi beginpaint() needs to be paired with endpaint(), etc.) i used scopeguard with all these resources, and also for allocating working buffers with new (e.g. for character set conversions to/from unicode).

what amazed me was how much shorter the program was. basically, it's a win-win: your code gets shorter and more robust at the same time. future code changes can't leak anything. they just can't. how cool is that?

scopeguard 介绍和实现

未完待续...