[英]Debug Assertion Failed! Expression: is_block_type_valid(header->_block_use). Object wont init and Push error
In advanced, let me thank your for looking at this code for me, because it has been bugging me for a while now and I can't seem to find the issue. 在高级方面,请允许我感谢您为我查看此代码,因为它已经困扰了我一段时间了,我似乎找不到问题。
Whenever I run it, it doesn't throw any errors within the console, instead it throws this error: 每当我运行它时,它都不会在控制台内引发任何错误,而是会引发以下错误:
Debug Assertion Failed!
调试断言失败!
Program: [Filepath to .exe] File: minkernel\\crts\\ucrt\\src\\appcrt\\heap\\debug_heap.cpp Line 892
程序:[.exe的文件路径]文件:minkernel \\ crts \\ ucrt \\ src \\ appcrt \\ heap \\ debug_heap.cpp第892行
Expression: is_block_type_valid(header->_block_use)
表达式:is_block_type_valid(header-> _ block_use)
I've been trying to figure it out, but it just wont work for me. 我一直在试图找出答案,但它对我来说不起作用。 I believe it has something to do with the template types I am passing in, as it only crashes whenever I try to initialize a minHeap object or try my Push method, which I also believe is an issue.
我相信它与我传入的模板类型有关,因为它仅在我尝试初始化minHeap对象或尝试我的Push方法时才崩溃,我也认为这是一个问题。
Once again, thank you all so much for looking at my code. 再次非常感谢您查看我的代码。
Main: 主要:
#include "minHeap.h"
#include "Node.h"
#include <iostream>
using namespace std;
int main() {
Node<char> A = Node<char>(0, 'A');
Node<char> B = Node<char>(1, 'B');
Node<char> C = Node<char>(2, 'C');
Node<char> D = Node<char>(3, 'D');
minHeap<char> myHeap = minHeap<char>();
//Below doesn't work, something about subscript is not of integrap type
//myHeap.push(A.index, A.value);
//myHeap.push(B.index, B.value);
//myHeap.push(C.index, C.value);
//myHeap.push(D.index, D.value);
cout << A.index << endl;
myHeap.~minHeap();
return 0;
}
Here is Node.h: 这是Node.h:
#pragma once
template<typename T>
class Node
{
public:
float index;
T value;
Node(float indx, T val);
~Node();
};
template<typename T>
inline Node<T>::Node(float indx, T val)
{
index = indx;
value = val;
}
template<typename T>
inline Node<T>::~Node()
{
}
And finally, minHeap: 最后,minHeap:
#pragma once
template<typename T>
class minHeap
{
private:
T* arr[100];
int arrSize = 0;
void heapifyUp(int indx);
void heapifyDown(int indx);
int getParent(int indx);
int childLeft(int indx);
int childRight(int indx);
int swap(int indxA, int indxB);
public:
minHeap();
~minHeap();
void push(int indx, T val);
void pop();
};
template<typename T>
inline minHeap<T>::minHeap()
{
}
template<typename T>
inline minHeap<T>::~minHeap()
{
delete[] arr;
}
template<typename T>
inline void minHeap<T>::heapifyUp(int indx)
{
if (indx <= 0) return;
int j = getParent(indx);
if (arr[indx] < arr[j]) {
int temp = arr[indx];
arr[indx] = arr[j];
arr[j] = temp;
}
heapifyUp(j);
}
template<typename T>
inline void minHeap<T>::heapifyDown(int indx)
{
int j;
//if no left child
if (childLeft(indx) > arrSize - 1) return;
//if no right child
if (childRight(indx) > arrSize - 1) j = childLeft(indx);
//No children
else j = (arr[childLeft(indx)] < arr[childRight(indx)]) ? (childLeft(indx)):(childRight(indx));
if (arr[indx] > arr[indx]) {
int temp = arr[indx];
arr[indx] = arr[j];
arr[j] = temp;
}
heapifyDown(j);
}
template<typename T>
inline int minHeap<T>::getParent(int indx)
{
return (indx - 1) / 2;
}
template<typename T>
inline int minHeap<T>::childLeft(int indx)
{
return 2 * i + 1;
}
template<typename T>
inline int minHeap<T>::childRight(int indx)
{
return 2 * i + 2;
}
template<typename T>
inline int minHeap<T>::swap(int indxA, int indxB)
{
int tempA = arr[indxA];
int tempB = arr[indxB];
arr[indxA] = tempB;
arr[indxB] = tempA;
return 0;
}
template<typename T>
inline void minHeap<T>::push(int indx, T val)
{
//Something with Array is broken. Fix it pupper
int tempVal = arr[indx];
arr[indx] = val;
arrSize += 1;
heapifyUp(arrSize - 1);
}
template<typename T>
inline void minHeap<T>::pop()
{
int temp = arr[0];
arr[0] = arr[arrSize - 1];
arr[arrSize - 1] = nullptr;
arrSize -= 1;
heapifyDown(0);
}
Why are you calling myHeap.~minHeap();
你为什么叫
myHeap.~minHeap();
? ? This results in
myHeap
being destroyed twice, with the second call trying to free memory that has already been freed. 这导致
myHeap
被破坏两次,第二次调用试图释放已经释放的内存。 This can cause the error you're seeing. 这可能会导致您看到的错误。
You can construct your variables a lot more concisely: 您可以更简洁地构造变量:
Node<char> A(0, 'A');
minHeap<char> myHeap;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.