c++ segmentation fault for dynamic arrays

I want to add a theater object into a boxoffice object in a C++ code. When I try to add it in main code, first one is added successfully. But a segmentation fault occurs for second and obvioulsy other theater objects. Here is the add function;

#include <iostream>
#include <string>
#include "BoxOffice.h"

using namespace std;

BoxOffice::BoxOffice()
{
    sizeReserv = 0;
    sizeTheater = 0;
    theaters = new Theater[sizeTheater];
    reserv = new Reservation[sizeReserv];
}

BoxOffice::~BoxOffice(){}

void BoxOffice::addTheater(int theaterId, string movieName, int numRows, int numSeatsPerRow){

    bool theaterExist = false;

    for(int i=0; i<sizeTheater; i++)
    {
        if(theaters[i].id == theaterId)
        {
            theaterExist=true;
        }
    }

    if(theaterExist)
        cout<<"Theater "<<theaterId<<"("<<movieName<<") already exists"<< endl;

    else
    {
        ++sizeTheater;

        Theater *tempTheater = new Theater[sizeTheater];


        if((sizeTheater > 1)){
            tempTheater = theaters;
        }

        tempTheater[sizeTheater-1] = Theater(theaterId,movieName,numRows,numSeatsPerRow);
        delete[] theaters;
        theaters = tempTheater;

        cout<<"Theater "<<theaterId<<"("<<movieName<<") has been added"<< endl;
        cout<<endl;

        delete[] tempTheater;
    }
}

And I get segmentation fault on this line;

tempTheater[sizeTheater-1] = Theater(theaterId,movieName,numRows,numSeatsPerRow);

This is Theater cpp;

#include "Theater.h"
using namespace std;

    Theater::Theater(){
        id=0;
        movieName="";
        numRows=0;
        numSeatsPerRow=0;
    }

    Theater::Theater(int TheaterId, string TheaterMovieName, int TheaterNumOfRows, int TheaterNumSeatsPerRow)
    {
        id = TheaterId;
        movieName = TheaterMovieName;
        numRows = TheaterNumOfRows;
        numSeatsPerRow = TheaterNumSeatsPerRow;

        theaterArray = new int*[TheaterNumOfRows];
        for(int i=0;i<TheaterNumOfRows;i++)
            theaterArray[i]= new int[TheaterNumSeatsPerRow];

        for(int i=0; i<TheaterNumOfRows;i++){
            for(int j=0;j<TheaterNumSeatsPerRow;j++){
                theaterArray[i][j]=0;
            }
        }
    }
This is header file of Theater;

#include <iostream>
#include <string>
using namespace std;

class Theater{

    public:
        int id;
        string movieName;
        int numRows;
        int numSeatsPerRow;
        int **theaterArray;

        Theater();

        Theater(int TheaterId, string TheaterMovieName, int TheaterNumOfRows, int TheaterNumSeatsPerRow);

};

And this is how i call add functions;

BoxOffice R;
R.addTheater(10425, "Ted", 4, 3);
R.addTheater(8234, "Cloud Atlas", 8, 3);
R.addTheater(9176, "Hope Springs",6,2);

Upvotes: 0

Views: 521

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

The problematic lines are these:

    if((sizeTheater > 1)){
        tempTheater = theaters;
    }

First you allocate memory and assign it to tempTheater, but here you overwrite that pointer so it will point to the old memory. It does not copy the memory. Since the code is for a homework assignment, I'll leave it up to you how to copy the data, but I do hope you follow the rule of three for the Theater class (as for the BoxOffice class) which will make it very simple.

Also, there's no need to allocate a zero-size "array", just make the pointers be nullptr (or 0).

Upvotes: 2

Related Questions