简体   繁体   English

重新初始化指针是不好的做法吗?

[英]Is it bad practice to reinitialize a pointer?

I have an Image class and initially I do not know the image dimensions, so I just initialize a data_ pointer to be an array of size 0. Later when I find the image information I reinitialize data_ to a new size. 我有一个Image类,最初我不知道图像的尺寸,所以我只是将data_指针初始化为一个大小为0的数组。后来当我找到图像信息时,我将data_重新初始化为一个新的大小。 Will this create any problem in memory? 这会在内存中造成任何问题吗? and is there a cleaner way to do this? 有没有更清洁的方法来做到这一点?

Below is the class I have written: 以下是我写的课程:

class Image
{
private:
    int numRows_, numCols_;
    unsigned char* data_;
public:
    Image() : numRows_(0), numCols_(0), data_(new unsigned char[0])
    {}
    void setData(int r, int c, unsigned char* data)
    {
        this->numRows_ = r;
        this->numCols_ = c;
        this->data_ = new unsigned char[r*c];
        for (int i = 0; i < r*c; i++)
        {
            this->data_[i] = data[i];
        }
    }
    int rows();
    int cols();
    unsigned char* data();
    ~Image();
};

Thanks in advance 提前致谢

This will in fact leak memory. 这实际上会泄漏内存。 The call to new allocates memory for the array, even if it is empty . new的调用为数组分配内存, 即使它是空的 As soon as you reassign data_ , the previous array is leaked and can no longer be freed. 一旦重新分配data_ ,前一个数组就会泄漏,无法再释放。

You can either make sure you delete[] any new[] you allocate, or just don't allocate an empty array and instead set data_ to nullptr until you have meaningful data to use. 您可以确保delete[]您分配的任何new[] ,或者只是不分配空数组,而是将data_设置为nullptr直到您有有意义的数据要使用。

An even better idea is don't allow the creation of an object in an invalid state, require the data in the constructor - see RAII : 更好的想法是不允许在无效状态下创建对象,需要构造函数中的数据 - 请参阅RAII

In RAII, holding a resource is a class invariant, and is tied to object lifetime: resource allocation (or acquisition) is done during object creation (specifically initialization), by the constructor, while resource deallocation (release) is done during object destruction (specifically finalization), by the destructor. 在RAII中,持有资源是类不变的,并且与对象生存期相关联:资源分配(或获取)在对象创建(特别是初始化)期间由构造函数完成,而资源释放(释放)在对象销毁期间完成(具体完成),由析构函数。

If you do decide to keep setData , then as mentioned in comments, you also must make sure to delete[] existing data in setData before reassigning data_ , in case the method is called more than once. 如果您决定保留setData ,那么如注释中所述,您还必须确保在重新分配data_之前delete[] setData现有数据,以防多次调用该方法。

I think a cleaner way to do so will be using a vector: 我认为更清洁的方法是使用矢量:

std::vector<unsigned  char> v; // vector with size 0
v.resize(r*c);                 // after size is known, just resize

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在返回指针的Getter上进行数组访问,这是不好的做法吗? - Array access on a Getter that returns a pointer, is that bad practice? 是std :: thread的共享指针是一种不好的做法? - is a shared pointer to std::thread a bad practice? 在DLL中分配内存并将其指向客户端应用程序的指针是不好的做法吗? - Is it bad practice to allocate memory in a DLL and give a pointer to it to a client app? C ++成员函数需要指针,不好的做法是通过引用传递? - C++ member function requires pointer, bad practice to pass by reference? 子对象具有指向其父对象的指针是不好的做法吗? - Is it bad practice for a child object to have a pointer to its parent? Typecast将普通指针指向unique_ptr是一种不好的做法吗? - Is Typecasting a normal pointer to a unique_ptr a bad practice? 重载函数以获取指针或引用是不好的做法吗? - Is it bad practice to overload a function to take both a pointer or a reference? 在构造函数中使用原始指针以立即将其包装在智能指针中是否被认为是不好的做法? - Is it considered bad practice to use a raw pointer in a constructor with the intention of wrapping it immediately in a smart pointer? 让子对象引用其父对象而不是指针是不好的做法吗? - Is it bad practice to have a child object have a reference to its parent object instead of a pointer? 拥有一个带有原始指针访问器的unique_ptr成员是不好的做法吗? - Is it bad practice to have a unique_ptr member that has a raw pointer accessor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM