- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 boost 库的新手,正在尝试 boost::scoped_ptr
,它声明无法复制或移动此智能指针。但是我在玩一些代码时发现了一个问题。我能够创建 scoped_ptr
的新实例并使用现有的有效 scoped_ptr
对其进行初始化。因此,如果 scoped_ptr
的范围之一结束并释放内存,其他 scoped_ptr 仍然认为其有效指针并尝试访问。它在运行时给了我错误。
我正在开发 boost 库版本 1.66,使用 cygwin g++ 编译器并在编译时使用 std=c++03 选项。
#include<boost/scoped_ptr.hpp>
#include<iostream>
using namespace std;
int main(){
boost::scoped_ptr<int> pi(new int(9));
cout << *pi << endl;
cout << uintptr_t(pi.get()) << endl;
boost::scoped_ptr<int> ppi(pi.get()); // initialized with same memory pointed by pi.
cout << *ppi << endl; // so ownership of same memory is with pi.
cout << uintptr_t(ppi.get()) << endl; // as well as with ppi.
pi.reset(new int(12)); //its previous memory location pointing to 9 is deallocated.
cout << *ppi << endl; // throws garbage value..
cout << uintptr_t(ppi.get()) << endl; // shows same memory location it had previous memory shown by pi.
cout << *pi << endl;
cout << uintptr_t(pi.get()) << endl;
return 0;
}
所以下面是编译好的代码运行的快照......
-> g++ -std=c++03 -Wall scoped_ptr.cpp
-> ./a.exe
9
25769804960
9
25769804960
-2144292696
25769804960
12
25769879920
Aborted (core dumped)
执行结束时显示核心转储,它在上面的运行中错误地显示了 -2144292696
。
我还检查了 boost::scoped_ptr
是否能够将其分配给指针int * p = pi.get()
语句编译正常(这应该工作吗?)
上面的操作用其他scoped_ptr
初始化scoped_pt
r是否有效?
最佳答案
Is above operation initializing
scoped_ptr
with otherscoped_ptr
valid?
没有。 Boost documentation阅读:
explicit scoped_ptr(T * p = 0); // never throws
Constructs a
scoped_ptr
, storing a copy ofp
, which must have been allocated via a C++new
expression or be0
.
当你用另一个智能指针持有的原始指针初始化一个智能指针时,你不会转移所有权,你只是复制原始指针。因此,该指针将被 delete
多次,这是未定义的行为。
您的代码基本上是这样的:
int* p = new int;
delete p; // when you call pi.reset()
delete p; // when ppi goes out of scope
.get()
返回存储的原始指针。它不检查其有效性。
要转移所有权,您应该移动分配智能指针本身。 std::unique_ptr
和 C++11 的简单示例:
auto p = std::unique_ptr<int>(new int);
// auto p1 = std::unique_ptr<int>(p.get()); // same error
auto p2 = std::move(p); // fine, transfers ownership from p to p2;
// p will not delete the raw pointer it previously held
boost::scoped_ptr
不支持所有权转移。
关于c++ - boost::scoped_ptr 使用其他 scoped_ptr 进行初始化会导致问题。这段代码是否正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58714610/
我是 boost 库的新手,正在尝试 boost::scoped_ptr,它声明无法复制或移动此智能指针。但是我在玩一些代码时发现了一个问题。我能够创建 scoped_ptr 的新实例并使用现有的有效
我有一个具有以下结构的单例: // Hpp class Root : public boost::noncopyable { public: ~Root();
既然 scoped_ptr 保证给定线程中的所有对象都以类似堆栈的方式分配,那么为 scoped_ptr > , 事实上 std::vector*存储在 scoped_ptr 中与std::vecto
这个问题在这里已经有了答案: 关闭 9 年前。 Possible Duplicate: What is a smart pointer and when should I use one? 我正在阅
我有一个结构 typedef struct myStruct_st { int a; }myStruct; 可以使用创建 myStruct * myStruct_new() { printf(
假设我实现了一个scoped_ptr: template class scoped_ptr { public: scoped_ptr() = delete; explicit scoped
我的类的一个成员是 boost::scoped_ptr,它在创建对象时设置为 (T*)0。该类有一个 init() 方法,它实际上用一个新对象初始化智能指针。 但是,如果在智能指针具有有效引用之前抛出
谁能解释为什么这个例子中引用类型的转换失败了?我想使用 boost::scoped_ptr 作为缺少 std::unique_ptr 的解决方法。 struct A{}; struct B {
我很惊讶编译器拒绝编译这种代码: class A { virtual ~A() {} }; class B : public A { virtual ~B() {} void foo() {} };
我在 pagePtr.h 中有这样的东西 typedef int (*FunPtrType)(char* sz, unsigned int max_bytes, char* arg1, char* a
我正在尝试理解 boost::scoped_ptr 的语法.让我们定义并写入标准输出 a scoped_ptr指针及其指向的值: #include #include int main() {
通常我会遵循 Google 风格指南,我觉得它与我看待事物的方式非常吻合。我也几乎完全使用 boost::scoped_ptr 以便只有一个管理器拥有特定对象的所有权。然后我传递裸指针,我的想法是我的
检查以下代码: 这个有效: T *p = (std::find( this->first(), this->last(), *pPos )); if( p != last() ) { this
我正在使用可变参数模板,我想找到解压参数的好方法 template class MetaKernel : public MyKernel { public: MetaKernel (
我使用 boost::scoped_ptr 实现了一个简单的单例: template class Singleton : public boost::noncopyable { public:
所以我尝试围绕 boost.extension 函数创建一些包装器来创建类。所以我创建了一个函数: template boost::scoped_ptr get_class (shared_lib
我在像这样的小函数中使用 scoped_ptr。这样我就不必调用 delete 了。这是这种用法的矫枉过正吗?我的团队成员更喜欢原始指针和删除。如果这恰好用在非常关键的路径中,那么使用 scoped_
我刚刚开始使用 C++ boost 库。我在很多地方读到,当使用 scoped_ptr 时,即使出现异常,对象也总是被销毁。 They behave much like built-in C++ po
我正在研究智能指针,尤其是 scoped_ptr。我阅读了运算符 * 和 ->。我试着运行这段代码: int main(){ boost::scoped_ptrnumber(new int);
boost::scoped_ptr documentation包含一个称为 Handle/Body Idiom 的技术示例。它是用以下文字描述的: The scoped_ptr_example_tes
我是一名优秀的程序员,十分优秀!