title: Pointer in C++ isn't initialized correctly
body: <p>I'm trying to execute the following code, everything is fine except one thing and that is tellerArray[2] is never initialized properly, it always creates problems for me, and I don't why. It creates problem for me: I came to know this fact when I debugged the code multiple times.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stddef.h&gt;
  
  using namespace std;
  
  class Customer {
  public:
      void setTime(int time) { this-&gt;_time = time; }
      int getTime() { return this-&gt;_time; }
      void setNextCustomer(Customer *next) { this-&gt;_next = next; }
      Customer* getNextCustomer() { return this-&gt;_next;}
  private:
      int _time;
      Customer *_next;
  };
  
  
  class Teller {
  public:
      Teller();
      ~Teller();
      void addCustomer(Customer *customer);
      int totalCustomers();
      int totalTime();
  
  private:
      Customer *head;
      Customer *tail;
  };
  
  Teller::Teller() {
      this-&gt;head = NULL;
      this-&gt;tail = NULL;
  }
  
  Teller::~Teller() {
      delete head;
      delete tail;
      head = NULL;
      tail = NULL;
  }
  
  void Teller::addCustomer(Customer *customer) {
      customer-&gt;setNextCustomer(NULL);
      if(head == NULL) {
          head = customer;
      } else {
          tail-&gt;setNextCustomer(customer);
      }
      tail = customer;
  }
  
  int Teller::totalTime() {
      int totalTime = 0;
      Customer *tempCust = new Customer;
      for(tempCust = head; tempCust != NULL; tempCust = tempCust-&gt;getNextCustomer()) {
          totalTime += tempCust-&gt;getTime();
      }
      return totalTime;
  }
  
  int Teller::totalCustomers() {
      int totalCustomers = 0;
      Customer *tempCust = new Customer;
      for(tempCust = head; tempCust != NULL; tempCust = tempCust-&gt;getNextCustomer()) {
          totalCustomers += 1;
      }
      return totalCustomers;
  }
  
  
  int getLeast(int, int, int, int);
  int getMax(int, int, int, int);
  
  
  int main(int argc, const char*argv[]) {
  
  
      Teller *tellerArray[4];
  
      // creating four tellers ( counters )
      Teller *tellerOne   = new Teller();
      Teller *tellerTwo   = new Teller();
      Teller *tellerThree = new Teller();
      Teller *tellerFour  = new Teller();
  
      tellerArray[0] = tellerOne;
      tellerArray[1] = tellerTwo;
      tellerArray[2] = tellerThree;
      tellerArray[3] = tellerFour;
  
      char wannaBuyAnother = 'n';
      int  duration = 0, minTime = 0, maxTime = 0, index = 0;
  
      do {
          cout&lt;&lt;"Enter duration of your transaction: ";
          cin&gt;&gt;duration;
          Customer *customer = new Customer;
          customer-&gt;setTime(duration);
          minTime = getLeast(     tellerOne-&gt;totalTime(),
                                  tellerTwo-&gt;totalTime(),
                                  tellerThree-&gt;totalTime(),
                                  tellerFour-&gt;totalTime()     );
  
          for(index = 0; index &lt; 4; index++) {
              if( (tellerArray[index]-&gt;totalTime()) == minTime ) {
                  break;
              }
          }
  
  
  
          tellerArray[index]-&gt;addCustomer(customer);
  
          cout&lt;&lt;"You can stand in Queue "&lt;&lt;index + 1&lt;&lt;"\n";
  
          cout&lt;&lt;"Do you want to buy another Ticket(Y/N)? ";
          cin&gt;&gt;wannaBuyAnother;
  
      } while ( wannaBuyAnother == 'y' || wannaBuyAnother == 'Y' );
  
  
      cout&lt;&lt;"Number of Customers Deal By Every Teller\n";
  
      for(index = 0; index &lt; 4; index++) {
          cout&lt;&lt;"T"&lt;&lt;index&lt;&lt; "= \t"&lt;&lt;tellerArray[index]-&gt;totalCustomers()&lt;&lt;"\n";
      }
  
      maxTime = getMax( tellerOne-&gt;totalTime(),
                              tellerTwo-&gt;totalTime(),
                              tellerThree-&gt;totalTime(),
                              tellerFour-&gt;totalTime()  );
      for(index = 0; index &lt; 4; index++) {
          if( (tellerArray[index]-&gt;totalTime()) == maxTime ) {
              cout&lt;&lt;"TELLER "&lt;&lt;index+1&lt;&lt;" Deal Maximum Customers of the Day\n";
              break;
          }
      }
  
      return 0;
  }
  
  
  
  
  
  int getLeast(int first, int second, int third, int fourth) {
      int min = first;
      if( second &lt; min ) {
          min = second;
      } else if ( third &lt; min ) {
          min = third;
      } else if ( fourth &lt; min ) {
          min = fourth;
      }
      return min;
  }
  
  int getMax(int first, int second, int third, int fourth) {
      int max = first;
      if( second &gt; max ) {
          max = second;
      } else if ( third &gt; max ) {
          max = third;
      } else if ( fourth &gt; max ) {
          max = fourth;
      }
      return max;
  }
  </code></pre>
  
  <p>Here is output when I debug my code. </p>
  
  <pre><code>tellerArray[0]  Teller *    0xbffff308  
  tellerArray[1]  Teller *    0x8048c64   
  tellerArray[2]  Teller *    0x1 
  tellerArray[3]  Teller *    0xffff
  </code></pre>
  
  <p>What my code is actually doing is using a linked list ( customer class ) to create a queue ( teller class ) and then based upon the time of each queue, it determines in which queue to put the next customer?</p>