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

问题描述 投票:-3回答:3

运行程序时出现此错误。有谁知道这里有什么问题?

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

这里是所有代码(更新)。你认为是圆顶内存问题还是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
c++ class oop heap
3个回答
0
投票

我没有完全通过你的代码,但我怀疑下面的代码。


data = countData()+ 1; heapAry = new int [data];

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

例如,您将10个数据分配到heapAry(0-9)语句中,并将11个数据初始化(0-10)到同一个变量。

你能检查下面的循环(0-10)。 for(int i = 0; i <= data; i ++){heapAry [i] = 0; }

Thanges,Mannavagam P.


0
投票

好吧,我只是调试了代码,问题看起来像是在countData fucntion中,它应该返回38,但返回0。有没有想过为什么会这样?


0
投票

实际上整个问题是countData函数的输入在调用时从未被初始化,这就是该函数返回0值并导致该错误的方式。由店主解决.....

© www.soinside.com 2019 - 2024. All rights reserved.