0

I made a Flight class which holds flightNo, rowNo, seatNo and seating plan of this flight. I am creating the seating plan by using 2d dynamic string array. This array will show seat and row numbers, and also the availablity of the seats by 'o' or 'x'.

For instance if rowNo = 3 and seatNo = 3, seating plan will be:

 A B C 
1o o o
2o o o
3o o o

This is Flight class

#include "Flight.h"
#include <iostream>
using namespace std;

Flight::Flight( int fNo, int rNo, int sNo ) {
    setFlightNo( fNo );
    setRowNo( rNo );
    setSeatNo( sNo );
    setAvailable( rNo * sNo );
    
    //Create 2d dynamic array for seatPlan
    seatPlan = new string * [ rNo + 1 ];
    for (int i = 0; i < rNo + 1; i++) {
        seatPlan[i] = new string[ sNo + 1 ];
    }

    //Initialize seatPlan
    initializeSeatPlan( rNo, sNo );
}

Flight::Flight() {
    setFlightNo(0);
    setRowNo(0);
    setSeatNo(0);
    setAvailable(0);

    seatPlan = new string * [ getRowNo() + 1 ];
    for (int i = 0; i < getRowNo() + 1; i++) {
        seatPlan[i] = new string[ getSeatNo() + 1 ];
    }

    //Since its size is one, just initialize seatPlan[0][0]
    seatPlan[0][0] = " ";
}

Flight::~Flight() {
    for (int k = 0; k < getRowNo() + 1; k++) {
         delete [] seatPlan[k];
     }
     delete[] seatPlan;
}

void Flight::initializeSeatPlan(int rNo, int sNo) {

    const string alphabet[26] = { "A","B","C","D","E","F","G","H","I","J",
                    "K","L","M","N","O","P","Q","R",
                    "S","T","U","V","W","X","Y","Z" };

    seatPlan[0][0] = " ";

    //Initialize first row except seatPlan[0][0]
    for (int i = 1; i < sNo + 1; i++) {
        seatPlan[0][i] = alphabet[i - 1];
    }
     
    //Initialize first indexes of each row except seatPlan[0][0]
    for (int i = 1; i < rNo + 1; i++) {
        seatPlan[i][0] = i + '0';
    }
    
    //Initialize rest of the array with 'o'
    for (int i = 1; i < rNo + 1; i++) {
        for (int j = 1; j < sNo + 1; j++) {
            seatPlan[i][j] = "o";
        }
    }
}

Then I created another class called ReservationSystem which holds a dynamic Flight array. flightAmount is the size of this array. In this class, there is an addFlight function and when I call it, my program crashes. Visual Studio says that there is a memory access violation in my Flight class destructor, but I can't find why. When I comment inside of Flight class' destructor, it works fine, but it leaks memory.

This is my ReservationSystem

#include "ReservationSystem.h"
#include <iostream>
using namespace std;

ReservationSystem::ReservationSystem() {
    flightAmount = 0;
    flights = new Flight[flightAmount];
}

ReservationSystem::~ReservationSystem() {
    delete[] flights;
}

void ReservationSystem::addFlight(const int flightNo, const int rowNo, const int seatNo) {
    if (flightNoExist(flightNo) != -1) {
        cout << "Flight " << flightNo << " already exists" << endl;
    }
    else if (rowNo <= 0) {
        cout << "Invalid Row Number " << rowNo << "!" << endl;
    }
    else if (seatNo <= 0) {
        cout << "Invalid Seat Number!" << seatNo << "!" << endl;
    }
    else {
        //To increase the size of flights array create new tmp array
        Flight* tmp = new Flight[flightAmount + 1];
        
        //Copy from flights to tmp
        for (int i = 0; i < flightAmount; i++) {
            tmp[i] = flights[i];
        }

        //Create the new flight and add it to tmp
        Flight f(flightNo, rowNo, seatNo);
        tmp[flightAmount] = f;
 
        delete[] flights;
        flights = tmp; //Flights now pointing to where tmp is pointing
        flightAmount++;

        cout << "Flight " << flightNo << " has been added" << endl;
    }
}

This is the part where Visual Studio says there is a memory access violation:

Flight::~Flight() {
    for (int k = 0; k < getRowNo() + 1; k++) {
         delete [] seatPlan[k];
     }
     delete[] seatPlan;
}

EDIT:

I overrided the = operator but error still persists.

My = operator

Flight& Flight::operator=( Flight& right ) {
    if (&right != this) {

        flightNo = right.getFlightNo();
        rowNo = right.getRowNo();
        seatNo = right.getSeatNo();
        available = right.getAvailable();

        //Delete old seatPlan
        for (int k = 0; k < rowNo + 1; k++) {
            delete[] seatPlan[k];
        }
        delete[] seatPlan;
        
        //Create new seatPlan with right's dimensions
        seatPlan = new string*[right.getRowNo() + 1];
        for (int i = 0; i < right.getRowNo() + 1; i++) {
            seatPlan[i] = new string[right.getSeatNo() + 1];
        }

        //Copy the content from right
        for (int i = 0; i < right.getRowNo() + 1; i++) {
            for (int k = 0; k < right.getSeatNo(); k++) {
                seatPlan[i][k] = right.seatPlan[i][k];
            }
        }
    }
    return* this;
}

But now Visual Studio says that memory violation occurs inside of my overrided = operator function while deleting seatPlan array. Spesifically it is again this part:

for (int k = 0; k < rowNo + 1; k++) {
    delete[] seatPlan[k];
}
delete[] seatPlan;
firemancer
  • 25
  • 7
  • 1
    Does this answer your question? [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Yksisarvinen Nov 19 '20 at 10:18
  • 2
    that address is awfully similar to "0xDDDDDDDD : Used by Microsoft's C++ debugging heap to mark freed heap memory". – Botje Nov 19 '20 at 10:20
  • 1
    You get this error, because you violated the Rule of Three. When you copy `Flight` object here: `tmp[i] = flights[i];`, you use default copy assignment operator which does shallow copy. – Yksisarvinen Nov 19 '20 at 10:21
  • 3
    I see no reason to use raw pointers here at all. Have you looked at `std::vector`? `k < getRowNo() + 1` looks suspicious btw, why `+ 1`? – Ted Lyngmo Nov 19 '20 at 10:22
  • [When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?](https://stackoverflow.com/q/370195/995714) 0xDD means dead memory – phuclv Nov 19 '20 at 16:00

0 Answers0