質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

ハッシュ

ハッシュは、高速にデータ検索を行うアルゴリズムのことです。

Q&A

解決済

1回答

842閲覧

C言語;ハッシュ;add関数の動作停止

shimaneya

総合スコア2

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

ハッシュ

ハッシュは、高速にデータ検索を行うアルゴリズムのことです。

0グッド

0クリップ

投稿2020/06/06 12:22

前提

C言語でハッシュを用いた、プログラムを作っています。

発生している問題

コンパイラは通りますが、実行中に動作が停止します。
空のハッシュテーブルに連結リストを入れるときは、動作は停止しませんが。
連結リストが存在するハッシュテーブルに連結リストを入れようとすると、動作が停止します。
何が問題で、どうやって解決すればよろしいでしょうか?

該当のソースコード

C言語

#include <stdio.h> #include <stdlib.h> #include <string.h> #define HASHSIZE 17 struct match *hashtable[HASHSIZE]; struct match_score{ int year; int month; int day; int home_score; int away_score; struct match_score *next; } ; struct match{ char *home; char *away; struct match_score *r; struct match *next; } ; int hash(char *home, char *away); struct match *make_match(char *home, char *away, struct match_score *r, struct match *next); struct match_score *make_match_score(int year, int month, int day, int home_score, int away_score, struct match_score *next); void add(int year, int month, int day, char *home, int home_score, int away_score, char *away); struct match *find(char *home, char *away); int main(void) { for(int i=0; i < HASHSIZE; i++){ hashtable[i] = NULL; } printf("%d\n",hash("Kashima_Antler", "Yokohama_Flugels")); add(1992, 9, 5, "Kashima_Antler", 4, 2, "Yokohama_Flugels"); printf("%d\n",hashtable[2]->r->year); add(1996, 5, 12, "mashima_Antler", 3, 1, "Yokohama_Flugels"); printf("%d\n",hashtable[2]->r->year); return 0; } int hash(char *home, char *away) { int hashval = 0; int i = 0; while(home[i] != '\0') { hashval += home[i]; i++; } while(away[i] != '\0') { hashval += away[i]; i++; } return hashval % HASHSIZE; } struct match_score *make_match_score(int year, int month, int day, int home_score, int away_score, struct match_score *next) { struct match_score *newmsp = malloc(sizeof(struct match)); if (newmsp != NULL) { newmsp->year = year; newmsp->month = month; newmsp->day = day; newmsp->home_score = home_score; newmsp->away_score = away_score; newmsp->next = next; } return newmsp; } struct match *make_match(char *home, char *away, struct match_score *r, struct match *next) { struct match *newmp = malloc(sizeof(struct match)); if (newmp != NULL) { newmp->home = (char*)malloc(sizeof(char) * sizeof(home)); newmp->away = (char*)malloc(sizeof(char) * sizeof(away)); strcpy(newmp->home, home); strcpy(newmp->away, away); newmp->r = r; newmp->next = next; } return newmp; } struct match *find(char *home, char *away) { struct match *p; int hashval = hash(home, away); p = hashtable[hashval]; while (p != NULL) { if (strcmp(home, p->home) == 0 && strcmp(away, p->away) == 0) { return p; } p = p->next; } return NULL; } void add(int year, int month, int day, char *home, int home_score, int away_score, char *away) { struct match *newmp = NULL; struct match_score *newmsp = NULL; int hashval = hash(home,away); struct match *pmatch = NULL; struct match *prematch = NULL; if (hashtable[hashval] == NULL) { newmsp = make_match_score(year, month, day, home_score, away_score, NULL); newmp = make_match(home, away, newmsp, hashtable[hashval]); hashtable[hashval] = newmp; return ; } struct match *fmp = find(home, away); if (fmp != NULL) { newmsp = make_match_score(year, month, day, home_score, away_score, fmp->r); fmp->r = newmsp; return ; } newmsp = make_match_score(year, month, day, home_score, away_score, NULL); newmp = make_match(home, away, newmsp, hashtable[hashval]); hashtable[hashval] = newmp; return ; }

補足情報(FW/ツールのバージョンなど)

開発環境 : VS Code (バージョン1.45.1)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

newmp->home = (char*)malloc(sizeof(char) * sizeof(home));

homeはポインタなので、sizeof(home)は8固定です。
ここでは8バイトを確保してますが、それでいいのでしょうか

#ほかのところもおなじ

投稿2020/06/06 12:27

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shimaneya

2020/06/06 13:01

すいません、そのままもう一回実行したら、正常に動作しました。 回答していただきありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問