[英]Errоr: Invalid address specified to RtlValidateHeap
我正在制作一個程序來檢查堆棧是否相等。
使用此代碼,我收到一個錯誤:
HEAP[ldf.exe]:指定給 RtlValidateHeap 的地址無效
它一直有效,直到在 func equal_stacks
中返回答案,所以也許有一個問題與刪除他在 function 啟動時獲得的堆棧有關?
析構函數是我的老師寫的,所以它可能是正確的。 也許問題出在equals_stacks
function 的某處?
#include <iostream>
template<typename T>
class stackm{
int capacity;
int count;
T* data;
public:
stackm();
~stackm();
void push(const T& elem);
void push(T&& elem);
void pop();
T& top();
const T& top() const;
int size() const;
};
template<typename T>
stackm<T>::stackm() {
capacity = 1;
count = 0;
data = new T[1];
}
template<typename T>
stackm<T>:: ~stackm() {
delete[] data;
}
template<typename T>
void stackm<T>::pop() {
--count;
}
template <typename T>
void stackm<T>::push(const T& elem) {
if (this->count == this->capacity) {
size_t new_capacity = capacity * 2;
T* new_data = new T[new_capacity];
for (size_t i = 0; i < this->capacity; ++i) {
new_data[i] = std::move(this->data[i]);
}
delete[] this->data;
this->capacity = new_capacity;
this->data = new_data;
}
data[count++] = elem;
}
template <typename T>
void stackm<T>::push(T&& elem) {
if (this->count == this->capacity) {
size_t new_capacity = capacity * 2;
T* new_data = new T[new_capacity];
for (size_t i = 0; i < this->capacity; ++i) {
new_data[i] = std::move(this->data[i]);
}
delete[] this->data;
this->capacity = new_capacity;
this->data = new_data;
}
data[count++] = elem;
}
template <typename T>
int stackm<T>::size() const {
return count;
}
template <typename T>
T& stackm<T>::top() {
return data[count - 1];
}
template <typename T>
const T& stackm<T>::top() const {
return data[count - 1];
}
template<typename Stack>
bool equal_stacks(Stack s1, Stack s2) {
if (s1.size() != s2.size()) {
return false;
}
while (s1.size() != 0) {
if (s2.top() != s1.top()) {
return false;
}
s1.pop();
s2.pop();
}
return true;
}
int main() {
stackm<int> stac1 = stackm<int>();
stackm<int> sd23 = stackm<int>();
sd23.push(23);
sd23.push(45);
std::cout << equal_stacks<stackm<int>>(stac1, sd23);
}
你有delete[] data;
在第 26 行delete data;
在第 39 行data
是一個數組,因此您只能使用delete[]
刪除它。
而且您永遠不會在任何地方使用new_data
。 您可能想將它分配給第 39 行之后的data
。
另請注意,您已經編寫了一個自定義析構函數,因此您應該遵循規則 5 並指定您的 class 在復制構造函數、復制賦值、移動構造函數和移動賦值的情況下的行為方式。
您還想學習如何調試小程序。
我通過引用將值傳遞給 function 來解決它
#include <iostream>
template<typename T>
class stackm{
int capacity;
int count;
T* data;
public:
stackm();
~stackm();
void push(const T& elem);
void push(T&& elem);
void pop();
T& top();
const T& top() const;
int size() const;
};
template<typename T>
stackm<T>::stackm() {
capacity = 1;
count = 0;
data = new T[1];
}
template<typename T>
stackm<T>:: ~stackm() {
delete[] data;
}
template<typename T>
void stackm<T>::pop() {
--count;
}
template <typename T>
void stackm<T>::push(const T& elem) {
if (this->count == this->capacity) {
size_t new_capacity = capacity * 2;
T* new_data = new T[new_capacity];
for (size_t i = 0; i < this->capacity; ++i) {
new_data[i] = std::move(this->data[i]);
}
delete[] this->data;
this->capacity = new_capacity;
this->data = new_data;
}
data[count++] = elem;
}
template <typename T>
void stackm<T>::push(T&& elem) {
if (this->count == this->capacity) {
size_t new_capacity = capacity * 2;
T* new_data = new T[new_capacity];
for (size_t i = 0; i < this->capacity; ++i) {
new_data[i] = std::move(this->data[i]);
}
delete[] this->data;
this->capacity = new_capacity;
this->data = new_data;
}
data[count++] = elem;
}
template <typename T>
int stackm<T>::size() const {
return count;
}
template <typename T>
T& stackm<T>::top() {
return data[count - 1];
}
template <typename T>
const T& stackm<T>::top() const {
return data[count - 1];
}
template<typename Stack>
bool equal_stacks(Stack &s1, Stack &s2) {
Stack temp1 = Stack();
Stack temp2 = Stack();
if (s1.size() != s2.size()) {
return false;
}
while (s1.size() != 0) {
if (s1.top() != s2.top()) {
restoreStack<Stack>(s1, temp1);
restoreStack<Stack>(s2, temp2);
return false;
}
temp1.push(s1.top());
temp2.push(s2.top());
s1.pop();
s2.pop();
}
restoreStack<Stack>(s1, temp1);
restoreStack<Stack>(s2, temp2);
return true;
}
我決定不聽從 WhozCraig 的建議並實施 3/5/0 規則,因為堆棧代碼是老師給我的,我認為我不應該更改它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.