调试错误堆损坏c ++指针

时间:2017-11-20 13:22:50

标签: c++ pointers debugging dynamic

所以我有这个任务,告诉用户输入两个数字,让他从菜单中选择一些基本操作(如计算器)。 这个练习的目的是帮助我们更熟悉函数/动态分配的指针/指针...教授更喜欢我们使用“malloc”和“calloc”命令。

有一部分我坚持,显示历史的部分。它适用于第一次执行,但随着代码的进行发出调试错误。有没有人可以帮我解决这个问题?

c ++代码:

 #include <iostream>
using namespace std;
//structure for the history array
struct Operation{
float result;
int choice;
};


float add(float, float);
float sub(float, float);
float mult(float, float);
float divi(float, float);



int main(){
//size1=for 1st history array, size2=for 2nd history array, t=to check if an operation was done at least once
int choice,size1=0,t=0,i,size2=0;
//pointer to func
int(*operation)(int, int);

float  a, b,result;
//pointers set to NULL
Operation *history1 = NULL, *history1i = NULL, *history2 = NULL, *history2i = NULL;

cout << "Enter 2 numbers a & b: ";
cin >> a >> b;
cout << "What would you like to do?" << endl << "1=Addition" << endl << "2=Subtraction" << endl << "3=Division" << endl;
cout << "4=Multiplication" << endl << "5=Display History" << endl << "6=Exit" << endl;

do{
    cin >> choice;
    //initialize the size of 1st array if a choice is valid
    if (choice < 5 && choice >0)
        size1++;
    //initialize 1st array
    history1 = (Operation*)calloc(size1, sizeof(Operation));
    if (history1 == NULL){
        cout << "History allocation failed."<<endl;
        exit(0);
    }
    //menu
    switch (choice){
    case 1: result = (add)(a, b);
        cout << endl << "The addition of " << a << " & " << b << " gives: " << result << endl;
        break;
    case 2:result = (sub)(a, b);
        cout << endl << "The subtraction of " << a << " & " << b << " gives: " << result << endl;
        break;
    case 3:if (b != 0){
                result = (divi)(a, b);
                cout << endl << "The division of " << a << " & " << b << " gives: " << result << endl;
            }
           else
               cout << "Can't perform the divison.";
        break;
    case 4:result = (mult)(a, b);
        cout << endl << "The multiplication of " << a << " & " << b << " gives: " << result << endl;
        break;
    //display history
    case 5:if (t == 1){
                cout << endl << "Your history is: " << endl;
                for (history2i = history2,i=1; (history2i - history2) < size2; history2i++,i++){
                    cout << "\t"<<i<<"-  " << a;

                    if (history2i->choice == 1)
                        cout << " + ";
                    else if (history2i->choice == 2)
                        cout << " - ";
                    else if (history2i->choice == 3)
                        cout << " / ";
                    else
                        cout << " * ";

                    cout << b << " = " << history2i->result<<endl;
                }
            }
           else
               cout <<endl<< "You haven't done any calcualtions yet."<<endl;
        break;
    case 6:break;
    default:cout << endl << "The option you entered is not valid." << endl;
        break;
    }
    //if choice is valid
    if (choice < 5 && choice >0){
        //if at least one opertion executed copy the elements of history2 in history1
        if (t == 1){
            for (history1i = history1, history2i = history2; (history1i - history1) < size2; history1i++, history2i++){
                history1i->result = history2i->result;
                history1i->choice = history2i->choice;
            }
            history1i++;//in order to enter the new operation
        }
        //if no operation done
        else{
            history1i = history1;
        }
        history1i->result = result;
        history1i->choice = choice;
    }
    //if at least one operation done=>history exists and not NULL
    if (t == 1){
        free(history2);//in order to create a new array with the new elements
        history2 = NULL;
    }
    //if not operations done before increment t
    if (choice < 5 && choice >0 && t == 0){
        t++;
    }
    //if valid choice create history 2
    if (choice < 5 && choice >0){
        size2 = size1;
        history2 = (Operation*)calloc(size2, sizeof(Operation));
        if (history2 == NULL){
            cout << "History allocation failed." << endl;
            exit(0);
        }
        //store all the previous history including the new one
        for (history2i = history2, history1i = history1; (history2i - history2) < size2; history2i++, history1i++){
            history2i->result = history1i->result;
            history2i->choice = history1i->choice;
        }
    }

    free(history1);
    history1 = NULL;

    if (choice != 6)
        cout << endl << "Choose another option: ";


} while (choice != 6);
return 0;
}

float add(float a, float b){
    return a + b;
}
float sub(float a, float b){
    return a - b;
}
float mult(float a, float b){
    return a*b;
}
float divi(float a, float b){
    return a / b;
}

0 个答案:

没有答案