简体   繁体   English

有没有办法判断是否已分配内存缓冲区并且必须将其删除?

[英]Is there a way to tell if a memory buffer was allocated and must be deleted?

I came up with a clever (or stupid) design pattern. 我想出了一个聪明的(或愚蠢的)设计模式。 In one of our use cases, there might be the need to create one large memory buffer that would be used over and over for the lifetime of the class instance. 在我们的一种使用情况下, 可能需要创建一个大型内存缓冲区,该缓冲区将在类实例的生存期内反复使用。 In the case that we don't need the memory buffer, I'd like to avoid creating it. 在我们不需要内存缓冲区的情况下,我想避免创建它。 So I came up with the following: 因此,我提出了以下建议:

class Class {
  public:
    void func()
    {
      if (A) {
        // do something
      } else if (B) {
        // need data buffer, which we will recycle
        static float * data = new float[1000000];
        // do something with the data
      } else {
        ...
      }
    }

  ~Class() {
    // to delete[] or not to delete[] that is the question
  }
}

Is there a way to deallocate this buffer in the destructor? 有没有办法在析构函数中释放此缓冲区? I can use delete[] , but the question is: how do we know if we have to delete the buffer? 我可以使用delete[] ,但问题是:我们如何知道是否必须删除缓冲区? In other words, is there a clever way to know if if (B) was executed? 换句话说,是否有一个聪明的方法来知道是否执行了if (B) Of course I can do this with flags, but I wasn't sure if there was a better way to do it. 我当然可以用标志来做到这一点,但是我不确定是否有更好的方法来做到这一点。 The concept of a static memory buffer on the heap that may or may not be initialized is sort of confusing to me. 堆上可能会或可能不会初始化的静态内存缓冲区的概念令我感到困惑。

You could allow data to exist in the class, but default its value to NULL . 您可以允许data存在于类中,但默认值为NULL When you delete [] it, it will be a nop if it was not allocated. delete []时,如果未分配,它将是nop

If you're using C++11, use nullptr instead of NULL , as it is safer (because nullptr does not convert to integral type, and NULL is usually defined as 0 ) 如果您使用的是C ++ 11,请使用nullptr而不是NULL ,因为它更安全(因为nullptr不会转换为整数类型,并且NULL通常定义为0

Alternatively, create data as a vector<float> data instead of a float* , and you don't need to do anything in the destructor. 另外,也可以将data创建为vector<float> data而不是float* ,而无需在析构函数中进行任何操作。 You're not really wasting any memory if you never add anything to the vector. 如果您从不向向量添加任何内容,那么您实际上并不会浪费任何内存。 (Thanks Manu343726 and Malloc) (感谢Manu343726和Malloc)

Embrace std::vector 拥抱std::vector

#include <iostream>
#include <vector>

class Class {
public:
  void func(bool test)
  {
    if (test) {
      data_.resize(1000000);
    }
  }

  ~Class() {
    std::cout << "Destroying data of size: " << data_.size() << std::endl;
  }

private:
  std::vector<float> data_;
};

int main() {
  Class A;
  Class B;

  A.func(true);
  B.func(false);
}

This prints: 打印:

Destroying data of size: 0
Destroying data of size: 1000000

Live Demo 现场演示

You could also defer all this logic to vector. 您也可以将所有这些逻辑推迟到矢量。 When your class is destructed the vector destructor will take care of all cases. 当您的类被破坏时,向量析构函数将处理所有情况。 In general vector is an acceptable solution to buffers. 通常,向量是缓冲区的可接受解决方案。

class Class {
  public:
    void func()
    {
      if (A) {
        // do something
      } else if (B) {
        // need data buffer, which we will recycle
        MyVector.resize(1000000,0.0);
        // do something with the data
      } else {
        ...
      }
    }
   private: 
      vector<float> MyVecto_;

  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM