1

I'm creating a simple constructor and initializing an array:

// Construtor
Cinema::Cinema(){
    // Initalize reservations
    for(int i = 0; i < 18; i++){
        for(int j = 0; j < 12; j++){
            setReservation(i, j, 0);
        }
    }

    // Set default name
    setMovieName("N/A");

    // Set default price
    setPrice(8);
}

The setReservation function:

void Cinema::setReservation(int row, int column, int reservation){
    this->reservations[row][column] = reservation;
}

The setMovieName function:

void Cinema::setMovieName(std::string movieName){
    this->movieName = movieName;
}

For some odd reason when I run the program, the setMovieName function gives the following error: "Program Received Signal: EXC_BAD_ACCESS"

If I take out the for-loop that initializes the array of reservations, the problem goes away and the movie name is set without any problems. Any idea what I'm doing wrong?

This is the Cinema.h file:

#ifndef Cinema_h
#define Cinema_h

class Cinema{

private:
    int reservations[17][11];
    std::string movieName;
    float price;
public:
    // Construtor
    Cinema();

    // getters/setters
    int getReservation(int row, int column);
    int getNumReservations();
    std::string getMovieName();
    float getPrice();

    void setReservation(int row, int column, int reservation);
    void setMovieName(std::string movieName);
    void setPrice(float price);
};

#endif
4
  • 2
    How is the reservations array declared? It is not uncommon to mix up rows and columns. Commented Nov 21, 2011 at 17:44
  • Any reason why you have to use a manual loop at all? Any self-respecting container would have a constructor that zeros its elements. Commented Nov 21, 2011 at 17:48
  • Thanks for the quick reply. Just added the .h file above @UncleBens Commented Nov 21, 2011 at 17:49
  • 1
    Scrap the loop and value-initialize the array in the base initalizer list: Cinema::Cinema() : reservations() { } Commented Nov 21, 2011 at 17:52

5 Answers 5

2

If there's supposed to be 18 rows and 12 columns, that's exactly how you need to dimension your array:

int reservations[18][12];

Also better use static constants for those, instead of "magic numbers". In addition, rows and columns are easy to confuse, so a better idea is to give more descriptive names to iterating variables i and j.

class Cinema
{
    static const int row_count = 18;
    static const int column_count = 12;

    int reservations[row_count][column_count];

    //looping
    Cinema() {
      for (int row = 0; row < row_count; ++row) {
        for (int column = 0; column < column_count; ++column {
            ...
        }
      }
    }

};
Sign up to request clarification or add additional context in comments.

Comments

1

Did you init this->reservations somewhere or is it static? Also, are the dimensions correct? Would be important to see its definition. Otherwise that might be the reason. If that doesn't solve your issue, set a break point and then walk the code step by step, to see the line where it fails.

Comments

1

Are you actually allocating space for reservations?

If it's fixed size, are you declaring it as int[18][12]?

If not, please don't use int**. This is c++, you can use

std::vector<std::vector<int>>

EDIT: There's your problem:

int reservations[17][11];

This has 17/11 dimensions, you're iterating 18/12. Use int[18][12].

1 Comment

Thanks for the reply. The for-loop iterates as long as i < 18 and j < 12 so it should be fine
1

Your reservations array is too small. You should initialise it with the number of rows/columns (ie. 18 and 12), not the highest index. When you initialise the reservations it will run off the end of the array and corrupt movieName, after which anything could happen when you try to access it.

Also, you may know this already, but you don't need to always prefix member variable access with this-> in C++. That's implied unless you have a local variable with the same name (as in your setMovieName function).

Comments

1

You declared an array int reservations[17][11];, but your constructor is accessing [0 to 17][0 to 11], which is beyond the valid ranges [0 to 16][0 to 10].

You should prefer a std::vector<std::vector<int>> over that array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.