我有以下数据类型:
typedef struct formation_t {
Player players[FORMATION_NUM_PLAYERS];
int numPlayers; /* How many players are in the above array */
int timesPlayed;
int timesWon;
}* Formation;
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));
答案 0 :(得分:2)
在teamCreate()
中,在分配之后,你永远不会将你的编队局部变量设置到你的团队结构中。
这是第一次:
//Try allocating memory for formations.
Formation* formations = malloc(sizeof(Formation) * maxFormations);
if (formations == NULL)
{
free(teamName);
free(coachName);
return NULL;
}
然后在分配主机对象后执行此操作:
//Initialize newly created team.
newTeam->name = teamName;
newTeam->coachName = coachName;
newTeam->maxFormations = maxFormations;
newTeam->currFormations = 0;
//Return created team.
return newTeam;
您永远不会将结构指针保存到结构成员,因此指针成员是不确定的,使用它会调用未定义的行为。
将其添加到该分配堆栈的底部:
newTeam->formations = formations;
答案 1 :(得分:-1)
//Try allocating memory for name.
char* teamName = malloc(strlen(name) + 1);
if (teamName == NULL)
{
return NULL;
}
strcpy(teamName, name);
团队名称指向已分配的char数组。这个数组从未删除过。这会导致内存泄漏。