无法找出导致段错误的原因 |来自硬件

标签 c

我有以下数据类型:

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

最佳答案

teamCreate() 中,您永远不会在分配后将 formations 局部变量设置到您的团队结构中。

这是第一个:

//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;

关于无法找出导致段错误的原因 |来自硬件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19717332/

相关文章:

iphone - 在C中跨步复制内存的最快方法?

c - 合并十六进制值中的位

c - 我在使用线程传递参数时遇到问题

c - 如何保存 execvp 输出

c - 当我重新分配一个更小的缓冲区时,它会删除缓冲区中的数据直到达到正确的大小吗?

c - Stream != nullptr Visual Studio,简单地打印 txt 文件中的值 (C)

c - 有没有办法为数组重新分配内存以允许在开头添加附加值?

c - c中的指针与数组有什么不同?

c - 在C中使用malloc不起作用

c - 你应该释放传递给 prctl() 的内存吗?