Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我找不到内存泄漏_C++_Oop_Memory Leaks_Constructor_Copy Constructor - Fatal编程技术网

C++ 我找不到内存泄漏

C++ 我找不到内存泄漏,c++,oop,memory-leaks,constructor,copy-constructor,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;

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

#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;
    */
}

在这段代码中,函数的第一行是执行delete而不是delete[]。这至少是一个内存泄漏

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[]匹配。

很可能,但我不能确定,因为您没有.char*nume;-请停止并使用std::string。int*Locori;-请停止并使用std::vector locnum;。问题会神奇地消失。但我的老师告诉我要用这个…@CatalinChirea-你的老师需要一个老师。虽然这是未定义的行为,但通常不会产生内存泄漏。数组元素有一个普通的d'tor字符类型,因此不运行d'tor与运行它们具有相同的效果。