c++ - 具有 POD 和未初始化值数组的 auto_ptr

标签 c++ arrays memory-management c++03

这与 auto_ptr for arrays 非常相似.然而,我的问题是我不想要一个初始化数组,这是一个 vector 会提供的(const T& value = T()):

explicit vector(size_type count,
    const T& value = T(),
    const Allocator& alloc = Allocator());

我不想初始化数组,因为它是一个大数组,值会立即被丢弃。

我目前正在用以下方法破解它,但感觉它有问题:

//! deletes an array on the heap.
template <class T>
class AutoCleanup
{
public:
    AutoCleanup(T*& ptr) : m_ptr(ptr) { }
    ~AutoCleanup() { if (m_ptr) { delete[] m_ptr; m_ptr = NULL; }}

private:
    T*& m_ptr;
};

和:

// AutoCleanup due to Enterprise Analysis finding on the stack based array.
byte* plaintext = new byte[20480];
AutoCleanup<byte> cleanup(plaintext);

// Do something that could throw...

对于未初始化和正确删除的 POD 类型的数组,C++ 提供了什么?


项目是C++03,没有外部依赖,比如Boost。

最佳答案

从您的问题来看,要解释您实际需要什么并不容易。但我猜你想要一个堆栈保护(即生命周期绑定(bind)到堆栈)、堆分配、未初始化、动态或静态大小并且与 C++11 之前的编译器兼容的数组。

auto_ptr 无法处理数组(因为它不在其析构函数中调用 delete[],而是调用 delete)。

相反,我会将 boost::scoped_arraynew[] 一起使用(据我所知,它不会初始化 POD 类型)。

boost::scoped_array<MyPodType> a(new MyPodType[20480]);

如果你不想使用 boost,你可以很容易地重新实现 scoped_array,方法是将此类从 boost 库中包含的代码粘贴在一起:

#include <cassert>
#include <cstddef>

// From <boost/checked_delete.hpp>:
template<class T>
inline void checked_array_delete(T * x)                                                                          
{
    typedef char type_must_be_complete[sizeof(T) ? 1 : -1];
    (void) sizeof(type_must_be_complete);
    delete [] x;
}

// From <boost/smartptr/scoped_array.hpp>:
template<class T>
class scoped_array
{
private:
    T * px;

    // Make this smart pointer non-copyable
    scoped_array(scoped_array const &);
    scoped_array & operator=(scoped_array const &);

    typedef scoped_array<T> this_type;

public:
    typedef T element_type;

    explicit scoped_array(T * p = 0) : px(p)  { }

    ~scoped_array() {
        checked_array_delete(px);
    }

    void reset(T * p = 0) {
        assert(p == 0 || p != px); // catch self-reset errors
        this_type(p).swap(*this);
    }

    T & operator[](std::ptrdiff_t i) const {
        assert(px != 0);
        assert(i >= 0);
        return px[i];
    }

    T * get() const {
        return px;
    }

    operator bool () const {
        return px != 0;
    }

    bool operator ! () const {
        return px == 0;
    }

    void swap(scoped_array & b) {
        T * tmp = b.px;
        b.px = px;
        px = tmp;
    }
};

关于c++ - 具有 POD 和未初始化值数组的 auto_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868532/

相关文章:

c++ - 选择排序程序显示异常结果

javascript - 通过mongo中的填充字段查询

python - 无替换样本的 np.random.choice 与 np.random.shuffle 的比较

java - Full GC 什么时候触发?

ios - viewDidUnload 的最佳实践?

c++ - 按值传递时谁调用拷贝构造函数?

c++ - Getline 只读第一个字

c++ - 在 std::map<..., boost::any> 中初始化子图

c++ - 文本文件中的值未写入二维数组?

c++ - 为发送到方法的参数分配的大小