简体   繁体   中英

C++ Initialize array in constructor EXC_BAD_ACCESS

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

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 {
            ...
        }
      }
    }

};

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.

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] .

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).

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.

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