前提・実現したいこと
オープンアドレス法というハッシュ法をCで実装しようとしています.
環境はWindows10上のUbuntu 20.04.2 LTSです.
エラーメッセージはgdbを利用してトレースした際に出たものです.
open_addressing.cのcreate_dict関数に問題があることはわかるのですが,具体的な箇所がわからず困っています.
わかる方いましたらお願いします.
発生している問題・エラーメッセージ
$ ./a.out Breakpoint 1, main () at main_open_addressing.c:5 5 int main(void) { (gdb) step 6 DictOpenAddr *test_dict = create_dict(10); (gdb) step create_dict (len=0) at open_addressing.c:7 warning: Source file is more recent than executable. 7 { (gdb) step 8 DictOpenAddr *dictopenaddr = (DictOpenAddr *)malloc(sizeof(DictOpenAddr)); (gdb) step Dictionary created! Program received signal SIGSEGV, Segmentation fault.
該当のソースコード
<open_addresing.c>
C
1#include <stdio.h> 2#include <stdlib.h> 3#include "open_addressing.h" 4#include "main_open_addressing.c" 5 6DictOpenAddr *create_dict(int len) 7{ 8 DictOpenAddr *dictopenaddr = (DictOpenAddr *)malloc(sizeof(DictOpenAddr)); 9 10 dictopenaddr->H = (DictData *)malloc(sizeof(DictData) * len); 11 dictopenaddr->B = len; 12 13 for (int i=0; i < len; i++) 14 { 15 dictopenaddr->H[i].name = -1; 16 dictopenaddr->H[i].state = EMPTY; 17 } 18 19 printf("Dictionary created!\n"); 20 21 return dictopenaddr; 22} 23 24int h(DictOpenAddr *dict, int d, int count){ 25 26 int hash = d % (dict->B); 27 28 hash = (hash + count) % (dict->B); 29 30 return hash; 31} 32 33void insert_hash(DictOpenAddr *dict, int d) 34{ 35 if (search_hash(dict,d) != -1 ) 36 { 37 return ; 38 } 39 40 int count = 0; 41 int b = h(dict,d,count); 42 int init_b = b; 43 44 while (b != init_b){ 45 if (dict->H[b].state == EMPTY || dict->H[b].state == DELETED) 46 { 47 dict->H[b].name = d; 48 dict->H[b].state = OCCUPIED; 49 } 50 51 count += 1; 52 b = h(dict,d,count); 53 } 54 55 fprintf(stderr, "Cannot be inserted.\n"); 56} 57 58int search_hash(DictOpenAddr *dict, int d) 59{ 60 int count = 0; 61 int b = h(dict,d,count); 62 int init_b = b; 63 64 while (b != init_b){ 65 if (dict->H[b].state == OCCUPIED) 66 { 67 if (dict->H[b].name == d) 68 { 69 return b; 70 } 71 72 } 73 74 else if ((dict->H)[b].state == EMPTY) 75 { 76 return -1; 77 } 78 79 count += 1; 80 81 b = h(dict,d,count); 82 83 84 } 85 return -1; 86 } 87 88void delete_hash(DictOpenAddr *dict, int d) 89{ 90 int b = search_hash(dict,d); 91 if (b != -1) 92 { 93 (dict->H)[b].state = DELETED; 94 } 95} 96 97void display(DictOpenAddr *dict) 98{ 99 int i = 0; 100 101 while (i<dict->B){ 102 switch (dict->H[i].state) 103 { 104 case EMPTY: 105 printf("e"); 106 case DELETED: 107 printf("d"); 108 case OCCUPIED: 109 printf("%d",dict->H[i].name); 110 } 111 i++; 112 } 113} 114 115void delete_dict(DictOpenAddr *dict){ 116 free(dict); 117 printf("Dictionary deleted!"); 118}
<open_addresing.h>
C
1#ifndef INCLUDE_GUARD_OPEN_ADDRESSING_H 2#define INCLUDE_GUARD_OPEN_ADDRESSING_H 3 4typedef enum state { 5 EMPTY, 6 DELETED, 7 OCCUPIED 8} State; 9 10typedef struct dict_data { 11 int name; 12 State state; 13} DictData; 14 15typedef struct dict_open_addr { 16 DictData *H; 17 int B; 18} DictOpenAddr; 19 20DictOpenAddr *create_dict(int len); 21int h(DictOpenAddr *dict, int d, int count); 22void insert_hash(DictOpenAddr *dict, int d); 23int search_hash(DictOpenAddr *dict, int d); 24void delete_hash(DictOpenAddr *dict, int d); 25void display(DictOpenAddr *dict); 26void delete_dict(DictOpenAddr *dict); 27 28#endif // INCLUDE_GUARD_OPEN_ADDRESSING_H
<main_open_addresing.c>
C
1#include <stdio.h> 2#include <stdlib.h> 3#include "open_addressing.h" 4 5int main(void) { 6 DictOpenAddr *test_dict = create_dict(10); 7 8 insert_hash(test_dict, 1); 9 insert_hash(test_dict, 2); 10 insert_hash(test_dict, 3); 11 insert_hash(test_dict, 11); 12 insert_hash(test_dict, 12); 13 insert_hash(test_dict, 21); 14 display(test_dict); 15 16 printf("Search 1 ...\t%d\n", search_hash(test_dict, 1)); 17 printf("Search 2 ...\t%d\n", search_hash(test_dict, 2)); 18 printf("Search 21 ...\t%d\n", search_hash(test_dict, 21)); 19 printf("Search 5 ...\t%d\n", search_hash(test_dict, 5)); 20 21 delete_hash(test_dict, 3); 22 display(test_dict); 23 24 delete_hash(test_dict, 11); 25 display(test_dict); 26 27 delete_dict(test_dict); 28 29 return EXIT_SUCCESS; 30}
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/31 23:41
2021/10/31 23:43