我有以下数据类型:
编队.h
typedef struct formation_t {
Player players[FORMATION_NUM_PLAYERS];
int numPlayers; /* How many players are in the above array */
int timesPlayed;
int timesWon;
}* Formation;
团队.h
typedef struct team_t {
char* name;
char* coachName;
Formation* formations;
int currFormations;
int maxFormations;
}* Team;
以及以下功能:
Team teamCreate(char* name, char* coach, int maxFormations)
{
//Check if parameters make sense.
if (name == NULL || coach == NULL || maxFormations < 1)
{
return NULL;
}
//Try allocating memory for name.
char* teamName = malloc(strlen(name) + 1);
if (teamName == NULL)
{
return NULL;
}
strcpy(teamName, name);
//Try allocating memory for coachName.
char* coachName = malloc(strlen(coach) + 1);
if (coachName == NULL)
{
free(teamName);
return NULL;
}
strcpy(coachName, coach);
//Try allocating memory for formations.
Formation* formations = malloc(sizeof(Formation) * maxFormations);
if (formations == NULL)
{
free(teamName);
free(coachName);
return NULL;
}
//Try allocating memory for team.
Team newTeam = malloc(sizeof(struct team_t));
if (newTeam == NULL)
{
free(teamName);
free(coachName);
free(formations);
return NULL;
}
//Initialize newly created team.
newTeam->name = teamName;
newTeam->coachName = coachName;
newTeam->maxFormations = maxFormations;
newTeam->currFormations = 0;
//Return created team.
return newTeam;
}
TeamResult teamAddFormation(Team team, Formation formation)
{
//Check for TEAM_NULL_ARGUMENT.
if (team == NULL | formation == NULL)
{
return TEAM_NULL_ARGUMENT;
}
//Check for TEAM_IS_FULL.
if (team->currFormations == team->maxFormations)
{
return TEAM_IS_FULL;
}
//Add formation.
printf("\n -about to clone- \n");
team->formations[team->currFormations] = formationClone(formation);
printf("\n -clone completed- \n");
team->currFormations = team->currFormations + 1;
return TEAM_SUCCESS;
}
Formation formationClone(Formation formation)
{
if (formation == NULL)
{
return NULL;
}
Formation newFormation = malloc(sizeof(struct formation_t));
if (newFormation == NULL)
{
return NULL;
}
*newFormation = *formation;
return newFormation;
}
当我尝试使用以下代码测试我的工作时,在“即将克隆”之后立即出现分段错误。
Team team = teamCreate("Ac Milan", "Carletto", 2);
Formation formation1 = formationCreate();
ASSERT_NULL_ARGUMENT(teamAddFormation(NULL, formation1));
ASSERT_SUCCESS(teamAddFormation(team, formation1));