teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

ソースファイルの修正です

2021/11/01 05:23

投稿

Mellonn
Mellonn

スコア3

title CHANGED
File without changes
body CHANGED
@@ -12,44 +12,6 @@
12
12
  ## 発生している問題・エラーメッセージ
13
13
 
14
14
  ```
15
- cc -c -o open_addressing.o open_addressing.c
16
- open_addressing.c: In function ‘display’:
17
- open_addressing.c:111:12: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
18
- 111 | printf(nchr);
19
- | ^~~~
20
- | |
21
- | unsigned char
22
- In file included from open_addressing.c:1:
23
- /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char’
24
- 332 | extern int printf (const char *__restrict __format, ...);
25
- | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
26
- open_addressing.c:111:5: warning: format not a string literal and no format arguments [-Wformat-security]
27
- 111 | printf(nchr);
28
- | ^~~~~~
29
- open_addressing.c:115:12: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
30
- 115 | printf(nchr);
31
- | ^~~~
32
- | |
33
- | unsigned char
34
- In file included from open_addressing.c:1:
35
- /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char’
36
- 332 | extern int printf (const char *__restrict __format, ...);
37
- | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
38
- open_addressing.c:115:5: warning: format not a string literal and no format arguments [-Wformat-security]
39
- 115 | printf(nchr);
40
- | ^~~~~~
41
- open_addressing.c:117:22: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
42
- 117 | printf(dict->H[i].name);
43
- | ~~~~~~~~~~^~~~~
44
- | |
45
- | int
46
- In file included from open_addressing.c:1:
47
- /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘int’
48
- 332 | extern int printf (const char *__restrict __format, ...);
49
- | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
50
- open_addressing.c:117:5: warning: format not a string literal and no format arguments [-Wformat-security]
51
- 117 | printf(dict->H[i].name);
52
-
53
15
  $ ./a.out
54
16
  Breakpoint 1, main () at main_open_addressing.c:5
55
17
  5 int main(void) {
@@ -89,7 +51,7 @@
89
51
  }
90
52
 
91
53
  printf("Dictionary created!\n");
92
-
54
+
93
55
  return dictopenaddr;
94
56
  }
95
57
 
@@ -102,7 +64,8 @@
102
64
  return hash;
103
65
  }
104
66
 
105
- void insert_hash(DictOpenAddr *dict, int d){
67
+ void insert_hash(DictOpenAddr *dict, int d)
68
+ {
106
69
  if (search_hash(dict,d) != -1 )
107
70
  {
108
71
  return ;
@@ -111,31 +74,31 @@
111
74
  int count = 0;
112
75
  int b = h(dict,d,count);
113
76
  int init_b = b;
77
+
114
- DictData targetdata = (dict->H)[b];
78
+ while (b != init_b){
115
-
116
- if (targetdata.state == EMPTY || targetdata.state == DELETED)
79
+ if (dict->H[b].state == EMPTY || dict->H[b].state == DELETED)
117
80
  {
118
- targetdata.name = d;
81
+ dict->H[b].name = d;
119
- targetdata.state = OCCUPIED;
82
+ dict->H[b].state = OCCUPIED;
120
83
  }
121
84
 
122
85
  count += 1;
123
86
  b = h(dict,d,count);
124
-
125
- while (b != init_b)
126
- {
127
- return ;
128
87
  }
88
+
89
+ fprintf(stderr, "Cannot be inserted.\n");
129
90
  }
130
91
 
131
- int search_hash(DictOpenAddr *dict, int d){
92
+ int search_hash(DictOpenAddr *dict, int d)
93
+ {
132
94
  int count = 0;
133
95
  int b = h(dict,d,count);
134
96
  int init_b = b;
97
+
135
-
98
+ while (b != init_b){
136
- if ((dict->H)[b].state == OCCUPIED)
99
+ if (dict->H[b].state == OCCUPIED)
137
100
  {
138
- if ((dict->H)[b].name == d)
101
+ if (dict->H[b].name == d)
139
102
  {
140
103
  return b;
141
104
  }
@@ -147,45 +110,40 @@
147
110
  return -1;
148
111
  }
149
112
 
150
- count = count + 1;
113
+ count += 1;
114
+
151
115
  b = h(dict,d,count);
152
116
 
153
- while (b != init_b)
117
+
154
- {
118
+ }
155
119
  return -1;
156
120
  }
157
- }
158
121
 
159
- void delete_hash(DictOpenAddr *dict, int d){
122
+ void delete_hash(DictOpenAddr *dict, int d)
123
+ {
160
124
  int b = search_hash(dict,d);
161
- DictData targetdata = (dict->H)[b];
162
125
  if (b != -1)
163
126
  {
164
- targetdata.state = DELETED;
127
+ (dict->H)[b].state = DELETED;
165
128
  }
166
129
  }
167
130
 
168
- void display(DictOpenAddr *dict){
131
+ void display(DictOpenAddr *dict)
169
- int len = dict->B;
132
+ {
170
133
  int i = 0;
171
- char *newDict;
134
+
172
-
173
- newDict = (char *)malloc(0*len);
174
-
175
- while (i<len){
135
+ while (i<dict->B){
176
- switch ((dict->H)[i].state)
136
+ switch (dict->H[i].state)
177
137
  {
178
138
  case EMPTY:
179
- newDict[i] = 'e';
139
+ printf("e");
180
140
  case DELETED:
181
- newDict[i] = 'd';
141
+ printf("d");
182
142
  case OCCUPIED:
183
- sprintf(newDict[i],"%d",(dict->H)[i].name);
143
+ printf("%d",dict->H[i].name);
184
144
  }
185
145
  i++;
186
146
  }
187
- printf(newDict);
188
- free(newDict);
189
147
  }
190
148
 
191
149
  void delete_dict(DictOpenAddr *dict){

4

実行時の結果の修正

2021/11/01 05:23

投稿

Mellonn
Mellonn

スコア3

title CHANGED
File without changes
body CHANGED
@@ -12,26 +12,58 @@
12
12
  ## 発生している問題・エラーメッセージ
13
13
 
14
14
  ```
15
+ cc -c -o open_addressing.o open_addressing.c
16
+ open_addressing.c: In function ‘display’:
17
+ open_addressing.c:111:12: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
18
+ 111 | printf(nchr);
19
+ | ^~~~
20
+ | |
21
+ | unsigned char
22
+ In file included from open_addressing.c:1:
23
+ /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char’
24
+ 332 | extern int printf (const char *__restrict __format, ...);
25
+ | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
26
+ open_addressing.c:111:5: warning: format not a string literal and no format arguments [-Wformat-security]
27
+ 111 | printf(nchr);
28
+ | ^~~~~~
29
+ open_addressing.c:115:12: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
30
+ 115 | printf(nchr);
31
+ | ^~~~
32
+ | |
33
+ | unsigned char
34
+ In file included from open_addressing.c:1:
35
+ /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char’
36
+ 332 | extern int printf (const char *__restrict __format, ...);
37
+ | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
38
+ open_addressing.c:115:5: warning: format not a string literal and no format arguments [-Wformat-security]
39
+ 115 | printf(nchr);
40
+ | ^~~~~~
41
+ open_addressing.c:117:22: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
42
+ 117 | printf(dict->H[i].name);
43
+ | ~~~~~~~~~~^~~~~
44
+ | |
45
+ | int
46
+ In file included from open_addressing.c:1:
47
+ /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘int’
48
+ 332 | extern int printf (const char *__restrict __format, ...);
49
+ | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
50
+ open_addressing.c:117:5: warning: format not a string literal and no format arguments [-Wformat-security]
51
+ 117 | printf(dict->H[i].name);
52
+
53
+ $ ./a.out
15
54
  Breakpoint 1, main () at main_open_addressing.c:5
16
55
  5 int main(void) {
17
56
  (gdb) step
18
57
  6 DictOpenAddr *test_dict = create_dict(10);
19
58
  (gdb) step
20
- create_dict (len=32767) at open_addressing.c:6
59
+ create_dict (len=0) at open_addressing.c:7
21
60
  warning: Source file is more recent than executable.
22
- 6 DictOpenAddr *create_dict(int len)
61
+ 7 {
23
62
  (gdb) step
24
63
  8 DictOpenAddr *dictopenaddr = (DictOpenAddr *)malloc(sizeof(DictOpenAddr));
25
64
  (gdb) step
26
- 9
27
- (gdb) step
65
+ Dictionary created!
28
- 13 for (int i=0; i < len; i++)
29
- (gdb) step
30
-
31
66
  Program received signal SIGSEGV, Segmentation fault.
32
- 0x00007fffff6452b4 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>,
33
- f=<optimized out>) at genops.c:394
34
- 394 genops.c: No such file or directory.
35
67
  ```
36
68
 
37
69
  ### 該当のソースコード

3

コードを全て載せました

2021/11/01 03:03

投稿

Mellonn
Mellonn

スコア3

title CHANGED
File without changes
body CHANGED
@@ -60,7 +60,106 @@
60
60
 
61
61
  return dictopenaddr;
62
62
  }
63
+
64
+ int h(DictOpenAddr *dict, int d, int count){
65
+
66
+ int hash = d % (dict->B);
67
+
68
+ hash = (hash + count) % (dict->B);
69
+
70
+ return hash;
71
+ }
72
+
73
+ void insert_hash(DictOpenAddr *dict, int d){
74
+ if (search_hash(dict,d) != -1 )
75
+ {
76
+ return ;
77
+ }
78
+
79
+ int count = 0;
80
+ int b = h(dict,d,count);
81
+ int init_b = b;
82
+ DictData targetdata = (dict->H)[b];
83
+
84
+ if (targetdata.state == EMPTY || targetdata.state == DELETED)
85
+ {
86
+ targetdata.name = d;
87
+ targetdata.state = OCCUPIED;
88
+ }
89
+
90
+ count += 1;
91
+ b = h(dict,d,count);
92
+
93
+ while (b != init_b)
94
+ {
95
+ return ;
96
+ }
97
+ }
98
+
99
+ int search_hash(DictOpenAddr *dict, int d){
100
+ int count = 0;
101
+ int b = h(dict,d,count);
102
+ int init_b = b;
103
+
104
+ if ((dict->H)[b].state == OCCUPIED)
105
+ {
106
+ if ((dict->H)[b].name == d)
107
+ {
108
+ return b;
109
+ }
110
+
111
+ }
112
+
113
+ else if ((dict->H)[b].state == EMPTY)
114
+ {
115
+ return -1;
116
+ }
117
+
118
+ count = count + 1;
119
+ b = h(dict,d,count);
120
+
121
+ while (b != init_b)
122
+ {
123
+ return -1;
124
+ }
125
+ }
126
+
127
+ void delete_hash(DictOpenAddr *dict, int d){
128
+ int b = search_hash(dict,d);
129
+ DictData targetdata = (dict->H)[b];
130
+ if (b != -1)
131
+ {
132
+ targetdata.state = DELETED;
133
+ }
134
+ }
135
+
136
+ void display(DictOpenAddr *dict){
137
+ int len = dict->B;
138
+ int i = 0;
139
+ char *newDict;
140
+
141
+ newDict = (char *)malloc(0*len);
142
+
143
+ while (i<len){
144
+ switch ((dict->H)[i].state)
145
+ {
146
+ case EMPTY:
147
+ newDict[i] = 'e';
148
+ case DELETED:
149
+ newDict[i] = 'd';
150
+ case OCCUPIED:
151
+ sprintf(newDict[i],"%d",(dict->H)[i].name);
152
+ }
63
- (略)
153
+ i++;
154
+ }
155
+ printf(newDict);
156
+ free(newDict);
157
+ }
158
+
159
+ void delete_dict(DictOpenAddr *dict){
160
+ free(dict);
161
+ printf("Dictionary deleted!");
162
+ }
64
163
  ```
65
164
 
66
165
  <open_addresing.h>
@@ -112,7 +211,20 @@
112
211
  insert_hash(test_dict, 12);
113
212
  insert_hash(test_dict, 21);
114
213
  display(test_dict);
214
+
115
-
215
+ printf("Search 1 ...\t%d\n", search_hash(test_dict, 1));
216
+ printf("Search 2 ...\t%d\n", search_hash(test_dict, 2));
217
+ printf("Search 21 ...\t%d\n", search_hash(test_dict, 21));
218
+ printf("Search 5 ...\t%d\n", search_hash(test_dict, 5));
219
+
220
+ delete_hash(test_dict, 3);
116
-
221
+ display(test_dict);
222
+
223
+ delete_hash(test_dict, 11);
224
+ display(test_dict);
225
+
226
+ delete_dict(test_dict);
227
+
228
+ return EXIT_SUCCESS;
117
229
  }
118
230
  ```

2

create_dictの修正とエラーメッセージを更新しました

2021/10/31 23:42

投稿

Mellonn
Mellonn

スコア3

title CHANGED
File without changes
body CHANGED
@@ -19,13 +19,13 @@
19
19
  (gdb) step
20
20
  create_dict (len=32767) at open_addressing.c:6
21
21
  warning: Source file is more recent than executable.
22
- 6 DictOpenAddr *create_dict(int len){
22
+ 6 DictOpenAddr *create_dict(int len)
23
23
  (gdb) step
24
- 8 DictElement.name = 0;
24
+ 8 DictOpenAddr *dictopenaddr = (DictOpenAddr *)malloc(sizeof(DictOpenAddr));
25
25
  (gdb) step
26
- 9 DictElement.state = EMPTY;
26
+ 9
27
27
  (gdb) step
28
- 13 DDp = (DictData *)malloc(len * sizeof(DictData));
28
+ 13 for (int i=0; i < len; i++)
29
29
  (gdb) step
30
30
 
31
31
  Program received signal SIGSEGV, Segmentation fault.
@@ -43,28 +43,22 @@
43
43
  #include "open_addressing.h"
44
44
  #include "main_open_addressing.c"
45
45
 
46
- DictOpenAddr *create_dict(int len){
46
+ DictOpenAddr *create_dict(int len)
47
+ {
47
- DictData DictElement;//DictData型のDictElementの定義
48
+ DictOpenAddr *dictopenaddr = (DictOpenAddr *)malloc(sizeof(DictOpenAddr));
48
- DictElement.name = 0;//格納するデータ部の初期化
49
- DictElement.state = EMPTY;//空の状態
50
49
 
51
- DictData *DDp; //DictData型の配列を表すポインタ変数
50
+ dictopenaddr->H = (DictData *)malloc(sizeof(DictData) * len);
51
+ dictopenaddr->B = len;
52
52
 
53
- DDp = (DictData *)malloc(len * sizeof(DictData));//データの要素分の(len個)DictDataの主記憶領域の動的確保
54
-
55
- for (int i=0;i<len;i++)
53
+ for (int i=0; i < len; i++)
56
54
  {
55
+ dictopenaddr->H[i].name = -1;
57
- DDp[i] = DictElement;//DDpの配列にDictElementをlen個分格納
56
+ dictopenaddr->H[i].state = EMPTY;
58
57
  }
59
58
 
60
- DictOpenAddr *dictopenaddr;//DictOpenAddr型の変数の宣言
59
+ printf("Dictionary created!\n");
61
60
 
62
- dictopenaddr->H = DDp;//メンバHにDDpを格納
63
- dictopenaddr->B = len;//メンバBにlenを格納
64
-
65
- printf("Dictionary created!");
66
-
67
- return dictopenaddr;DictOpenAddr型のポインタを返す
61
+ return dictopenaddr;
68
62
  }
69
63
  (略)
70
64
  ```

1

関数名の訂正

2021/10/31 23:35

投稿

Mellonn
Mellonn

スコア3

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  エラーメッセージはgdbを利用してトレースした際に出たものです.
7
7
 
8
- open_addressing.cのDictOpenAddr関数に問題があることはわかるのですが,具体的な箇所がわからず困っています.
8
+ open_addressing.cのcreate_dict関数に問題があることはわかるのですが,具体的な箇所がわからず困っています.
9
9
 
10
10
  わかる方いましたらお願いします.
11
11