title: Dynamic Allocation of Arrays
body: <p>I'm trying to write a simple flight reservation system for my school assignment. I should dynamically create an array without determining a size. Since I have to keep track of the size of the array, I declared an integer variable named as count in my class. I also have a flight class that has a copy constructor and a couple of getters. Then I wrote the following method </p>
  
  <pre><code>void ReservationSystem::addFlight(const int flightNo, const int rowNo, const int seatNo) {
      if (count == 0) {
          Flight *tmp = new Flight(flightNo, rowNo, seatNo);
          listOfFlights = new Flight*[count+1];
          listOfFlights[count] = tmp;
          count++;
      } else {
          bool check = true;
          for (int i = 0; i &lt; count &amp;&amp; check; i++) {
              if (listOfFlights[i]-&gt;getFlightNo() == flightNo) {
                  std::cout &lt;&lt; "There is already a flight with that flight code" &lt;&lt; std::endl;
                  check = false;
              }
          }
  
          if (check) {
              Flight *tmp = new Flight(flightNo, rowNo, seatNo);
              Flight** tmparr = new Flight*[count + 1];
  
              for (int i = 0; i &lt; count; i++) {
                  Flight *f = new Flight(*listOfFlights[i]);
                  tmparr[i] = f;
              }
  
              tmparr[count + 1] = tmp;
  
              for (int i = 0; i &lt; count; i++) {
                  delete listOfFlights[i];
              }
  
              delete listOfFlights;
              listOfFlights = tmparr;
              count++;
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>I also have a <code>showFlight(const int flightCode)</code> method that shows the specific flight:</p>
  
  <pre><code>void ReservationSystem::showFlight(const int flightNo) {
      bool check = true;
      for (int i = 0; i &lt; count; i++) {
          if (listOfFlights[i]-&gt;getFlightNo() == flightNo) {
              std::cout &lt;&lt; "Flight " &lt;&lt; listOfFlights[i]-&gt;getFlightNo() &lt;&lt; " has " &lt;&lt; listOfFlights[i]-&gt;getAvailableSeats()  &lt;&lt; " available seats" &lt;&lt; std::endl;
              listOfFlights[i]-&gt;printSeats();
              check = false;
          }
      }
  }
  </code></pre>
  
  <p>This is my default constructor and copy constructor for the <code>Flight</code> class:</p>
  
  <pre><code>Flight::Flight(const int flightNo, const int rowNo, const int seatNo) {
      flight = flightNo;
      row = rowNo;
      seat = seatNo;
      available = rowNo * seatNo;
      flightPlan = new char*[seatNo];
  
      // initialize the flight plan to all seats available
      for(int i = 0; i &lt; seatNo; ++i) flightPlan[i] = new char[rowNo];
  
      for(int i = 0; i &lt; seatNo; ++i) {
          for(int j = 0; j &lt; rowNo; ++j) flightPlan[i][j] = 'o';
      }
  }
  
  Flight::Flight(const Flight &amp;obj) {
      const int flight = obj.flight;
      const int row = obj.row;
      const int available = obj.available;
      char** flightPlan = obj.flightPlan;
  
  }
  </code></pre>
  
  <p>But in the line <code>if (listOfFlights[i]-&gt;getFlightNo() == flightNo)</code> xcode gives me EXC_BAD_ACCESS error. I think the reason behind this is a malfunction in my <code>addFlight()</code> method, because since there is no objects the array points to something null, right? And since it can't reach to <code>getFlightNo()</code> method, it throws this error.</p>
  
  <p>Notice that this is my first time with C++, so I'm a complete n00b and I can be terribly mistaken. Any help would be greatly appreciated.</p>