2016-10-27 11 views
0

こんにちは私は学校のプロジェクトに取り組んできたので、コードの助けが必要です。 exaclyハッシュテーブルvalgrindメモリリーク

機能がテーブルに新しい要素を挿入し、あなたの考えで何を意味するのか私には

これは、エラーのいずれかである私が得る間違っているものを仕事とひどいエラーを取り除くために必要にvalgrindのを使用

無効なサイズ1の書き込み0x4C29B32のstrcpy(vg_replace_strmem.c:458)0x401CE9:HTab_insert(ial.c:65) by 0x4019B5:main(main.c:82)アドレス0x5449785はブロックの後に0バイトです0x4C28FA4でのサイズ5の割り当ての :malloc(vg_replace_malloc.c:296) by 0x401CC9: HTab_insert(ial.c:64) 0x4019B5によって:メイン(main.cの:82)

HTab_listitem* HTab_insert(HTab_t* ptrht, Ttoken token) { 
    unsigned ind = hash_function(ptrht->htable_size,token); 
    HTab_listitem* item_ptr = NULL; 
    HTab_listitem* item = ptrht->list[ind]; 
    HTab_listitem* nextitem; 

    if(item == NULL) { 
     nextitem = malloc(sizeof(HTab_listitem)+sizeof(char)*(strlen(token.data)+1)); 

     if(nextitem == NULL) 
      /*allocation error*/ 
      return NULL; 
     else { 
      //printf("HERE\n"); 
      //printf("%s\n", token.data); 
      //memcpy(nextitem->token.data,token.data,strlen(token.data)+1); 
      int length = strlen(token.data); 
      nextitem->token.data = malloc(length * sizeof((char) +2)); 
      strcpy(nextitem->token.data,token.data); 
      nextitem->token.data[length] = '\0'; 
      nextitem->token.stav = token.stav; 
      //printf("HERE AFTER\n"); 
      nextitem->ptrnext = NULL; 

      item = ptrht->list[ind] = nextitem; 

      nextitem = NULL; 
      if(item == NULL) 
       return NULL; 
     } 
    } 
    else { 
     while(item != NULL) { 
      if(strcmp(item->token.data,token.data) == 0) { 
       //if found 
       item_ptr = item; 
       break; 
      } 
      else { 
       //next item 
       item_ptr = item; 
       item = item->ptrnext; 
      } 
     } 
     if(item_ptr != NULL && item != item_ptr) { 
      //not found insert next item 
      nextitem = malloc(sizeof(HTab_listitem*)+sizeof(char)*(strlen(token.data)+1)); 
      if(nextitem == NULL) 
       /*allocation error*/ 
       return NULL; 
      else { 
       //memcpy(nextitem->token.data,token.data,strlen(token.data)+1); 
       int length = strlen(token.data); 
       nextitem->token.data = malloc(length * sizeof((char) +2)); 
       strcpy(nextitem->token.data,token.data); 
       nextitem->token.data[length] = '\0'; 
       nextitem->token.stav = token.stav; 

       nextitem->ptrnext = NULL; 
       item = nextitem; 
       if(item == NULL) 
        return NULL; 
       item_ptr->ptrnext = item; 
      } 
     } 
    } 
    return item; 
} 
+1

[mcve]を表示してください。 –

+0

'token.data'のコピーに' strcpy'を使います。 'token.data'がnullで終了していますか? – GMichael

+1

これは間違っています(+2は括弧の中に入れてはいけません): 'malloc(length * sizeof((char)+2))'。あなたは 'malloc(length +2)'を意味しますか? (サイドノート: 'sizeof(char)'は '1 'なので省略可能です) –

答えて

0

あなたは未定義の動作につながる可能性があるいくつかの割り当てエラーを持っています。私は上から下

まずにコードになります 、あなたは多くのメモリにnextitem-> token.dataが後で割り当てられているので、次は、十分なはず

nextitem = malloc(sizeof(HTab_listitem)+sizeof(char)*(strlen(token.data)+1)); 

を割り当てます。 token.dataを割り当てるとき

nextitem = malloc(sizeof(HTab_listitem)); 

また、次のようにします。

int length = strlen(token.data); 
nextitem->token.data = malloc(sizeof(char) * (length + 1)); 
nextitem->token.data[length] = 0; 

あなたの2番目の項目の割り当ては再び右のサイズではありません。 構造体のサイズの代わりにポインタのsizeof(8バイト)を割り当て、token.dataを追加することはまだ有用ではありません。あるべき

nextitem = malloc(sizeof(HTab_listitem*)+sizeof(char)*(strlen(token.data)+1)); 
//Error here (HTab_listitem*) 

nextitem = malloc(sizeof(HTab_listitem)); 

その後、再びpreviousely行われているようtoken.dataを割り当てます。

関連する問題