我找不到内存泄漏

时间:2017-06-12 20:15:42

标签: c++ oop memory-leaks constructor copy-constructor

你能告诉我内存泄漏在哪里,并解释我为什么做错了吗?我找不到问题,但我没有在谷歌找到答案。

#include <iostream>
using namespace std;


class Avion{

private:

    static int autoincrementare;
    char* nume;
    int randuri;
    int* locuri ;
    const double pret = 100.00;
    int nrPersonal;
    bool servire;

public:

    // setteri si getteri


    int getRanduri(){
        return this->randuri;
    }

    int getNrPersonal(){

        return this->nrPersonal;

    }

    bool getServire(){
        return this->servire;
    }


    char* getNume(){
        return this->nume;
    }


    int* getLocuri(){
        return this->locuri;
    }


    void setNume(char* nume){

        if (nume != NULL) delete this->nume;
        this->nume = new char[strlen(nume) + 1];
        strcpy(this->nume, nume);


    }


    void setLocuri(int* locuri){

        if (randuri != NULL) {

            this->locuri = new int[this->randuri];

            for (int i = 0; i < randuri; i++)
                this->locuri[i] = locuri[i];
        }
    }

    void setRanduri(int randuri){
        this->randuri = randuri;
    }

    void setNrPersonal(int nrPersonal){
        this->nrPersonal = nrPersonal;
    }

    void setServire(bool servire){
        this->servire = servire;
    }



    //constructor fara parametrii


    Avion() :pret(autoincrementare++){

        this->randuri = 3;
        this->servire = true;
        this->nrPersonal = 10;
        this->nume = new char[strlen("Luft") + 1];
        strcpy(this->nume, "Luft");

        this->locuri = new int[this->randuri];
        for (int i = 0; i < this->randuri; i++){
            this->locuri[0] = 30;
            this->locuri[1] = 40;
            this->locuri[2] = 50;
        }
    }



    Avion(int randuri, bool servire, int nrPersonal, char* nume, int* locuri) :pret(autoincrementare++){

        this->randuri = randuri;
        this->servire = servire;
        this->nrPersonal = nrPersonal;

        //if (nume != NULL) delete this->nume;
        this->nume = new char[strlen(nume) + 1];
        strcpy(this->nume, nume);


    //  if (locuri != NULL)delete this->locuri;
        this->locuri = new int(this->randuri);
        for (int i = 0; i < randuri; i++)
        {
            this->locuri[i] = locuri[i];
        }


    }




    friend ostream & operator<<(ostream & out, Avion & a){


        out << "Avionul are " << a.randuri << " randuri" << endl;
        out << "Avionul are deschis bufetul : " << a.servire << endl;
        out << "Avionul are numarul de personal de: " << a.nrPersonal << endl;

        out << "Avionul are numele : " << a.nume << endl;


        out << " Avionul are: " << a.randuri << " randuri cu " << endl;
        for (int i = 0; i < a.getRanduri(); i++){
            cout << a.getLocuri()[i] << " locuri " << endl;
        }

        return out;

    }


    friend istream & operator >>(istream & in, Avion &a){
        char aux[50];
        cout << "Nume avion : "; in >> aux;
        if (a.nume != NULL) delete[] a.nume;
        a.nume = new char[strlen(aux) + 1];
        strcpy(a.nume, aux);
        return in;
    }



    Avion& operator=(const Avion& a){

        this->randuri = a.randuri;
        this->servire = a.servire;
        this->nrPersonal = a.nrPersonal;


        this->nume = new char[strlen(a.nume) + 1];
        strcpy(this->nume, a.nume);


        this->locuri = new int(this->randuri);
        for (int i = 0; i < randuri; i++)
        {
            this->locuri[i] = a.locuri[i];

        }

        return *this;


    }




    ~Avion(){
        if (nume != NULL) delete[] this->nume;
        if (locuri != NULL) delete[] this->locuri;
    }




};

int Avion::autoincrementare = 1;

void main(){



    Avion Luft;
    cin >> Luft;
    cout << Luft << endl;




    cout << "================================"<<endl;
    cout << "==========================" << endl;


    int a[3]{10, 20, 30};
    Avion BlueAir(3, true, 10, "Blue Air", a);

    cout << BlueAir << endl;



    /*
    Avion G6;

    G6 = Luft;

    cout << G6 << endl;
    cout << "==================";

    cout << Luft << endl;
    */
}

1 个答案:

答案 0 :(得分:1)

在此代码中,函数的第一行是删除而不是删除[]。因此,至少有一次内存泄漏。

void setNume(char* nume){

    if (nume != NULL) delete this->nume;
    this->nume = new char[strlen(nume) + 1];
    strcpy(this->nume, nume);


}

你必须将new与delete和new []与delete []匹配。