title: c++ a heap has been corrupted error when running the program
body: <p>I have the following program and every time I'm running it sometimes (most of the times) I'm getting heap corruption error</p>
  
  <p>I can't put my finger where its happenning since every run it pops in different place on my program.</p>
  
  <p>can someone put some light on it please?</p>
  
  <p>P.S heap corruption also pop when I'm trying to free p </p>
  
  <p>Thanks in advance</p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #include "stdafx.h"
  #include&lt;iostream&gt;
  #include&lt;string.h&gt;
  
  using namespace std;
  const int MAX_OF_PLAYERS = 10;
  const int SIZE = 100;
  
  struct player_t {
      char *name;
      int numOfShirt;
  };
  struct team_t {
      char *nameOfTeam;
      int maxOfPlayers;
      int numOfPlayers;
      player_t *players;
  
  };
  
  void readPlayer(player_t *player);
  void initTeam(team_t *team);
  void addPlayer(team_t *team);
  void printTeam(team_t *team);
  void freeAll(team_t *team);
  player_t** getAllPlayersStartWithA(team_t *team);
  void printAteam(player_t **p);
  
  int main()
  {
      team_t t;
      player_t **p;
      initTeam(&amp;t);
      addPlayer(&amp;t);
      addPlayer(&amp;t);
      printTeam(&amp;t);
      p = getAllPlayersStartWithA(&amp;t);
      if (p[0] != NULL)
          printAteam(p);
      system("pause");
      freeAll(&amp;t);
      //delete[] p;
  }
  
  void readPlayer(player_t *player)
  {
      char name[SIZE];
      cout &lt;&lt; " please enter the name of the player " &lt;&lt; endl;
      cin &gt;&gt; name;
      cout &lt;&lt; " please enter the num of the shirt " &lt;&lt; endl;
      cin &gt;&gt; player-&gt;numOfShirt;
      int size = strlen(name);
      char *res = new char[size + 2];
      strcpy(res, name);
      player-&gt;name = res;
  }
  void initTeam(team_t *team)
  {
  
      char name[SIZE];
  
      // get the team name
      cout &lt;&lt; " please enter your team name" &lt;&lt; endl;
      cin &gt;&gt; name;
      // get the name length
      int size = strlen(name);
      // allocate new array with length size
      team-&gt;nameOfTeam = new char[size + 1];
      // copy the string to the new array
      strcpy(team-&gt;nameOfTeam, name);
  
      // get the number of max players
      cout &lt;&lt; "please enter the number of the max players on your team" &lt;&lt; endl;
      cin &gt;&gt; team-&gt;maxOfPlayers;
  
      // create new players array
      player_t *players = new player_t[team-&gt;maxOfPlayers];
      // initial the players array
      for (int i = 0; i &lt; team-&gt;maxOfPlayers; i++)
      {
          players[i] = { 0 };
      }
      //bind the array to team
      team-&gt;players = players;
  
      // set current players to 0
      team-&gt;numOfPlayers = 0;
  
  }
  void addPlayer(team_t *team)
  {
  
      for (int i = 0; i &lt; team-&gt;maxOfPlayers; i++)
      {
          if (team-&gt;players[i].name == NULL)
          {
              readPlayer(team-&gt;players + i);
              break;
          }
      }
  
  }
  void printTeam(team_t *team)
  {
  
      cout &lt;&lt; "Team name: ";
      cout &lt;&lt; team-&gt;nameOfTeam &lt;&lt; endl;
  
      cout &lt;&lt; "Max Number of players in team: ";
      cout &lt;&lt; team-&gt;maxOfPlayers &lt;&lt; endl;
  
      cout &lt;&lt; "Current number of players in team: ";
      cout &lt;&lt; team-&gt;numOfPlayers &lt;&lt; endl;
  
      cout &lt;&lt; "Team Players:" &lt;&lt; endl;
      for (int i = 0; i &lt; team-&gt;maxOfPlayers; i++)
      {
          if (team-&gt;players[i].name)
          {
              cout &lt;&lt; "Player name: ";
              cout &lt;&lt; team-&gt;players[i].name;
              cout &lt;&lt; ", ";
              cout &lt;&lt; "Player shirt: ";
              cout &lt;&lt; team-&gt;players[i].numOfShirt &lt;&lt; endl;
          }
      }
      cout &lt;&lt; endl;
  }
  void freeAll(team_t *team)
  {
      for (int i = 0; i &lt; team-&gt;maxOfPlayers; i++)
      {
          if ((team-&gt;players + i)-&gt;name != NULL)
              delete[](team-&gt;players + i)-&gt;name;
      }
  
      delete[] team-&gt;players;
  }
  player_t** getAllPlayersStartWithA(team_t *team)
  {
      int sum = 0, position = 0;
      for (int i = 0; team-&gt;players[i].name != NULL; i++)
      {
          if (team-&gt;players[i].name[0] == 'a' || team-&gt;players[i].name[0] == 'A')
          {
              sum++;
          }
      }
  
      player_t **p = new player_t*[sum + 1];
  
      for (int i = 0; i &lt; team-&gt;maxOfPlayers; i++)
      {
          p[i] = NULL;
      }
  
      for (int i = 0; team-&gt;players[i].name != NULL; i++)
      {
          if (team-&gt;players[i].name[0] == 'a')
          {
              p[position++] = team-&gt;players + i;
          }
      }
      return p;
  }
  void printAteam(player_t **p)
  {
      cout &lt;&lt; "Players start with 'A': " &lt;&lt; endl;
      for (int i = 0; p[i] != NULL; i++)
      {
          cout &lt;&lt; "Player name: ";
          cout &lt;&lt; (p[i]-&gt;name);
          cout &lt;&lt; ", ";
          cout &lt;&lt; "Player shirt: ";
          cout &lt;&lt; (p[i]-&gt;numOfShirt) &lt;&lt; endl;
      }
  }
  </code></pre>