在c++的实践中,什么是RAII,什么是智能指针,这些是如何在程序中实现的,以及使用RAII和智能指针的好处是什么?
当前回答
Boost has a number of these including the ones in Boost.Interprocess for shared memory. It greatly simplifies memory management, especially in headache-inducing situations like when you have 5 processes sharing the same data structure: when everyone's done with a chunk of memory, you want it to automatically get freed & not have to sit there trying to figure out who should be responsible for calling delete on a chunk of memory, lest you end up with a memory leak, or a pointer which is mistakenly freed twice and may corrupt the whole heap.
其他回答
void foo() { std::string bar; // // more code here // }
无论发生什么,一旦foo()函数的作用域被保留,bar将被正确删除。
在内部std::string实现经常使用引用计数指针。因此,内部字符串只需要在字符串的一个副本更改时才需要复制。因此,引用计数智能指针可以只在必要时复制某些内容。
此外,当不再需要内部字符串的副本时,内部引用计数可以正确地删除内存。
前提和理由在概念上很简单。
RAII是一种设计范式,用于确保变量在构造函数中处理所有需要的初始化,并在析构函数中处理所有需要的清理。这将所有的初始化和清理减少到一个步骤。
c++不需要RAII,但是越来越多的人认为使用RAII方法可以生成更健壮的代码。
RAII在c++中很有用的原因是,在变量进入和离开作用域时,c++本质上管理变量的创建和销毁,无论是通过正常的代码流还是通过异常触发的堆栈展开。这在c++中是免费的。
通过将所有初始化和清理绑定到这些机制,可以确保c++也会为您处理这些工作。
在c++中讨论RAII通常会引出智能指针的讨论,因为指针在清理时特别脆弱。当管理从malloc或new获得的堆分配内存时,通常由程序员负责在指针被销毁之前释放或删除这些内存。智能指针将使用RAII原理来确保在指针变量被销毁时,堆分配的对象被销毁。
智能指针是RAII的变体。RAII的意思是资源获取是初始化。智能指针在使用前获取资源(内存),然后在析构函数中自动丢弃它。会发生两件事:
我们总是在使用内存之前分配内存,甚至当我们不喜欢它的时候——这很难用智能指针做另一种方式。如果这没有发生,你将尝试访问NULL内存,导致崩溃(非常痛苦)。 即使出现错误,我们也会释放内存。没有遗留的记忆。
例如,另一个例子是网络套接字RAII。在这种情况下:
We open network socket before we use it,always, even when we don't feel like -- it's hard to do it another way with RAII. If you try doing this without RAII you might open empty socket for, say MSN connection. Then message like "lets do it tonight" might not get transferred, users will not get laid, and you might risk getting fired. We close network socket even when there's an error. No socket is left hanging as this might prevent the response message "sure ill be on bottom" from hitting sender back.
现在,正如你所看到的,RAII在大多数情况下是一个非常有用的工具,因为它可以帮助人们上床。
智能指针的c++源代码在网络上数以百万计,包括我上面的回复。
Boost has a number of these including the ones in Boost.Interprocess for shared memory. It greatly simplifies memory management, especially in headache-inducing situations like when you have 5 processes sharing the same data structure: when everyone's done with a chunk of memory, you want it to automatically get freed & not have to sit there trying to figure out who should be responsible for calling delete on a chunk of memory, lest you end up with a memory leak, or a pointer which is mistakenly freed twice and may corrupt the whole heap.
对于一个简单但很棒的概念来说,这是个奇怪的名字。更好的名称是范围绑定资源管理(SBRM)。这个想法是,你经常在块的开始分配资源,并需要在块的出口释放它。退出块可以通过正常的流控制、跳出块,甚至可以通过异常发生。为了涵盖所有这些情况,代码变得更加复杂和冗余。
只是一个没有SBRM的例子:
void o_really() {
resource * r = allocate_resource();
try {
// something, which could throw. ...
} catch(...) {
deallocate_resource(r);
throw;
}
if(...) { return; } // oops, forgot to deallocate
deallocate_resource(r);
}
如你所见,我们有很多方法可以被击败。其思想是我们将资源管理封装到一个类中。对象的初始化会获取资源(“资源获取即初始化”)。当我们退出块(块作用域)时,资源再次被释放。
struct resource_holder {
resource_holder() {
r = allocate_resource();
}
~resource_holder() {
deallocate_resource(r);
}
resource * r;
};
void o_really() {
resource_holder r;
// something, which could throw. ...
if(...) { return; }
}
如果你有自己的类,而不仅仅是为了分配/释放资源,那就太好了。分配只是他们完成工作的一个额外问题。但是一旦你只是想分配/释放资源,上面的事情就变得不方便了。您必须为获得的每种资源编写一个包装类。为了缓解这个问题,智能指针允许你自动化这个过程:
shared_ptr<Entry> create_entry(Parameters p) {
shared_ptr<Entry> e(Entry::createEntry(p), &Entry::freeEntry);
return e;
}
通常,智能指针是new / delete的精简包装,当它们所拥有的资源超出作用域时,刚好调用delete。一些智能指针,比如shared_ptr,允许你告诉它们一个所谓的删除器,而不是delete。这允许你,例如,管理窗口句柄,正则表达式资源和其他任意的东西,只要你告诉shared_ptr关于正确的删除器。
有不同的智能指针用于不同的目的:
Unique_ptr是一个智能指针,它独占一个对象。它还没有在boost中,但它可能会出现在下一个c++标准中。它是不可复制的,但支持所有权转移。一些示例代码(下一个c++):
代码:
unique_ptr<plot_src> p(new plot_src); // now, p owns
unique_ptr<plot_src> u(move(p)); // now, u owns, p owns nothing.
unique_ptr<plot_src> v(u); // error, trying to copy u
vector<unique_ptr<plot_src>> pv;
pv.emplace_back(new plot_src);
pv.emplace_back(new plot_src);
与auto_ptr不同,unique_ptr可以放入容器中,因为容器将能够保存不可复制(但可移动)的类型,如流和unique_ptr。
Scoped_ptr是一个boost智能指针,既不可复制也不可移动。当你想要确保指针在超出作用域时被删除时,它是一个完美的东西。
代码:
void do_something() {
scoped_ptr<pipe> sp(new pipe);
// do something here...
} // when going out of scope, sp will delete the pointer automatically.
Shared_ptr表示共享所有权。因此,它既可复制又可移动。多个智能指针实例可以拥有相同的资源。一旦拥有该资源的最后一个智能指针超出作用域,该资源将被释放。我的一个项目的真实例子:
代码:
shared_ptr<plot_src> p(new plot_src(&fx));
plot1->add(p)->setColor("#00FF00");
plot2->add(p)->setColor("#FF0000");
// if p now goes out of scope, the src won't be freed, as both plot1 and
// plot2 both still have references.
如您所见,plot-source (function fx)是共享的,但每个都有一个单独的条目,我们在其上设置颜色。当代码需要引用智能指针所拥有的资源,但不需要拥有该资源时,使用weak_ptr类。你应该创建一个weak_ptr,而不是传递一个原始指针。当它注意到您试图通过weak_ptr访问路径访问资源时,即使不再有shared_ptr拥有该资源,它也会抛出异常。