2016-05-08 15 views
0
Valgrindのは私に 条件付きジャンプを与えているか、動きが私の現在のコードでヒープ割り当てによって エラーが作成された初期化されていない値(S)と 初期化されていない値に依存

Valgrindのは:条件付きジャンプや動きが初期化されていない値(複数可)に依存

void createMonsters(Game *game) { 
     (void) game; 

     Creature *nr; 

     int x = 0; 
     int r = 0; 
     int y = 0; 
     int check = 0; 
     int c = game->opts.numMonsters; 
     int nrm=0; 


     nr=malloc(sizeof (Creature) * c); 


     for(int i=0;i<c;i++){ 

      game->numMonsters=nrm; 
      r = rand()%2; 
      x = rand() % game->opts.mapWidth; 
      y = rand() % game->opts.mapHeight; 
      check=1; 

      while(check!=2){ 
       check = isBlocked(game,x,y); 
       if(check==1){ 
        x = rand() % game->opts.mapWidth; 
        y = rand() % game->opts.mapHeight; 
      } 
       else{ 

        nr[i].pos.y=y; 
        nr[i].pos.x=x; 
        nr[i].attack=attackPunch; 
        nrm += 1; 
        check=2; 
      } 
     } 

      if(r==0){ 
       nr[i].name[0] = 'C'; 
       nr[i].maxhp = 15; 
       nr[i].hp = nr[i].maxhp; 
       nr[i].sign = 'C'; 
     } 
      else{ 
       nr[i].name[0] = 'D'; 
       nr[i].maxhp = 50; 
       nr[i].hp = nr[i].maxhp; 
       nr[i].sign = 'D'; 


     }  

      game->monsters=nr; 

     } 
     game->numMonsters=nrm; 
    } 


int isBlocked(Game *game, int x, int y) 
    { 
      (void) game; 
      (void) x; 
      (void) y; 
      if(x>game->opts.mapWidth || y>game->opts.mapHeight){ 
       return 1; 
      }  } 

      if(game->numMonsters!=0){     
      for (unsigned int i = 0; i < game->numMonsters; i++){ 
       Creature *monst = &game->monsters[i]; 
       if (monst->pos.x == x && monst->pos.y == y){ 
        return 1; 
       }} 
      } 



      if(game->map.tile[y][x]==TILE_OPEN || game->map.tile[y][x]==TILE_ROOM){ 
       return 0; 
      } 
      else{ 
       return 1; 
      } 

    } 

valgrindはnr = mallocを指しています(sizeof(Creature)* c); 私は間違っているのですか?

typedef struct creature_st { 
     char name[20]; // name of the monster 
     char sign; // character that represents monster on the game display 
     Point pos; // location of the monster 
     float hp; // current hitpoints 
     unsigned int maxhp; // maximum hitpoints 
     void (*move)(struct game_st *, struct creature_st *); // current movement algorithm for monster 
     void (*attack)(struct game_st *, struct creature_st *); // current attack algorithm for monster 
    } Creature; 

フルvalgrindのエラー:

== 386 ==条件付きジャンプまたは移動が386 == 0x4C2C1B8に初期化されていない値(S)==に依存:strlenを(では/ usr/libに/ valgrindの/ vgpreload_memcheck/tmc/test/test)== 386 == 0x4057C9:tmc_run_tests(tmc/test/testの場合)== 386 == by 0x403F54:test_createMonsters(test_source.c:179)== 386 == 0x409377によって:srunner_run (tmc-check.c:134)== 386 == by 0x405464:main(test_source.c:529)== 386 ==ヒープ割り当てによって初期化されていない値が作成された== 386 == 0x4C28C20:malloc(in/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)== 386 == 0x4026C2:createMonsters(mon == 386 == by 0x409377:srunner_run(/ tmc/test/test)== 386 == 0x4057C9:tmc_run_tests(tmc/test/test) TMC-check.c:134)== 386 == 0x405464によって:メイン(test_source.c:529)== 386 ==

typedef struct game_st { 
     Map map; 
     unsigned int numMonsters; // number of elements in 'monsters' array 
     Creature *monsters; // dynamic array of all monsters 
     Point position; // current position of the player 
     float hp; // hit points, should never be higher than 'maxhp' 
     unsigned int maxhp; // maximum hit points 
     Options opts; 
    } Game; 
+0

は完全型ですか?これが構造体のtypedefであれば定義が含まれていますか? – 4pie0

+0

creatureのdefを追加しました –

+0

私は、そのファイルがその構造体の定義にアクセスできるかどうかを聞いていますか? – 4pie0

答えて

0

初期化されていない値がどこかにあなたのコードで作成され、その機能があることにアクセス値。 --track-origins=yesオプションを使用して、値がどこから来るのかを追跡することができます。

関連する問題