2

我有以下数据类型:

编队.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));
4

2 回答 2

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;
于 2013-10-31T22:17:10.967 回答
-1
//Try allocating memory for name.
char* teamName = malloc(strlen(name) + 1);
if (teamName == NULL)
{
    return NULL;
}
strcpy(teamName, name);

teamName 指向一个已分配的字符数组。该数组从未被删除。这会导致内存泄漏。

于 2013-10-31T22:07:40.233 回答