C++ 为RtlValidateHeap(00E90000,00E9FBC8)Project.exe指定的地址无效,已触发断点

C++ 为RtlValidateHeap(00E90000,00E9FBC8)Project.exe指定的地址无效,已触发断点,c++,class,oop,heap,C++,Class,Oop,Heap,我在运行程序时遇到此错误。有人知道这里有什么问题吗 HEAP[Project3.exe]:为RtlValidateHeap(00E90000,00E9FBC8)指定的地址无效 Project3.exe已触发断点 以下是所有代码(已更新)。您认为是dome内存问题还是IDE问题 #include <iostream> #include <fstream> #include "HeapSort.h" using namespace std; int main(int arg

我在运行程序时遇到此错误。有人知道这里有什么问题吗

HEAP[Project3.exe]:为RtlValidateHeap(00E90000,00E9FBC8)指定的地址无效 Project3.exe已触发断点

以下是所有代码(已更新)。您认为是dome内存问题还是IDE问题

#include <iostream>
#include <fstream>
#include "HeapSort.h"
using namespace std;

int main(int argc, char* argv[]) {

    // Input/OutPut files.
    string fileInput = argv[1];
    string fileOutput1 = argv[2];
    string fileOutput2 = argv[3];


    ofstream ofs;
    //This for loop is used to clear the output files before writting to them.
    for (int i = 2; i <= 3; i++){ ofs.open(argv[i], std::ofstream::out | std::ofstream::trunc); ofs.close(); }

    //Initializing HeapSort class
    HeapSort HS(fileInput, fileOutput1, fileOutput2);
    HS.buildHeap();
    HS.deleteHeap();

    system("PAUSE");
    return 0;
}



//HeapSort.h file.
#ifndef HeapSort_H                  
#define HeapSort_H
#include <iostream>
#include <fstream>
using namespace std;

class HeapSort {


public:

    int* heapAry;
    int numItems, rootIndex, fatherIndex, leftKidIndex, rightKidIndex, minKidIndex, data;
    string input, output1, output2;

    //constructor
    HeapSort(string filename1, string filename2, string filename3);

    int countData();
    void buildHeap();
    void deleteHeap();
    int getRoot();
    void replaceRoot();
    void bubbleUp(int s);
    void bubbleDown(int fatherIndex);
    bool isLeaf(int index);
    bool isRoot(int index);
    int findMinKidIndex(int fatherIndex);
    bool isHeapEmpty();
    bool isHeapFull();
    void printHeap(int s);
    void inserOneDataItem(int data);

};//end of HeapSort class
#endif 


    //HeapSort.cpp file.
    #include "HeapSort.h"
    #include <iostream>
    #include <fstream>
    #include<string>
    #include <sstream>
    #include <algorithm>
    using namespace std;

    //Used for int conversion to string since library is broken.
    namespace patch{
        template < typename T > std::string to_string(const T& n){
            std::ostringstream stm;
            stm << n;
            return stm.str();
        }
    }//End of patch namespace.


    //Constructor
    HeapSort::HeapSort(string filename1, string filename2, string filename3){
        data = countData() + 1;
        heapAry = new int[data];

        for (int i = 0; i <= data; i++){ heapAry[i] = 0; }

        input = filename1;
        output1 = filename2;
        output2 = filename3;
    }//End of cunstructor


    int HeapSort::countData(){

        ifstream inFile;
        inFile.open(input);
        int tempInt = 0;
        int counter = 0;

        while (inFile >> tempInt) {  cout << tempInt << "\n"; counter++; }//end of while loop
        inFile.close();
        return counter;
    }//End of countData function


    void HeapSort::buildHeap(){
        ifstream inFile;
        inFile.open(input);
        int number = 0;
        rootIndex = 1;
        while (inFile >> number) { 
            inserOneDataItem(number);
            int kidIndex = heapAry[0];
            bubbleUp(kidIndex);
            printHeap(1);
        }//end of while loop
        inFile.close();
    }//End of buildHeap function


    void HeapSort::deleteHeap(){
        ofstream outFile;
        outFile.open(output2, ios::app);
        while (isHeapEmpty() != true){
            int data = getRoot();
            if (data != 0){ outFile << "  |  " << data << "  |  \n"; }
            replaceRoot();
            fatherIndex = rootIndex;
            bubbleDown(fatherIndex);
            printHeap(2);
        }//end of while loop
        outFile.close();
    }//End of deleteHeap function.


    int HeapSort::getRoot(){
        return heapAry[1];
    }//End of getRoot function


    void HeapSort::replaceRoot(){
        heapAry[1] = heapAry[heapAry[0]];
        heapAry[0] = heapAry[0] - 1;
    }//End of replaceRoot function


    void HeapSort::bubbleUp(int s){
        if (isRoot(s)){ return; }//end if clause
        else{
            fatherIndex = s / 2;
            if (heapAry[s] >= heapAry[fatherIndex]){
                return;
            }//end inner if clause
            else{
                int temp = heapAry[s];
                heapAry[s] = heapAry[fatherIndex];
                heapAry[fatherIndex] = temp;
                bubbleUp(fatherIndex);
            }//end inner else clause
        }//end outter else clause
    }//End of bubbleUp function


    void HeapSort::bubbleDown(int fatherIndex){
        if (isLeaf(fatherIndex)){ return; }//end if clause
        else{
            leftKidIndex = fatherIndex * 2;
            rightKidIndex = (fatherIndex * 2) + 1;
            minKidIndex = findMinKidIndex(fatherIndex);
            if (heapAry[minKidIndex] >= heapAry[fatherIndex]){ return; }//end inner if clause
            else{
                int temp = heapAry[fatherIndex];
                heapAry[fatherIndex] = heapAry[minKidIndex];
                heapAry[minKidIndex] = temp;
                bubbleDown(minKidIndex);
            }//end inner else clause
        }//end outter else clause
    }//End of bubbleDown function


    bool HeapSort::isLeaf(int index){
        if (index * 2 > heapAry[0] && (index * 2) + 1 > heapAry[0]){ return true; }//end if clause
        else { return false; }//end else clause
    }//End of isLeaf function


    bool HeapSort::isRoot(int index){
        return (index == 1);
    }//End of isRoot function


    int HeapSort::findMinKidIndex(int fatherIndex){
        if (isLeaf(fatherIndex) == true) return fatherIndex;
        if ((fatherIndex * 2) + 1 > heapAry[0] && heapAry[(fatherIndex * 2)] < fatherIndex) return fatherIndex * 2;
        if ((fatherIndex * 2) + 1 < fatherIndex && (fatherIndex * 2) == 0) return (fatherIndex * 2) + 1;
        else{
            int s = std::min(heapAry[fatherIndex * 2], heapAry[(fatherIndex * 2) + 1]);
            if (s == heapAry[fatherIndex * 2]){ return fatherIndex * 2; }
            else { return (fatherIndex * 2) + 1; }
        }//end outer else clause
    }//End of findMinKidIndex function


    bool HeapSort::isHeapEmpty(){ return (heapAry[0] == 0); }//end of isHeapEmpty method


    bool HeapSort::isHeapFull(){ return (numItems == data); }//end of isHeapFull method


    void HeapSort::printHeap(int s){
        ofstream outFile;
        outFile.open(output1, ios::app);
        if (s == 1){ outFile << "Printing heap after adding one element! \n\n"; }
        else if (s == 2){ outFile << "Printing heap after removing one element! \n\n"; }

        for (int i = 0; i <= heapAry[0]; i++){
            if (heapAry[i] != 0){ outFile << heapAry[i] << "  |  "; }//end if clause
        }//end for loop
        outFile << "\n\n\n";
    //  outFile.close();
    }//end of printHeap


    void HeapSort::inserOneDataItem(int data){
        heapAry[0]++;
        heapAry[heapAry[0]] = data;
    }//end of inserOneDataItem method
#包括
#包括
#包括“HeapSort.h”
使用名称空间std;
int main(int argc,char*argv[]){
//输入/输出文件。
字符串fileInput=argv[1];
字符串fileOutput1=argv[2];
字符串fileOutput2=argv[3];
流的速度;
//此for循环用于在写入输出文件之前清除这些文件。
for(int i=2;i std::string to_string(const T&n){
std::ostringstream stm;
stm tempInt){cout数){
内标(编号);
int kidex=heapAry[0];
气泡指数;
打印堆(1);
}//while循环结束
infle.close();
}//构建堆函数结束
void HeapSort::deleteHeap(){
出流孔的直径;
outFile.open(output2,ios::app);
while(ishapemppty()!=true){
int data=getRoot();
if(data!=0){outFile heapAry[0]&&(index*2)+1>heapAry[0]){return true;}//end if子句
else{return false;}//end-else子句
}//isLeaf函数结束
bool HeapSort::isRoot(int索引){
收益率(指数=1);
}//isRoot函数结束
int HeapSort::findMinKidIndex(int fatherIndex){
if(isLeaf(fatherIndex)==true)返回fatherIndex;
如果((fatherIndex*2)+1>heapAry[0]&&heapAry[(fatherIndex*2)]如果(s==1){outFile我没有完全阅读您的代码,但是我对下面的代码有疑问


data=countData()+1; heapAry=新整数[数据]


对于(int i=0;i实际上,整个问题是countData函数的输入在调用时从未初始化,这就是该函数返回0值并导致该错误的方式。由所有者解决了…

您的代码有一个错误。如果不提供一个//buildHeap函数的末尾
heapAry[heapAry[0]]=data;
最有可能访问超出边界的数组(无论哪种方式,这都是一种奇怪的构造)。此外,您在任何位置对数组访问执行无边界检查,并且由于循环条件错误,您的构造函数已调用未定义的行为
我认为循环条件没有问题。存在堆问题损坏。可能是我的IDE,但我不确定。一次或两次代码运行正常,然后再次运行问题。是的,循环条件存在问题-您可以选择忽略它,但这不会减少未定义的行为(在您的情况下,这表现为堆损坏)。将自己的错误归咎于IDE不会修复代码