我已经坐了几个小时了,这真让我发疯。我也是C ++的新手,所以如果除了内存泄漏之外还有其他错误,我想知道! :D

问题是,如果我创建一个新的老师/助理/ TA,并进行打印,则会得到:

{212} normal block at 0x005694B0, 8 bytes long.
 Data: <  V     > BC 91 56 00 00 00 00 00
{210} normal block at 0x005691B8, 40 bytes long.
 Data: <      V Oliver  > BC A4 16 00 B0 94 56 00 4F 6C 69 76 65 72 00 CD
{209} normal block at 0x00568F30, 8 bytes long.
 Data: <  V     > 90 8E 56 00 00 00 00 00
{208} normal block at 0x00569468, 8 bytes long.
 Data: <d V     > 64 8E 56 00 00 00 00 00
{204} normal block at 0x00568E60, 76 bytes long.
 Data: <    h V elev    > D4 A6 16 00 68 94 56 00 65 6C 65 76 00 CD CD CD


如果我不断重复说明,我将不胜感激! :D

    int main(){



    int choise, count=0;
    bool ta = false , teacher = false, assistant = false;
    choise = meny();

    Employee **work = NULL;
    Employee **swapper = NULL;
    Employment *tmp = NULL;

    string name, types, subject, temp, type;
    int birthyear, salary, workhours, points;
    bool boss = false, ansvarig, key=false, cert;

    while (choise != NULL){
        if (choise > 0){

            if (choise == 1){
                fflush(stdin);
                cout << endl << "Namn: ";
                getline(cin, name);
                cout << endl << "Fodelsear: ";
                cin >> birthyear;
                cin.ignore();
                cout << "\nTjänst: ";
                getline(cin, type);
                cout << "\nLon: ";
                cin >> salary;
                cin.ignore();
                cout << "\nChef: ";
                cin >> temp;
                cin.ignore();
                if (temp == "Ja" || temp == "ja"){
                    boss = true;
                }
                else
                    boss = false;
                cout << "\nTjanstgoringstid: ";
                cin >> workhours;
                cin.ignore();
                cout << "\nHuvudämne: ";
                cin >> subject;
                cin.ignore();
                cout << "\nProgramansvarig: ";
                cin >> temp;
                cout << "\n\n\n";
                cin.ignore();
                if (temp == "Ja" || temp == "ja")
                    ansvarig = true;
                else
                    ansvarig = false;



                count++;
                teacher = true;

            }
            else if (choise == 2){
                fflush(stdin);
                cout << endl << "Namn: ";
                getline(cin, name);

                cout << endl << "Fodelsear: ";
                cin >> birthyear;
                fflush(stdin);

                cout << "\nTjänst: ";
                getline(cin, type);

                cout << "\nLon: ";
                cin >> salary;
                fflush(stdin);

                cout << "\nHuvudämne: ";
                cin >> subject;
                fflush(stdin);

                cout << "Avklarade poang: ";
                cin >> points;
                fflush(stdin);
                count++;

                assistant = true;
            }
            else if (choise == 3){

                fflush(stdin);

                cout << endl << "Namn: ";
                getline(cin, name);

                cout << endl << "Fodelsear: ";
                cin >> birthyear;
                cin.ignore();

                cout << "\nTjänst: ";
                getline(cin, type);


                cout << "\nLon: ";
                cin >> salary;
                cin.ignore();

                cout << "\nTjanstgoringstid: ";
                cin >> workhours;
                cin.ignore();

                cout << "\nAttestratt: ";
                cin >> temp;
                cin.ignore();
                if (temp == "Ja" || temp == "ja"){
                    cert = true;
                }
                else
                    cert = false;

                cout << "\nHar huvudnyckel: ";
                cin >> temp;
                cin.ignore();
                if (temp == "Ja" || temp == "ja"){
                    cert = true;
                }
                else
                    cert = false;


                cout << "\n\n\n";




                count++;
                ta = true;
            }

            else if (choise == 4){
                for (int x = 0; x < (count); x++){
                    cout << work[x]->toString();
                }
            }
            if (choise != 0 && choise!=4){
                swapper = new Employee*[count];

                if (teacher == true){
                    tmp = new Teacher(type, boss, salary, workhours, subject, ansvarig);

                    swapper[count - 1] = new Employee(name, birthyear, tmp);

                    teacher = false;

                    //delete tmp;
                }
                else if (ta == true){
                    tmp = new TA(type, boss, salary, workhours, cert, key);

                    swapper[count - 1] = new Employee(name, birthyear, tmp);

                    assistant = false;

                //  delete tmp;
                }
                else if (assistant == true){
                    tmp = new Assistant(type, salary, subject, points);


                    swapper[count - 1] = new Employee(name, birthyear, tmp);

                //  delete tmp;


                }



                    for (int x = 0; x < count - 1; x++){
                    swapper[x] = work[x];
                    }



                delete[] work;



                work = swapper;



            }

                choise = meny();




        }
    }

    /*for (int x = 0; x < count; x++){
        delete work[x];
        }*/

    delete [] work;



    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    _CrtDumpMemoryLeaks();

    return 0;

}

最佳答案

您永远不会delete数组。无论如何,您都应该使用向量。

关于c++ - 需要帮助寻找内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22671956/

10-13 05:30