Bad alloc problem

Hi guys I took a break from coding for about a month and already feeling the effects

I seem to be getting a bad alloc() exception but I can't seem to figure out where and why I am getting it thanks

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  #include <iostream>
#include <vector>

using namespace std;


class Seat{

public:
    int number;
    char letter;

    Seat(int n,char l){

       number = n;
       letter = l;

    }
    Seat(){

    }

    Seat(const Seat& other){

        number = other.number;
        letter = other.letter;

    }

    Seat& operator=(const Seat& other){

        number = other.number;
        letter = other.letter;
        return *this;
    }
};


class Airline{

  public:

    string name;
    vector<Seat> seats;
    int row;
    char letter;

    Airline(string n,int r,char l){

     name = n;
     row = r;
     letter = l;

     for(int i = 0; i < row; row++){

        for(char j = 'A'; j <= letter; j++){

            Seat seat(i,j);
            seats.push_back(seat);
        }
     }
    }

    void printSeats(){

       for(int i = 0; i < row; row++){

            cout << endl;

        for(int j = 'A'; j <= letter; letter++){

            cout << i << j << " ";

        }
       }
    }
};

int main()
{

    Airline delta("Delta",60,'F');

}
Last edited on
This is an infinite loop of you using push_back an infinite amount of times. That's probably causing some problems.
1
2
3
4
5
6
7
8
     for(int i = 0; i < row; row++){

        for(char j = 'A'; j <= letter; j++){

            Seat seat(i,j);
            seats.push_back(seat);
        }
     }

i is never being incremented. You're accidentally incrementing row.

You have similar infinite loops in printSeats().
Last edited on
wow nice spot Ganado

thanks
just a follow up

When I print the seats I correct output happens but with 6 trailing zeros

I'm not sure why these zeros appear at the end,it seems to be a problem with my print seats function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125


#include <iostream>
#include <vector>

using namespace std;


class Seat{

public:
    int number;
    char letter;
    bool taken;

    Seat(int n,char l){

       number = n;
       letter = l;
       taken = false;

    }
    Seat(){

       taken = false;
    }

    Seat(const Seat& other){

        number = other.number;
        letter = other.letter;
        taken = false;

    }

    Seat& operator=(const Seat& other){

        number = other.number;
        letter = other.letter;
        taken = false;
        return *this;
    }

    bool operator<(const Seat& other)const{

        return number < other.number;

    }

};


class Airline{

  public:

    string name;
    vector<Seat> seats;
    int row;
    char letter;

    Airline(string n,int r,char l){

     name = n;
     row = r;
     letter = l;

     for(int i = 1; i < row; i++){

        for(char j = 'A'; j <= letter; j++){

            Seat seat(i,j);
            seats.push_back(seat);
        }
     }
    }

    void printSeats(){

       int use = (row*6)+1;

       for(int i = 0; i < use; i++){

        char let = seats[i].letter;

        cout << seats[i].number << seats[i].letter << " ";

        if(let == 'F'){

            cout << endl;
        }
       }

    }

    bool reserveSeat(int r,char l){


       for(int i = 1; i < row * 6; i++){


            if((seats[i].number == r && seats[i].letter == l) && (!seats[i].taken) ){

                cout << "reserved" << endl;

                return true;

            }
        }

      return false;
    }


};

int main()
{

    Airline delta("Delta",60,'F');
    delta.printSeats();


}
Hmm, well, in printSeats, your use variable is assigned the value of 361, (60*6+1).
But seats.size() is only 354. Seems like you have two fencepost errors: the first being that seats.size() is 354 instead of 360, and the second being that use is being assigned 361 instead of 360 (drop the +1?).

On line 68 (and line 99), you start your for loop at i=1 instead of i=0. This is causing one less iteration of the inner loop, which is equivalent to 6 push_back's less than you probably mean to happen.


Your use of 6 while at the same time iterating from 'A' to 'F' seems very magic-numbery to me. I would have the for loops be more like:
1
2
3
4
5
for (int j = 0; j < Num_Seats_Per_Row; j++)
{
    char seat_letter = j + 'A'; // 0 -> A, 1 -> B, ..., 5 -> F
    // ...
}
Last edited on
thanks Ganado

yeah my math was way off

also very good point I probably should have just used the size() function to get the size instead of getting the size myself

and the solution looks clearer to read :)

thanks
Last edited on
Topic archived. No new replies allowed.