简体   繁体   中英

C++ dynamic memory allocation for classes

I have a problem in the second class while dynamically allocating objects from the first class . Sometimes when n < 3 or 4 the program works without problems but most of the time when n > 3 or even sometimes when its less and I run the code it crashes after the second entered object . crash-screenshot . Any help and tips on how to fix this will be appreciated.

#include <iostream>
#include <cstring>

using namespace std;
class ParkingPlac{
private:
    char adresa[20] ;
    char id[20] ;
    int zarabotka ;
    int cena ;
public:
    ParkingPlac() //first class
    {
        cout<<"Constructor "<<endl ;
    }
    ParkingPlac(char *adr , char *i , int c )
    {
        strcpy(this->adresa,adr) ;
        this-> cena = c ;
        char *id = new char [strlen(i)+1] ;
        strcpy(this->id,i) ;
        cout<<"Konstruktor"<<endl ;
        delete[] id ;
    }
    ~ParkingPlac()
    {
        cout<<"Destruktor" <<endl;
    }
    void print()
    {
        cout<<adresa << " "<<id<<" "<<cena<<endl ;
    }
    char *getid()
    {
        return id;
    }
    void setid(char *i)
    {
        strcpy(this->id,i) ;
    }
    char *getadresa()
    {
        return adresa;
    }
    int getcena()
    {
        return cena ;
    }
    void setadresa(char *a)
    {
        strcpy(this->adresa,a) ;
    }
    void setcena(int i)
    {
        this->cena = i ;
    }
};
class PretprijatieParking{
private:
    char imeprezie[35] ;
    ParkingPlac *placovi;
    int brojplacovi ;
public:
    PretprijatieParking(char *ime)
    {
        strcpy(this->imeprezie,ime) ;
        brojplacovi = 0;
    }
    void pecati()
    {
        cout<<imeprezie<<" "<<brojplacovi<<endl ;
    }
    ~PretprijatieParking()
    {
        cout<<"DestruKtor"<<endl;
        delete []placovi ;
    }
    void dodadiParkingPlac(ParkingPlac p)
    {
        int i ;
        for(i = 0 ; i<brojplacovi;i++)
        {
            if(strcmp(placovi[i].getid(), p.getid()) == 0)
                return  ;
        }
        placovi = new ParkingPlac[brojplacovi] ;
        placovi[brojplacovi].setadresa(p.getadresa()) ;
        placovi[brojplacovi].setid(p.getid());
        placovi[brojplacovi].setcena(p.getcena()) ;

        cout<<placovi[brojplacovi].getadresa()<<endl;
        cout<<placovi[brojplacovi].getid()<<endl  ;
        cout<<placovi[brojplacovi].getcena()<<endl ;
        brojplacovi+=1 ;
    }
 };

 int main(){
     PretprijatieParking gradski("Mirko Mirkovski");
     int n;
     char adresa[50],id[50];
     int brojcasovi,cenacas;
     cin>>n;
     for (int i=0;i<n;i++){
         cin.get();
         cin.getline(adresa,50);
         cin>>id>>cenacas;

         ParkingPlac edna(adresa,id,cenacas);
         gradski.dodadiParkingPlac(edna);
    }
    //plakjanje
    //cin>>n;
    //for (int i=0;i<n;i++){
    //
    //    cin>>id>>brojcasovi;
    //
    //    if(!gradski.platiParking(id,brojcasovi))
    //       cout<<"Ne e platen parking. Greshen ID."<<endl;
    //}

    cout<<"========="<<endl;
    gradski.pecati();
    return 0 ;
}

Problems I see:

1. Allocating memory for array of size 0

In the constructor of PretprijatieParking , the member variable brojplacovi is initialized to 0 . Then you go on to use:

     placovi = new ParkingPlac[brojplacovi] ;

2. Accessing memory using out of bounds indices

After you allocate memory using the above line, the highest valid index for the array is brojplacovi - 1 . However, you go on to use:

     placovi[brojplacovi].setadresa(p.getadresa()) ;

3. Leaking memory

You don't have any code to make sure that:

  1. The data stored in previously allocated memory is copied/moved to the newly allocated memory.

  2. You don't deallocate the previously allocated memory.


There might be other problems in your code. I saw the above after a quick glance. The first and the second problems are sufficient to cause undefined behavior.

Unless you are prohibited, use std::vector instead of dynamically allocated array and std::string instead of dynamically allocated string. They will take care of most memory related issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM