1


我目前正在开发一款游戏,我在以下代码中遇到了夹板问题,以将新的敌人结构添加到我的链接列表中。

void generate_enemy(enemy_struct* enemy)
{
    enemy_struct* new_enemy;

    // Make sure the incoming enemy isn't null.
    if(enemy == NULL)
    {
        return;
    }

    // Run through till we find the last enemy in the list.
    while(NULL != enemy->next_enemy)
    {
        enemy = enemy->next_enemy;
    }

    // Create a new enemy and point the last enemy to it.
    new_enemy = malloc(sizeof(enemy_struct));

    // If we're out of memory, don't bother making the new enemy.
    if(NULL == new_enemy)
    {
        return;
    }
    else
    {
        // Initialise the new enemy.
        new_enemy->location = 1;
        new_enemy->motion = ENEMY_STATIC;
        new_enemy->dead = false;//true;
        // Ensure it carries the last enemy flag.
        new_enemy->next_enemy = NULL;

        // Put new enemy in previous enemy.
        enemy->next_enemy = new_enemy;
    }
    return;
}

夹板给出警告:

enemy.c: (in function generate_enemy)
enemy.c:57:12: Storage *(enemy->next_enemy) reachable from parameter contains 4
                  undefined fields: location, motion, dead, next_enemy
  Storage derivable from a parameter, return value or global is not defined.
  Use /*@out@*/ to denote passed or returned storage which need not be defined.
  (Use -compdef to inhibit warning)

第 57 行是函数中的最后一个返回值。

现在,我相当有信心我的代码不能返回未定义的值,因为我要么设置所有字段,要么不更改任何内容并退出。

在某些情况下,我错过了将未定义的东西发回的地方吗?如果没有,停止夹板发出此警告的最佳方法是什么?

4

0 回答 0