title: c++ double free or corruption error at end of scope
body: <p>I am a novice C++ programmer who is currently a student at my local University. I am doing this assignment and I am told to create a linked list of a class called Car, to create a "train". I have two derived classes which simply print differently. </p>
  
  <p>What I want to do is create a list of train cars, and have two pointers pointing to the current Car of each type (Chair and Sleeping) so I know when the user wants to book a seat of a certain type within the train, I know exactly where to go there. When a train is full, the plan is to put a new train of that type onto the end. Ex: Sleeping Car's last seat is booked, a new Sleeping Car will be created and put at the end of the train. The current Sleeping Car pointer will now point to the new Car at the end of the list. </p>
  
  <p>Below is the code I have for this program:</p>
  
  <p><strong>carTest.cc</strong></p>
  
  <pre><code>int main()
  {
    int trainId = 0;
    list&lt;Car&gt; train;
  
    // Testing Car Default Constructor
  
    // Testing Car Constructor
    //cout &lt;&lt; "Creating a \"Chair car\"... " &lt;&lt; endl;
  
    Car* currChair = nullptr, *currSleep = nullptr;
    train.push_back(ChairCar(trainId++));
    currChair = &amp;train.back();
    train.push_back(SleepingCar(trainId++));
    currSleep = &amp;train.back();
  
    // Testing bookSeat
    //book_seat_test(car2);
  
    // Testing isTypeAvailable
    //is_type_available_test(car2);
  
    // Testing isBooked
    //is_booked_test(car2);
  
    // Testing mostAvailable
    //most_available_test(car2);
  
    // Testing firstAvailable
    //first_available_test(car2);
  
    // Testing getSeatNumber
    ///get_seat_number_test(car2);
  
    // Testing getIndex
    //get_index_test(car2);
  
    // Testing bookFirstAvailable
    //book_first_available_test(car2);
  
    // Testing chairCar printStatus
    //chairCar_print_status_test(*currChair);
  
    // Testing sleepingCar printStatus
    //sleepingCar_print_status_test(*currSleep);
  
    currSleep = nullptr;
    currChair = nullptr;
    return 0;
  }
  </code></pre>
  
  <p><strong>car.h</strong></p>
  
  <pre><code>class Car
  {
   public:
    class Seat
    {
    public:
    Seat() : row(-1), col(-1) {};
    Seat(int i, int j) : row(i), col(j) {};
      int getRow() const { return row; }
      int getCol() const { return col; }
  
    private:
      int row;
      int col;
    };
  
   public:
    // Contructors
    Car();
    explicit Car(int, int, int);
  
    // Deconstructor
    ~Car();
  
    // Public Member Functions
    bool bookSeat(int, int);
    bool isTypeAvailable(int) const;
    bool isBooked() const;
    int mostAvailable() const;
    int firstAvailable(int) const;
    int getSeatNumber(int, int) const;
    Seat getIndex(int) const;
    int bookFirstAvailable(int);
    virtual void printStatus(int) const;
  
   protected:
    int carNumber;
    int rows;
    int cols;
    bool** plan;
    int* numAvailable;
    int columnSeperation;
  
    void init();
  };
  </code></pre>
  
  <p><strong>car.cc</strong></p>
  
  <pre><code>// function: Deconstructor
  // Description:
  //             The Deconstructor simply frees the dynamically allocated memory
  // of the data members plan and numAvailable if the members are not pointing 
  // to the nullptr.
  Car::~Car()
  {
    if(plan != nullptr)
      {
        delete[] plan;
        plan = nullptr;
      }
  
    if(numAvailable != nullptr)
      {
        delete[] numAvailable;
        numAvailable = nullptr;
      }
  }
  </code></pre>
  
  <p><strong>chairCar.h</strong></p>
  
  <pre><code>class ChairCar : public Car
  {
   public:
    ChairCar();
    ChairCar(int);
   protected:
    void printStatus(int seatNumber) const;
  };
  
  #endif
  </code></pre>
  
  <p><strong>chairCar.cc</strong></p>
  
  <pre><code>#include "chairCar.h"
  
  ChairCar::ChairCar() 
    : Car() 
  {
  
  }
  
  ChairCar::ChairCar(int id) 
    : Car(7,4,id)
  {
  
  }
  
  void ChairCar::printStatus(int seatNumber) const
  {
    int maxSeatNumber = (rows-1)*cols+(cols-1);
    if(seatNumber &gt; maxSeatNumber || seatNumber &lt; 0)
      throw OutOfRangeException(seatNumber, 0, maxSeatNumber);
  
    cout &lt;&lt; setw(columnSeperation) &lt;&lt; ' ' &lt;&lt; " |";
    cout &lt;&lt; setw(columnSeperation) &lt;&lt; "WL";
    cout &lt;&lt; setw(columnSeperation) &lt;&lt; "ML";
    cout &lt;&lt; setw(columnSeperation) &lt;&lt; "MR";
    cout &lt;&lt; setw(columnSeperation) &lt;&lt; "WR";
    cout &lt;&lt; endl;
    Car::printStatus(seatNumber);
  }
  </code></pre>
  
  <p>Sleeping Car is exactly the same as Chair Car except different arguments are sent to the base constructor. When I run the code in the main function I get </p>
  
  <blockquote>
    <p>Error in
    `/home/w1llbedone/Programming/cpsc2620/assn/a4/carTest': double free or corruption (fasttop): 0x0000000000608010</p>
  </blockquote>
  
  <p>I was trying to set the <code>currCar</code> pointers to <code>nullptr</code> before end of the scope to avoid this error. Can anyone explain to me why this happens? This is my first time posting on StackExchange, so I apologise for any lack of information. Any help would be greatly appreciated. </p>