boost smart pointer

1. boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object and cannot be copied or moved.

#include <boost/scoped_ptr.hpp>
#include <iostream>

int main() {
  boost::scoped_ptr<int> p(new int(1));
  std::cout << *p << std::endl;  p指针地址
  p.reset(new int(2));  reset()释放之前的对象,重新设置新的对象
  std::cout << *p.get() << std::endl;  get()获取指针地址
  p.reset();
  std::cout << std::boolalpha << static_cast<bool>(p) << std::endl;  返回p的bool值,此时为false
  return 0;
}

A smart pointer of type boost::scoped_ptr can‘t transfer ownership of an object. Once initialized an address, the dynamically allocated object is released when

the destructor is executed or when the member function reset() is called.

2. boost::scoped_array is used like boost::scoped_ptr. The crucial difference is that the destructor of boost::scoped_array uses the operator delete[] to release the contained object. Because this operator only applies to arrays, a boost::scoped_array must be initialized with the address of a dynamically allocated array.

#include <boost/scoped_array.hpp>

int main() {
  boost::scoped_array<int> p(new int[2]);
  *p.get() = 1;
  p[1] = 2;
  p.reset(new int[3]);
  return 0;
}

3.  The smart pointer boost::shared_ptr is similar to boost::scoped_ptr. The key difference is that boost::shared_ptr is not necessarily the exclusive owner of an object. Onwership can be shared with other smart pointers of type boost::shared_ptr. In such a case, the shared object is not released until the last copy of the shared pointer referencing the object is destroyed. The smart pointer can be copied.

#include <boost/shared_ptr.hpp>
#include <iostream>

int main() {
  boost::shared_ptr<int> p1(new int(1));
  std::cout << *p1 << std::endl;
  boost::shared_ptr<int> p2(p1);
  p1.reset(new int(2));
  std::cout << *p1.get() << std::endl;
  p1.reset();
  std::cout << std::boolalpha << static_cast<bool>(p2) << std::endl;
  return 0;
}

输出为:

1

2

true

When reset() is called on p1, a new int object is anchored in p1. This doesn‘t mean that the existing int object is destroyed. Since it is also anchored in p2, it continues to exist. boost::shared_ptr uses a reference counter internally. Only when boost::shared_ptr detects that the last copy of the smart pointer has been destroyed is the contained object released with delete.

4. boost::shared_array  calls delete[] in the destructor, this smart pointer can be used for arrays.

#include <boost/shared_array.hpp>
#include <iostream>

int main() {
  boost::shared_array<int> p1(new int[1]);
  {
    boost::shared_array<int> p2(p1);
    p2[0] = 1;
  }
  std::cout << p1[0] << std::endl;
  return 0;
}

the smart pointers p1 and p2 share ownership of the dynamically allocated int array. When the array in p2 is accessed with operator[] to store the number 1, the same array is accessed with p1. Thus, the example writes 1 to standard output.

5. boost::weak_ptr only make sense if used in conjunction with boost::shared_ptr.

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <thread>
#include <functional>
#inclue <iostream>

void reset(boost::shared_ptr<int>& sh) {
  sh.reset();
}

void print(boost::weak_ptr<int>& w) {
  boost::shared_ptr<int> sh = w.lock();
  if (sh) {
    std::cout << *sh << std::endl;
  }
}

int main() {
  boost::shared_ptr<int> sh(new int(99));
  boost::weak_ptr<int> w(sh);
  std::thread t1(reset, std:ref(sh));
  std::thread t2(print, std::ref(w));
  t1.join();
  t2.join();
  return 0;
}

boost::weak_ptr must be initialized with a boost::shared_ptr. Its most important member function is lock(). lock() returns a boost::shared_ptr that shares ownership with the shared pointer used to initialize the weak pointer. In case the shared pointer is empty, the returned pointer will be empty as well.

原文地址:https://www.cnblogs.com/sssblog/p/10945129.html

时间: 2024-10-12 01:47:17

boost smart pointer的相关文章

[C++] smart pointer

写在前面的话: 智能指针的设计意图:C++没有垃圾回收机制,所有的动态内存释放全部由程序员负责,如果程序员没有释放内存,就会造成内存泄漏,这是C++ BUG的一大来源.为了管理动态内存,引入了智能指针,它是一种行为类似指针的类,但是能够管理自己负责的内存区域,当对象离开作用域时能够释放内存,防止内存泄漏. 现在常用的C++标准是C++98,其中只定义了一种智能指针auto_ptr. boost中早就引入了其它类型的智能指针:scoped_ptr,scoped_array,shared_ptr,s

Smart pointer 智能指针小总结

Smart pointer line 58之后smart pointer里的计数已经是0,所以会真正释放它引用的对象,调用被引用对象的析构函数.如果继续用指针访问,会出现如下图的内存访问异常.所以说如果选择了用智能指针,就不要再试图用其他方式再去访问对象了. 1 // sharedTest.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include

smart pointer

smart pointer是一种abstract data type,它能够模仿指针的行为,并且额外提供了一系列诸如自动内存管理.边界检查等特性,这些特性是为了在保证效率的基础上减少由于对指针的不正常使用而带来的bug.smart pointer能够自动进行object的销毁:当某个object的最后一个拥有者被destroy的时候(如局部变量离开了作用域),由smart pointer管理的object会被自动销毁.smart pointer是被声明在stack中的 在C++中,smart p

C++ smart pointer智能指针

  在C++中,程序员可以直接操作内存,给编程增加了不少的灵活性.但是灵活性是有代价的,程序员必须负责自己负责释放自己申请的内存,否则就会出现内存泄露.智能指针就是为了解决这个问题而存在的.它和其他指针没有本质的区别,主要的目的就是为了避免悬挂指针.内存泄露的问题.在这里,我使用对象的应用计数做了一个smart pointer,当一个对象还有引用的时候,就不执行释放内存的操作,当引用计数为0时,就执行内存释放操作,并且将指针重置为NULL. 代码如下: #include <iostream>

My understanding about smart pointer

Here are two simple questions. Problem A #include <string> include <iostream> using namespace std; class vehicle { public: vehicle(const string& name); virtual ~vehicle(){} void PrintOwnerInfo(); private: string driver_name_; }; vehicle::v

[CareerCup] 13.8 Smart Pointer 智能指针

13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates, that simulates a pointer while also providing automatic garbage collection. It automatically counts the number of references to a SmartPointer<T*>

【C++】智能指针(Smart Pointer)

1. 传统指针存在的问题 传统指针存在诸多的问题,比如指针所指向的对象的生命周期问题,挂起引用(dangling references),以及内存泄露(memory leaks). 如下是一个传统指针的使用过程 void Foo() { int *iPtr = new int[5]; // manipulate the memory block // ... // ... // ... delete[] iPtr; } 以上代码将正常运行且内存将被合理释放,但是使用指针常会发生一些意想不到的事情

智能指针(smart pointer)(2):unique_ptr

Unique pointer: Manages the storage of a pointer, providing a limited garbage-collection facility, with little to no overhead over built-in pointers (depending on the deleter used). These objects have the ability of taking ownership of a pointer: onc

智能指针(smart pointer)(1):auto_ptr

智能指针解决了资源生存期管理的问题(尤其是动态分配的对象).智能指针有各种不同的风格.多数都有一种共同的关键特性:自动资源管理.这种特性可能以不同的方式出现:如动态分配对象的生存期控制,和获取及释放资源 (文件, 网络连接).这里主要讨论第一种情况,它们保存指向动态分配对象的指针,并在正确的时候删除这些对象. 何时我们需要智能指针? 有三种典型的情况适合使用智能指针: ? 资源所有权的共享 ? 要写异常安全的代码时 ? 避免常见的错误,如资源泄漏 共享所有权,当多个对象需要同时使用第三个对象的情