C++ 增加指针数组大小-C++;

C++ 增加指针数组大小-C++;,c++,arrays,pointers,min-heap,C++,Arrays,Pointers,Min Heap,我正在创建一个Min Heap类(用于实践),它完全可以工作,但是,在我看来,指针数组的increase capacity函数(保存堆数组)可能会导致内存泄漏。(请原谅我糟糕的编码)。下面我展示了我的函数、构造函数和私有成员init,但是,代码中并不是这样构造的,就像这样是为了方便编写文章 int* heap; // init as a private member MinHeap() { this->total_capacity = 10; // init in default

我正在创建一个Min Heap类(用于实践),它完全可以工作,但是,在我看来,指针数组的increase capacity函数(保存堆数组)可能会导致内存泄漏。(请原谅我糟糕的编码)。下面我展示了我的函数、构造函数和私有成员init,但是,代码中并不是这样构造的,就像这样是为了方便编写文章

int* heap; // init as a private member

MinHeap() {
   this->total_capacity = 10; // init in default constructor
   heap = new int[total_capacity];
}

// if total capacity is reached:
 // creates a larger array and copies all elements over to it
 void ensureExtraCapacity() {
    if (length == total_capacity) {
       total_capacity *= 2;
       int* temp = new int[total_capacity];
       for (int i = 0; i < length; i++)
          temp[i] = heap[i];
       heap = temp;
       delete[] temp; // <= this is causing me issues
       cout << "Capacity added." << endl;
    }
 }
int*heap;//作为私人成员初始化
MinHeap(){
this->total_capacity=10;//默认构造函数中的init
堆=新的整数[总容量];
}
//如果达到总容量:
//创建一个更大的数组并将所有元素复制到其中
无效保证额外能力(){
if(长度==总容量){
总容量*=2;
int*temp=新int[总容量];
for(int i=0;i