Heap-Beschädigung Erkannt: nach dem Normalen block (#126)

Kann ich nicht für das Leben von mir herauszufinden, warum ich immer diese Debug-Fehler:

Heap-Beschädigung Erkannt: nach dem Normalen block (#126) an
0x004cF6c0
CRT festgestellt, dass die Anwendung geschrieben, um den Speicher nach dem Ende von heap Kerl.

Ich verstehe, dass Sie brauchen, um Speicher frei, wenn Sie neue Betreiber,
das habe ich getan und ich bin noch immer Probleme.

aus irgendeinem Grund das Programm nicht zu Ende, richtig in die rekursive Funktion.
Ich gedebuggt und es ging durch jede Zeile code mit breakpoints.

Am Ende der if-Anweisung in countSum es irgendwie subtrahiert 1 von ich
und dann kommt wieder die if-block.....was es nicht tun soll.

Warum ist das passiert?

/*this program calculates the sum of all the numbers in the array*/

#include <iostream>
#include <time.h>

using namespace std;

/*prototype*/
void countSum(int, int, int, int*);

int main(){

    bool flag;
    int num;

    int sum = 0, j=0;
    int *array =  new int;

    do{

        system("cls");
        cout<<"Enter a number into an array: ";
        cin>>array[j];

        cout<<"add another?(y/n):";
        char choice;
        cin>>choice;
        choice == 'y' ? flag = true : flag = false;

        j++;

    }while(flag);

    int size = j;

    countSum(sum, 0, size, array);
    //free memory
    delete array;
    array = 0;

    return 0;
}

void countSum(int sum, int i, int size, int *array){

    if (i < size){
        system("cls");

        cout<<"The sum is  :"<<endl;
        sum += array[i];
        cout<<"...."<<sum;

        time_t start_time, stop_time;
        time(&start_time);

        do
        {
            time(&stop_time); //pause for 5 seconds
        }
        while((stop_time - start_time) < 5);

        countSum(sum, (i+1) , size, array); //call recursive function
    }
}
InformationsquelleAutor user1066524 | 2012-07-19
Schreibe einen Kommentar