回答編集履歴

2

ソース追記

2018/10/07 11:28

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -73,3 +73,189 @@
73
73
  考え方:a[10]という配列がある場合、a[9]の次はa[0]になるわけですからインデックスが9になったら0にしてやればいいだけです。
74
74
 
75
75
  参考:[リングバッファ](https://ja.wikipedia.org/wiki/%E3%83%AA%E3%83%B3%E3%82%B0%E3%83%90%E3%83%83%E3%83%95%E3%82%A1)
76
+
77
+ 「追記」
78
+
79
+ 遅くなりましたが^^; たぶん動くw
80
+
81
+ ```c
82
+
83
+ usr~/test/c % ./a.out 11 22 33 44 55 6666 77 8888 99 00 aaaaaaa bb cc ddddddd ee ff
84
+
85
+ ./a.out
86
+
87
+ 11
88
+
89
+ 22
90
+
91
+ 33
92
+
93
+ 44
94
+
95
+ 55
96
+
97
+ 6666
98
+
99
+ 77
100
+
101
+ 8888
102
+
103
+ 99
104
+
105
+ 00
106
+
107
+ aaaaaaa
108
+
109
+ bb
110
+
111
+ cc
112
+
113
+ ddddddd
114
+
115
+ ee
116
+
117
+ ff
118
+
119
+ count:0
120
+
121
+ usr~/test/c % cat main.c
122
+
123
+ #include <stdio.h>
124
+
125
+ #include <stdlib.h>
126
+
127
+ #include <string.h>
128
+
129
+ //
130
+
131
+ typedef struct {
132
+
133
+ int count;
134
+
135
+ int rInx;
136
+
137
+ int wInx;
138
+
139
+ char *ptr[8];
140
+
141
+ } ringBuf;
142
+
143
+ //
144
+
145
+ enum { SET = 1, FULL = 0, EMPTY = 0 };
146
+
147
+ //
148
+
149
+ static int wQ(char *str, ringBuf *bPtr)
150
+
151
+ {
152
+
153
+ if (bPtr->ptr[bPtr->wInx]) {
154
+
155
+ return FULL;
156
+
157
+ }
158
+
159
+ bPtr->ptr[bPtr->wInx] = malloc(strlen(str) + 1);
160
+
161
+ strcpy(bPtr->ptr[bPtr->wInx], str);
162
+
163
+ //
164
+
165
+ bPtr->wInx = (bPtr->wInx + 1) & 0x07;
166
+
167
+ //
168
+
169
+ bPtr->count++;
170
+
171
+ //
172
+
173
+ return SET;
174
+
175
+ }
176
+
177
+ //
178
+
179
+ static char *rQ(ringBuf *bPtr)
180
+
181
+ {
182
+
183
+ char *ptr = bPtr->ptr[bPtr->rInx];
184
+
185
+ if (ptr == 0) {
186
+
187
+ return EMPTY;
188
+
189
+ }
190
+
191
+ //
192
+
193
+ bPtr->ptr[bPtr->rInx] = 0;
194
+
195
+ bPtr->rInx = (bPtr->rInx + 1) & 0x07;
196
+
197
+ //
198
+
199
+ bPtr->count--;
200
+
201
+ //
202
+
203
+ return ptr;
204
+
205
+ }
206
+
207
+ //
208
+
209
+ int main(int agc, char *agv[])
210
+
211
+ {
212
+
213
+ ringBuf rbf;
214
+
215
+ rbf.rInx = rbf.wInx = rbf.count = 0;
216
+
217
+ for (int i = 0; i < 8; i++) {
218
+
219
+ rbf.ptr[i] = 0;
220
+
221
+ }
222
+
223
+ //
224
+
225
+ for (int i = 0; i < agc; i++) {
226
+
227
+ if (wQ(agv[i], &rbf) == SET) {
228
+
229
+ char *str = rQ(&rbf);
230
+
231
+ if(str != 0){
232
+
233
+ puts(str);
234
+
235
+ free(str);
236
+
237
+ }
238
+
239
+ }
240
+
241
+ }
242
+
243
+ //
244
+
245
+ printf("count:%d\n",rbf.count);
246
+
247
+ //
248
+
249
+ return 0;
250
+
251
+ }
252
+
253
+ usr~/test/c %
254
+
255
+ ```
256
+
257
+ char *ptr[8]; ←バファのサイズ
258
+
259
+ bPtr->rInx = (bPtr->rInx + 1) & 0x07; ←0〜7を生成(2^n)をマスクする常套手段。
260
+
261
+ ・・・今のコンパイラは偉いから普通のif文で最後まで行ったら0に戻しても可d^^

1

加筆

2018/10/07 11:28

投稿

cateye
cateye

スコア6851

test CHANGED
@@ -65,3 +65,11 @@
65
65
  ```
66
66
 
67
67
  あとは、ポインタの配列をリングバッフにするだけです。
68
+
69
+ //
70
+
71
+ 注意:書き込み用のインデックスが読み出し用のインデックスを追い抜かないようにして下さい。さもないと、文字列を格納したアドレスが上書きされて、開放できなくなります。free()したらポインタをクリアすればどのデータが残っているか判定できます。
72
+
73
+ 考え方:a[10]という配列がある場合、a[9]の次はa[0]になるわけですからインデックスが9になったら0にしてやればいいだけです。
74
+
75
+ 参考:[リングバッファ](https://ja.wikipedia.org/wiki/%E3%83%AA%E3%83%B3%E3%82%B0%E3%83%90%E3%83%83%E3%83%95%E3%82%A1)