你可以使用shared_ptr用于C风格数组的RAII吗? [英] Can you use a shared_ptr for RAII of C-style arrays?

查看:97
本文介绍了你可以使用shared_ptr用于C风格数组的RAII吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一段代码,它有许多可能的失败点,导致它提前退出该函数。我要交互的库需要将C风格的数组传递给函数。所以,不是在每个退出点调用数组上的删除,我这样做:

  void SomeFunction(int arrayLength) 
{
shared_ptr< char> raiiArray(new char [arrayLength]);
pArray = raiiArray.get();

if(SomeFunctionThatRequiresCArray(pArray)== FAILED){return; }

//等。
}

我想使用 unique_ptr ,但我目前的编译器不支持它,并且引用计数开销在这种情况下并不重要。



我只是想知道如果有人有任何



UPDATE 我完全忘记了 shared_ptr 调用 delete 而不是 delete [] 。我只是看到没有内存泄漏,决定和它一起去。甚至没有想到使用一个向量。因为我一直在研究新的(对我来说)C ++最近,我想我有一个案例如果你有唯一的工具是一把锤子,一切看起来像一个钉子。综合征。感谢您的反馈。



UPDATE2 我想我会更改问题并提供答案,使其对某人更有价值我犯了同样的错误。虽然有诸如 scoped_array shared_array 向量你可以使用 shared_ptr 来管理数组的范围(但是在这之后我不知道为什么我想要):

  template< typename T> 
class ArrayDeleter
{
public:
void operator()(T * d)const
{
delete [] d;
}
};

void SomeFunction(int arrayLength)
{
shared_ptr< char> raiiArray(new char [arrayLength],ArrayDeleter< char>());
pArray = raiiArray.get();

if(SomeFunctionThatRequiresCArray(pArray)== FAILED){return; }

//等。
}


解决方案

$ c> shared_ptr 或 scoped_ptr 来保存指向动态分配的数组的指针。 shared_ptr和scoped_ptr使用 delete ptr; 在指针不再引用/超出范围时清除,这会调用动态分配的数组上未定义的行为。而是使用shared_array或scoped_array,它们在销毁时正确使用 delete [] ptr;



,如果你不会传递智能指针,使用 scoped_array ,因为它具有比 shared_array 更少的开销。



或者,使用 std :: vector 作为数组存储(向量已保证连续的内存分配) p>

I'm working on a section of code that has many possible failure points which cause it to exit the function early. The libraries I'm interacting with require that C-style arrays be passed to the functions. So, instead of calling delete on the arrays at every exit point, I'm doing this:

void SomeFunction(int arrayLength)
{
   shared_ptr<char> raiiArray(new char[arrayLength]);
   pArray = raiiArray.get();

   if(SomeFunctionThatRequiresCArray(pArray) == FAILED) { return; }

   //etc.
}

I wanted to use unique_ptr, but my current compiler doesn't support it and the reference count overhead doesn't really matter in this case.

I'm just wondering if anyone has any thoughts on this practice when interfacing with legacy code.

UPDATE I completely forgot about the shared_ptr calling delete instead of delete []. I just saw no memory leaks and decided to go with it. Didn't even think to use a vector. Since I've been delving into new (for me) C++ lately I'm thinking I've got a case of the "If the only tool you have is a hammer, everything looks like a nail." syndrome. Thanks for the feedback.

UPDATE2 I figured I'd change the question and provide an answer to make it a little more valuable to someone making the same mistake I did. Although there are alternatives like scoped_array, shared_array and vector, you can use a shared_ptr to manage scope of an array (but after this I have no idea why I would want to):

template <typename T>
    class ArrayDeleter
    {
    public:
        void operator () (T* d) const
        {
            delete [] d;
        }
    };

void SomeFunction(int arrayLength)
    {
       shared_ptr<char> raiiArray(new char[arrayLength], ArrayDeleter<char>());
       pArray = raiiArray.get();

       if(SomeFunctionThatRequiresCArray(pArray) == FAILED) { return; }

       //etc.
    }

解决方案

Do not use shared_ptr or scoped_ptr to hold pointers to dynamically allocated arrays. shared_ptr and scoped_ptr use delete ptr; to clean-up when the pointer is no longer referenced/goes out of scope, which invoked undefined behaviour on a dynamically allocated array. Instead, use shared_array or scoped_array, which correctly use delete[] ptr; when destructing.

To answer your question, if you are not going to pass the smart pointer around, use scoped_array, as it has less overhead than shared_array.

Alternatively, use std::vector as the array storage (vectors have guaranteed contiguous memory allocation).

这篇关于你可以使用shared_ptr用于C风格数组的RAII吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆