标签: auto-ptr

用于非对象类型的C++智能指针?

我正在尝试使用智能指针,如auto_ptr,shared_ptr.但是,我不知道如何在这种情况下使用它.

CvMemStorage *storage = cvCreateMemStorage();
... use the pointer ...
cvReleaseMemStorage(&storage);
Run Code Online (Sandbox Code Playgroud)

我不确定,但我认为存储变量只是一个malloc的内存,而不是C++类对象.有没有办法使用智能指针存储变量?

谢谢.

c++ smart-pointers auto-ptr shared-ptr

2
推荐指数
1
解决办法
444
查看次数

自动指针的容器

我知道不应该使用自动指针的容器,这可能会导致问题.这是什么原因?是否还有其他类型的"智能"指针可以安全地在容器中使用?

c++ stl auto-ptr

2
推荐指数
1
解决办法
299
查看次数

将auto_ptr传递给期望对auto_ptr的常量引用的函数有什么危险?

Nicolai Josuttis在他的书"The C++ Standard Library - A Tutorial and Reference"中,在第44页写下了以下段落:

根据auto_ptrs的概念,可以通过使用常量引用将所有权转移到函数中.这是非常危险的,因为人们通常期望在将对象作为常量引用传递时不会修改对象.幸运的是,有一个迟到的设计决定使auto_ptrs不那么危险.通过一些棘手的实现技术,通过常量引用无法转移所有权.实际上,你不能改变任何常量auto_ptr的所有权:...

如果无法通过常量引用来更改所有权,为什么上面的表达"这是非常危险的"和"不那么危险"?

c++ auto-ptr pass-by-reference

2
推荐指数
1
解决办法
1426
查看次数

如何将auto_ptr设置为NULL

有没有办法将auto_ptr设置为NULL或等效?例如,我正在创建一个由节点对象组成的二叉树:

struct Node {
    int weight;
    char litteral;
    auto_ptr<Node> childL;
    auto_ptr<Node> childR;
    void set_node(int w, char l, auto_ptr<Node> L, auto_ptr<Node> R){
        weight = w;
        litteral = l;
        childL = L;
        childR = R;
    }
};
Run Code Online (Sandbox Code Playgroud)

对于不是父节点的节点,我计划这样做:

auto_ptr<Node> n(new Node);
(*n).set_node(i->second, i->first, NULL, NULL);
Run Code Online (Sandbox Code Playgroud)

这会引发错误.有没有办法将它设置为NULL,还是有另一种有意义的行动方案?

c++ binary-tree struct stl auto-ptr

2
推荐指数
1
解决办法
1710
查看次数

函数不会接受auto_ptr的迭代器

我写了一些我试图修复的有缺陷的Huff压缩代码.我做的第一件事就是将指针切换到auto_ptr(有理由我没有使用另一个智能指针).我创建了一个向量,auto_ptr但是当我尝试将auto_ptr传递给函数时,*(vector.begin())它不起作用.

我的功能代码我试图将所有权传递给(它是一个成员函数为set_node):

struct Node {
    int weight;
    char litteral;
    auto_ptr<Node> childL;
    auto_ptr<Node> childR;
    void set_node(int w, char l, auto_ptr<Node>& L(), auto_ptr<Node>& R()){
        weight = w;
        litteral = l;
        childL = L;
        childR = R;
    }
};
Run Code Online (Sandbox Code Playgroud)

这就是我尝试调用它的方式(p是节点):

p.set_node(w, '*', *nodes->begin(), *(nodes->begin()+1));
Run Code Online (Sandbox Code Playgroud)

这是向量声明的方式:

vector<auto_ptr<Node> >* nodes = new vector<auto_ptr<Node> >;
Run Code Online (Sandbox Code Playgroud)

c++ auto-ptr standard-library huffman-code

2
推荐指数
1
解决办法
217
查看次数

函数采用std :: auto_ptr <Base>,可以接受std :: auto_ptr <Derived>

我想创建一个函数,一个auto_ptrBase类,我想用一个称呼它auto_ptrDerived类.但是我没能完成它.

我试过没有参考使用它:

void function(std::auto_ptr<Base> ptr);

std::auto_ptr<Derived> derivedPtr( new ... )
function(derivedPtr); // error:  #348: more than one user-defined conversion from
                      // "std::auto_ptr<Derived>" to "std::auto_ptr<Base>" applies
Run Code Online (Sandbox Code Playgroud)

并参考:

void function(std::auto_ptr<Base>& ptr);

std::auto_ptr<Derived> derivedPtr( new ... )
function(derivedPtr); //error:  #304: no instance of overloaded function "function" 
                      // matches the argument list
Run Code Online (Sandbox Code Playgroud)

编辑:我知道auto_ptr已被弃用但我只能访问C++03并且无法访问boost.所以如果答案将集中在问题本身上,我将不胜感激:)我也理解采用引用的函数和按值的auto_ptr的区别.在我的实际代码中,函数取得了所有权,auto_ptr所以如果我可以从转换DerivedBase工作,两者都很好.

c++ auto-ptr

2
推荐指数
1
解决办法
800
查看次数

C++中的自动指针(auto_ptr)

我试图找出这段代码打印的内容,但由于某些原因我无法输出它,它给了我一个错误:"1 [main] Q1c 5752 open_stackdumpfile:将堆栈跟踪转储到Q1c.exe.stackdump".

double *dp=new double(1.2);
auto_ptr <double> autodp1(dp);
auto_ptr <double> autodp2=autodp1;
cout<<*autodp1<<endl;
Run Code Online (Sandbox Code Playgroud)

我只是想知道它会打印什么,如果它甚至打印.

注意:这个问题在过去的试卷中,仅供修改.

c++ pointers auto-ptr

2
推荐指数
1
解决办法
247
查看次数

如何有效删除存储在多个容器中的C++对象?auto_ptr的?

我有一个应用程序,它在执行期间创建某种类型的对象(比如"Foo"类),跟踪一些统计信息,并将它们插入到两个STL映射中的一个或两个中,例如:

map<Foo*, int> map1;
map<Foo*, int> map2;
Run Code Online (Sandbox Code Playgroud)

我想知道删除Foo对象的最佳方法是什么.目前我的解决方案是迭代map1和map2,并将Foo指针放入一个集合中,然后在这个集合上进行交互并在每个集合上调用delete.

有没有更有效的方法,可能使用auto_ptr?如果是这样,因为auto_ptr <>对象不能存储在STL容器中?

提前致谢.

c++ stl auto-ptr

1
推荐指数
2
解决办法
887
查看次数

使非对象资源符合RAII标准

在我的代码中我使用了HANDLEs windows.h.他们像是一样使用

HANDLE h;
if (!openHandleToSomething(arg1, arg2, &h)) {
    throw std::exception("openHandleToSomething error");
}
/* Use the handle in other functions which can throw as well */
if (!CloseHandle(h)) {
    throw std::exception("closeHandle error");
}
Run Code Online (Sandbox Code Playgroud)

如您所见,您必须将此插入CloseHandle到可能在获取和释放过程中发生的每个异常中.因此,你可能会忘记一个(或者有一个你不知道的奇特的SEH异常)并且瞧,你的内存泄漏了.

最近,我已经阅读了RAII,它应该消除这种情况的麻烦,并应CloseHandle自动调用.我也看到std::auto_ptr<someType>C++ 中有类似的东西可以解决分配的资源问题new.

但是,因为我不使用new,因为HANDLE只是typedef编辑void *,我想知道我应该如何使用std::auto_ptr<someType>.不知何故,它应该可以给它一个自定义删除函数(if (!CloseHandle(h)) { throw std::exception("closeHandle error"); }).创建一个类将是另一种方法,因为析构函数在其实例超出范围时被调用.然而,为每一件简单的事情设一个课程实在是太过分了.

我该如何解决这些意外的内存泄漏?

请注意,我更喜欢纯C++中没有库和大依赖关系的解决方案,除非它们非常小并且无论如何都要在大多数环境中使用.

c++ raii auto-ptr

1
推荐指数
1
解决办法
713
查看次数

为什么它可以用GNU/C++编译,无法在VC++ 2010 RTM中编译?

#include <stdlib.h>
#include <iostream>
#include <memory>
#include "copy_of_auto_ptr.h"
#ifdef _MSC_VER
#pragma message("#include <string>")
#include <string>
// http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html#Diagnostic-Pragmas
#endif

/*
 case 1-4 is the requirement of the auto_ptr.
 which form http://ptgmedia.pearsoncmg.com/images/020163371X/autoptrupdate/auto_ptr_update.html
*/
/*
 case 1.
 (1) Direct-initialization, same type, e.g.
*/
std::auto_ptr<int> source_int() {
    // return std::auto_ptr<int>(new int(3));
    std::auto_ptr<int> tmp(new int(3));
    return tmp;
}

/*
 case 2.
 (2) Copy-initialization, same type, e.g.
*/
void sink_int(std::auto_ptr<int> p) {
    std::cout << "sink_int << " << *p << std::endl;
}

/*
 case 3.
 (3) …
Run Code Online (Sandbox Code Playgroud)

c++ gnu auto-ptr

1
推荐指数
1
解决办法
649
查看次数