gpt4 book ai didi

模板数组中的 C++ 堆损坏

转载 作者:太空宇宙 更新时间:2023-11-04 14:44:51 25 4
gpt4 key购买 nike

如标题所述,我的 C++ 代码中存在堆损坏问题。我知道有很多主题涉及堆损坏问题,我访问了很多主题,我在很多网站上阅读了很多关于这些问题的内容,我什至使用 Visual Leak Detector 来查找内存的位置泄露。我似乎仍然无法弄清楚为什么会发生堆损坏。

我的代码:

#include <iostream>
#include "stdafx.h"
#include "cstdlib"
#include <vld.h>
#include <math.h>

using namespace std;

template <class T>
class PrioQueue
{
private:
int size_;
int tempIndex;
public:
T *bottom_;
T *top_;
PrioQueue(int n =20){
size_ = n;
bottom_ = new T[size_];
top_ = bottom_;
}
void push(T c){
//add the item to the list
*top_ = c;
top_++;

//Save the old stack values in the temp memory
T* values = bottom_;
T tempItem;
int index = num_items();
cout << "Num items: " << index << endl;
cout << "1" << endl;
while(index > 1){
cout << "2" << endl;
if(values[index-1] > values[index-2])
{
cout << "2b" << endl;
tempItem = values[index-2];
values[index-2] = c;
values[index-1] = tempItem;

}
cout << "3" << endl;
index--;
}
cout << "4" << endl;

}

// + operator
PrioQueue* operator+ (PrioQueue que2)
{
PrioQueue<T>* temp = new PrioQueue<T>();
cout << "Created temporary queue" << endl;
for(int i = 0; i <num_items(); i++)
{
cout << "Element in list: " << bottom_[i] << endl;
temp->push(bottom_[i]);
cout << "Temp is now: ";
temp->print();
}
for(int i = 0; i < que2.num_items(); i++)
{
cout << "Element in list: " << que2.bottom_[i] << endl;
temp->push(que2.bottom_[i]);
cout << "Temp is now: ";
temp->print();
}
cout << "Ran loop" << endl;
return temp;
}

// * operator
PrioQueue* operator* (PrioQueue que2)
{
PrioQueue<T>* temp = new PrioQueue<T>();
for(int i = 0; i < num_items(); i++)
{
for(int j = 0; j < que2.num_items(); j++)
{
if(bottom_[i] == que2.bottom_[j])
{
temp->push(bottom_[i]);
break;
}
}
}

return temp;
}

friend ostream& operator<< (ostream& output, PrioQueue& q) {
for(T *element = q.bottom_; element < q.top_; element++)
output << *element << " | ";
return output;
}

int num_items() {
return (top_ - bottom_ );
}
T pop(){
top_--;
return *top_;
}
int full() {
return (num_items() >= size_);
}
int empty() {
return (num_items() <= 0);
}
void print(){
cout << "Print function..." << endl;
cout << "Stack currently holds " << num_items() << " items: " ;
for (T *element=bottom_; element<top_; element++) {
cout << " " << *element;
}
cout << "\n";
}
~PrioQueue(){ // stacks when exiting functions
delete [] bottom_;
}
};

int main()
{
PrioQueue<int> *p1 = new PrioQueue<int>(20);
p1->push(5);
p1->push(2);
p1->push(8);
p1->push(4);
p1->print(); cout << "\n";

PrioQueue<int> *p2 = new PrioQueue<int>(5);
p2->push(33);
p2->push(66);
p2->push(8);
p2->push(5);
p2->print(); cout << "\n";

//add them together

p1->print();
p2->print();

((*p1) + (*p2))->print();
((*p1) * (*p2))->print();


PrioQueue<float> *f1 = new PrioQueue<float>(5);
f1->push(1.1f);
f1->push(5.2f);
f1->push(8.3f);
f1->push(14.4f);
f1->push(17.5f);
f1->print(); cout << "\n";

PrioQueue<float> *f2 = new PrioQueue<float>(4);
f2->push(2.2f);
f2->push(6.7f);
f2->push(10.3f);
f2->push(15.6f);
f2->print();
cout << "\n";

//add them together
((*f1) + (*f2))->print();

// Multiply them.
((*f1) * (*f2))->print();

cout << "\n";
cout << p1 << endl;
cout << f1 << endl;

cout << "Press any key to exit...";
cin.get();
cin.get();

delete p1;
delete p2;
delete f1;
delete f2;
return 0;
}

我已经尝试删除所有内容并从头开始。似乎在改变:

    delete [] bottom_;

收件人:

    delete bottom_;

已修复它,但那是在我将值推送到数组之前。

请问有什么问题的请指教。将不胜感激。

提前谢谢你,

Greats Matti Groot。

最佳答案

您提到的更改会导致未定义的行为。如果你用 new[] 得到了一些东西,你必须把它传递给 delete[]。 Plain delete 仅适用于您通过 plain new 获得的内容。

您的运算符 + 和 * 创建新对象并返回指向它的指针。我没有看到任何删除这些对象的尝试,所以难怪你有泄漏。 (无缘无故地返回有义务的指针被认为是糟糕的设计,更何况是来自应该产生对象的对象的操作符。)

关于模板数组中的 C++ 堆损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17250415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com