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

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

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

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

ハッシュ

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

Q&A

解決済

2回答

558閲覧

C言語:ハッシュを用いたプログラムが実行中に停止してしまう

shimaneya

総合スコア2

C

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

ハッシュ

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

0グッド

0クリップ

投稿2020/06/07 02:46

編集2020/06/07 02:53

前提・実現したいこと

C言語でハッシュを用いて、Jリーグの対戦成績を整理するプログラムを作っています。
ハッシュテーブルに構造体matchのリストを挿入するプログラムを作成中に問題が起きました。

発生している問題

コンパイラは通りますが、実行中に動作が停止したりすることがあります。
プログラムを実行すると、正常に最後まで実行できたり、
途中で動作が停止して、実行途中のままプログラムの実行が終わってしまったりして非常に不安定です。
どこかに問題があるのでしょうか?

該当のソースコード(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); //match_scoreの連結リストを生成 struct match_score *make_match_score(int year, int month, int day, int home_score, int away_score, struct match_score *next); //matchの連結リストを生成 struct match *make_match(char *home, char *away, struct match_score *r, struct match *next); //2つのキーを持つmatchの連結リストの探索 struct match *find(char *home, char *away); //ハッシュテーブルにmatchの連結リストを挿入 void add(int year, int month, int day, char *home, int home_score, int away_score, 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("%s\n",hashtable[2]->home); printf("%d\n",hash("mashima_Antler", "Yokohama_Flugels")); add(1996, 5, 12, "mashima_Antler", 3, 1, "Yokohama_Flugels"); printf("%s\n",hashtable[2]->home); printf("%d\n",hash("mPshima_Antler", "Yokohama_Flugels")); add(1994, 3, 1, "mPshima_Antler", 1, 3, "Yokohama_Flugels"); printf("%s\n",hashtable[2]->home); 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; } //match_scoreの連結リストを生成 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; } //matchの連結リストを生成 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; } //2つのキーを持つmatchの連結リストの探索 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; } //ハッシュテーブルにmatchの連結リストを挿入 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); 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

回答2

0

struct match *make_match(char *home, char away, struct match_score r, struct match next) {
関数の
newmp->home = (char
)malloc(sizeof(char) * sizeof(home)); を
newmp->home = (char
)malloc(sizeof(char) * (strlen(home)+1));
に変えてください。
newmp->away = (char
)malloc(sizeof(char) * sizeof(away));も同様に変更してください。

何故、そのようにするのかが判らない場合は、再度その旨、補足してください。

投稿2020/06/07 23:57

tatsu99

総合スコア5424

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

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

0

ベストアンサー

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

繰り返しになりますが、ここでは8バイトしか確保されていません。
これが正しい動作なのか確認しましょう。

動かなくなる、というのは、メモリアクセス違反やメモリ破壊が起こったら起こるもんです。
ちゃんとコードを検証しましょう

投稿2020/06/07 02:51

y_waiwai

総合スコア87719

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

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

shimaneya

2020/06/07 03:15

再度、回答していただきありがとうございます。 これを修正するには sizeof(home) の部分をコピーする文字列の文字数に変えればよいのでしょうか? コメントしていただけると幸いです。
y_waiwai

2020/06/07 03:46

あなたが組んだあなたのコードです。 そこでどういう動作をするのが相当なのかあなたはわかってるはずですがどうでしょうか。 #ぶっちゃけ私はコード読んでませんw
shimaneya

2020/06/07 04:28

なるほど、わかりました。 回答を参考にしてコードを修正してみます。 コメントしていただきありがとうございましたm(_ _)m
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問