Помогите разобраться. Есть двусвязная очередь, при запуске функции myFunc, и выпадении 2 или 3, программа завершается с ошибкой.
Т.е запуская цикл:
C |
1
2
| for (i = 0; i < 100000000; i++)
myFunc(q, i); |
|
и если в myFunc, значение переменной ранд = 2 или 3, то программа аварийно завершается.
если выпало 2, то мы добавляем в случайную позицию значение.
если выпало 3, то мы удаляем значение из случайной позиции.
Так же, я думаю, что ошибка располагается в функции
, вроде как в строке
C |
1
| struct listnode *buffer = q->head; |
|
но я могу ошибаться.
Помогите разобраться, не понимаю почему происходит аварийное завершение.
C |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
| #include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define DEBUG_pushFront 0
#define DEBUG_pushBack 0
#define DEBUG_popFront 0
#define DEBUG_popBack 0
struct listnode
{
int value;
struct listnode *next;
struct listnode *prev;
};
struct queue
{
int size;
struct listnode *head;
struct listnode *tail;
};
struct queue *queue_create()
{
struct queue *buffer = malloc(sizeof(*buffer));
if (buffer == NULL)
exit(EXIT_FAILURE);
buffer->size = 0;
buffer->head = NULL;
buffer->tail = NULL;
printf("Queue Created!\n");
return buffer;
};
void queue_delete(struct queue *q)
{
struct listnode *buffer = q->head;
struct listnode *next = NULL;
while (buffer)
{
next = buffer->next;
free(buffer);
buffer = next;
}
free(q);
printf("Queue Deleted!\n");
}
int pushBack(struct queue *q, int value)
{
struct listnode *buffer = malloc(sizeof(*buffer));
if (buffer == NULL)
return 1;
buffer->value = value;
buffer->next = NULL;
buffer->prev = q->tail;
if (q->tail)
q->tail->next = buffer;
q->tail = buffer;
if (q->head == NULL)
q->head = buffer;
q->size++;
if (DEBUG_pushBack)
printf("\t\tpushBack: %d\n", value);
return 0;
}
int queue_size(struct queue *q)
{
return q->size;
}
void queue_print(struct queue *q)
{
struct listnode *buffer = q->head;
while (buffer)
{
printf("%d ", buffer->value);
buffer = buffer->next;
}
printf("\n");
}
struct listnode *get_pos(struct queue *q)
{
struct listnode *buffer = q->head;
int i = 0;
int index = 0 + (rand() % q->size);
printf("Pos = %d\n", index);
do
{
buffer = buffer->next;
i++;
} while (buffer && (i != index));
return 0;
}
int queue_enqueue(struct queue *q, int value)
{
struct listnode *pos = NULL;
pos = get_pos(q);
if (pos == NULL)
return 1;
struct listnode *buffer = malloc(sizeof(*buffer));
buffer->value = value;
buffer->next = pos;
buffer->prev = pos->prev;
pos->prev->next = buffer;
q->size++;
return 0;
}
int queue_dequeue(struct queue *q)
{
struct listnode *pos = NULL;
pos = get_pos(q);
if (pos == NULL)
return 1;
struct listnode *buffer;
buffer = pos->next;
pos->prev->next = buffer;
pos->next->prev = buffer->prev;
free(pos);
q->size--;
return 0;
}
double mtime()
{
struct timeval t;
gettimeofday(&t, NULL);
return (double)t.tv_sec * 1000 + t.tv_usec / 1000;
}
int myFunc(struct queue *q, int i)
{
// printf("I = %d\n", i);
int random = 1 + (rand() % 3);
switch (random)
{
case 1:
{
// printf("Random I\n");
pushBack(q, i);
break;
}
case 2:
{
// printf("Random II\n");
queue_enqueue(q, i);
break;
}
case 3:
{
// printf("Random III\n");
queue_dequeue(q);
break;
}
}
return 0;
}
int main(int argc, char const **argv)
{
struct queue *q;
double t;
int i;
q = queue_create();
printf ("Start test.\n");
t = mtime();
for (i = 0; i < 100000000; i++)
myFunc(q, i);
t = mtime() - t;
t /= 1000;
printf ("It took me %f seconds.\n", t);
//queue_print(q);
queue_delete(q);
system("pause");
exit(EXIT_SUCCESS);
} |
|
Добавлено через 9 минут
Всё разобрался, поспешил с созданием темы, просто забыл поставить проверку на пустоту очереди. Тему можно закрывать.