title: Pointer Help in C++
body: <p>I am new to c++(have a java background) and thus pointers are sort of new to me. I am dealing with an array of pointers where each index points to an object on the heap as so:</p>
  
  <pre><code>Deck::Deck()
  {
  seed = rand()%100; //this will be used in shuffle method
  srand(seed); 
  for(int i=0;i&lt;deckSize;i+=3) //deckSize=12 in this case, p defined as CardTypes* p[deckSize]
  {
      p[i]= new Infantry(); 
      p[i+1] = new Artillery();
      p[i+2] = new Cavalry();
  
  }
  </code></pre>
  
  <p>}
  All 3 of these classes are subclasses of the class CardTypes(which was only created so I could store diff types in an array).</p>
  
  <pre><code>class CardTypes
  {
  public:
  virtual string getCard() = 0;
  virtual ~CardTypes() {};
  };
  class Infantry: public CardTypes
  {
  const string name = "Infantry";
  public:
  string getCard(); //this simply returns "name" so that I can differentiate each object in the array by a data value
  };
  class Artillery:public CardTypes 
  {
  const string name= "Artillery";
  public:
  string getCard();
  };
  class Cavalry:public CardTypes
  {
  const string name = "Cavalry";
  public:
  string getCard();
  };
  </code></pre>
  
  <p>Although not a great way to do it, I have created another array of pointers(CardTypes* s[deckSize) which copies pointers from p into s randomly(thus mimicking a shuffle in a deck of cards):</p>
  
  <pre><code>void Deck::shuffle() //this is the method that puts objects in s to be grabbed in draw()
  {
  int j = 0;
  int k = 1;
  int l = 2; //initial setup(index 0 will have Infantry, index 1 will have Artillery and index 3 will have Cavalry and this pattern continues throughout p)
  int n = rand()%3 + 1; //gives random # between 1 and 3 1=infantry,2 = artillery,3 = cavalry
  int i=0; //counter for loop
  while(i&lt;deckSize)
  {
       n = rand()%3+1;
      if(n==1)
      {
          if(j&gt;9) //means no more infantry cards as due to pattern of p 
          infantry cards stop after index 9
          {
              continue; //used to reset loop foranother iteration(will get random number,I know this is bad for time complexity)
          }
          else
          {
          s[i] = p[j]; //copy "Infantry" pointer to s
          j+=3;
          i++;
          }
      }
      else if(n==2)
      {
          if(k&gt;10)//means no Artillery cards due to pattern in p
          {
              continue;
          }
          else
          {
              s[i] = p[k];//copy "Artillery" pointer to s
              k+=3;
              i++;
          }
      }
      else
      {
          if(l&gt;11) //means no more cavalary cards due to pattern in p
          {
              continue;
          }
          else
          {
              s[i] = p[l]; //copy "Cavalry" pointer to s
              l+=3;
              i++;
          }
      }
  }
  }
  </code></pre>
  
  <p>Now my issue is i am trying to create a draw method that grabs a pointer from s and returns it. My program completely crashes when I attempt this and I am not sure why:</p>
  
  <pre><code>CardTypes* Deck::draw() //draws a card from the deck and returns it
  {
  CardTypes* card = s[deckSize];
  delete s[deckSize];//clear heap
  s[deckSize] = NULL;//remove what pointer was pointing too (as card has been drawn)
  deckSize--;
  return card;
  }
  </code></pre>
  
  <p>I then attempt to call this method:`</p>
  
  <pre><code>int main()
  {
  Deck d1;
  d1.shuffle(); //this works
  d1.getCurrentDeck();//this works, just prints out each objects getCard() method in s
  CardTypes* card = d1.draw();//does not cause a crash
  cout&lt;&lt;"Card:"&lt;&lt;card-&gt;getCard() &lt;&lt;"\n";//crashes here
  }
  </code></pre>
  
  <p>This issue is probably due to my inexperience with pointers but any help would be appreciated. Also note I delete the arrays after I am done with the program using delete [] p and delete [] s, I have not included this in the code as it is not of issue right now.</p>