0
Cars::Cars( long id, int audienceRadius, const int numOfRow, const int numOfColumn) {
    
    radius = audienceRadius;
    row = numOfRow;
    column = numOfColumn;
    movieID = id;
    

    arr1 = new int*[row];
    for ( int i = 0; i < row; i++) {
        arr1[i] = new int[column];
    }

    for (int j = 0; j < row; j++)
        for (int k = 0; k < column; k++)
            arr1[j][k] = 0;
}
Cars::~Cars() {
    for(int i = 0; i < row; i++) {
        delete [] arr1[i];
    }
    delete [] arr1;
}

2d dynamic array for parking area, and i am using this class on my other class where it stores those parking areas in class

AllParking::AllParking(){
    //all park is also dynamic array
    allparks = NULL;
    numberOfParking= 0;
}
//can we delete like this?
AllParking::~AllParking(){
    if(numberOfParking> 0)
        delete[] allparks ;
}

My question is this cause any memory leak or error? When i am deleting allparks should i also delete member of arrays also or other destructor deletes that?

  • Your question is unclear. What is the type of allparks? If it's Cars type, you will call the destructor of Cars class with your delete. You should use vector by the way. – Kafka Mar 17 '21 at 10:26
  • 2
    leaking or not is not just a matter of writing a destructor. One needs to consider the class a whole or at least copy constructor and assignment operator. See [rule of 3/5](https://en.cppreference.com/w/cpp/language/rule_of_three) – 463035818_is_not_an_ai Mar 17 '21 at 10:26
  • 2
    this might answer part of your question https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three. And using a vector instead of manual memory managment will make your question obsolete ;) – 463035818_is_not_an_ai Mar 17 '21 at 10:28

0 Answers0