我有一个程序,每次运行它时,有时(大多数情况下)会出现堆破坏错误。我无法确定它发生的位置,因为每次运行它都会在程序中的不同位置弹出。请问有人能提供一些帮助吗?顺便说一句,当我尝试释放p时也会出现堆破坏的情况。谢谢!
#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"
#include<iostream>
#include<string.h>
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(&t);
addPlayer(&t);
addPlayer(&t);
printTeam(&t);
p = getAllPlayersStartWithA(&t);
if (p[0] != NULL)
printAteam(p);
system("pause");
freeAll(&t);
//delete[] p;
}
void readPlayer(player_t *player)
{
char name[SIZE];
cout << " please enter the name of the player " << endl;
cin >> name;
cout << " please enter the num of the shirt " << endl;
cin >> player->numOfShirt;
int size = strlen(name);
char *res = new char[size + 2];
strcpy(res, name);
player->name = res;
}
void initTeam(team_t *team)
{
char name[SIZE];
// get the team name
cout << " please enter your team name" << endl;
cin >> name;
// get the name length
int size = strlen(name);
// allocate new array with length size
team->nameOfTeam = new char[size + 1];
// copy the string to the new array
strcpy(team->nameOfTeam, name);
// get the number of max players
cout << "please enter the number of the max players on your team" << endl;
cin >> team->maxOfPlayers;
// create new players array
player_t *players = new player_t[team->maxOfPlayers];
// initial the players array
for (int i = 0; i < team->maxOfPlayers; i++)
{
players[i] = { 0 };
}
//bind the array to team
team->players = players;
// set current players to 0
team->numOfPlayers = 0;
}
void addPlayer(team_t *team)
{
for (int i = 0; i < team->maxOfPlayers; i++)
{
if (team->players[i].name == NULL)
{
readPlayer(team->players + i);
break;
}
}
}
void printTeam(team_t *team)
{
cout << "Team name: ";
cout << team->nameOfTeam << endl;
cout << "Max Number of players in team: ";
cout << team->maxOfPlayers << endl;
cout << "Current number of players in team: ";
cout << team->numOfPlayers << endl;
cout << "Team Players:" << endl;
for (int i = 0; i < team->maxOfPlayers; i++)
{
if (team->players[i].name)
{
cout << "Player name: ";
cout << team->players[i].name;
cout << ", ";
cout << "Player shirt: ";
cout << team->players[i].numOfShirt << endl;
}
}
cout << endl;
}
void freeAll(team_t *team)
{
for (int i = 0; i < team->maxOfPlayers; i++)
{
if ((team->players + i)->name != NULL)
delete[](team->players + i)->name;
}
delete[] team->players;
}
player_t** getAllPlayersStartWithA(team_t *team)
{
int sum = 0, position = 0;
for (int i = 0; team->players[i].name != NULL; i++)
{
if (team->players[i].name[0] == 'a' || team->players[i].name[0] == 'A')
{
sum++;
}
}
player_t **p = new player_t*[sum + 1];
for (int i = 0; i < team->maxOfPlayers; i++)
{
p[i] = NULL;
}
for (int i = 0; team->players[i].name != NULL; i++)
{
if (team->players[i].name[0] == 'a')
{
p[position++] = team->players + i;
}
}
return p;
}
void printAteam(player_t **p)
{
cout << "Players start with 'A': " << endl;
for (int i = 0; p[i] != NULL; i++)
{
cout << "Player name: ";
cout << (p[i]->name);
cout << ", ";
cout << "Player shirt: ";
cout << (p[i]->numOfShirt) << endl;
}
}
char*
而不是std::string
?为什么将对象的指针传递给函数而不是引用? - Algirdas Preidžiusfor (int i = 0; team->players[i].name != NULL; i++)
。如果球队有一整套球员,那么他们中没有人会有空名字。 - Bo Persson