-8

Could you please tell me where is the memory leak and explain why i did wrong? I can't find the problem and i don't find the answer on google.

#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;
    */
}
Fred Larson
  • 58,972
  • 15
  • 110
  • 164

1 Answers1

1

In this code, the first line of the function is doing a delete instead of a delete[]. So that's at least one memory leak.

void setNume(char* nume){

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


}

You have to match new with delete and new[] with delete[].

Anon Mail
  • 4,575
  • 1
  • 17
  • 20
  • While being undefined behavior, this does not in general produce a memory leak. The array elements have a trivial d'tor (`char` type), so not running the d'tors has the same effect as running them. – IInspectable Jun 12 '17 at 20:32