Ошибка сегментации при освобождении памяти
25.12.2010, 18:14. Показов 993. Ответов 1
Вот в упор не понимаю, почему программа вылетает уже на завершающем этапе. Пожалуйста, посмотрите, что не так
main.cpp
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
| // для тестирования
#include <iostream>
#include "vbytearray.h"
using namespace std;
int main()
{
cout << "\n******** Test VByteArray ********\n";
VByteArray ba;
VByteArray by("data");
VByteArray vv("data", 3);
cout << ba << ' ' << by << ' ' << vv << '\n';
cout << vstrcmp(by, "data") << '\n';
cout << by.append(" dataX") << '\n';
VByteArray free("free");
VByteArray dom("dom");
free.append(dom); // free == "freedom"
cout << free.append('!') << '\n';
cout << free.remove(4, 3) << '\n';
cout << free.replace(0, 2, VByteArray("b")) << '\n';
VByteArray mont("Montreal");
cout << mont.remove(1, 4) << '\n';
cout << mont.insert(1, VByteArray("ontr")) << '\n';
cout << mont.insert(4, 'V') << '\n';
cout << mont.replace(VByteArray("Mont"), VByteArray("MVVVfONT")) << '\n';
cout << "\n******** End Test VByteArray ********\n";
return 0;
} |
|
vbytearray.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
| #include <ctype.h>
#include <assert.h>
#include "vbytearray.h"
VByteArray::VByteArray()
{
printf("***Construc VByteArray()\n");
d = 0;
reallocData(0);
}
VByteArray::VByteArray(const char *str)
{
printf("***Construc VByteArray(const char *str)\n");
d = 0;
reallocData(vstrlen(str));
::memcpy(d->str, str, d->size);
d->str[d->size] = '\0';
}
VByteArray::VByteArray(const char *data, int size)
{
printf("***Construc VByteArray(const char *data, int size)\n");
d = 0;
reallocData(size);
memcpy(d->str, data, size);
d->str[size] = '\0';
}
VByteArray::VByteArray(int size, char ch)
{
printf("***Construc VByteArray(int size, char ch)\n");
d = 0;
fill(ch, size);
d->str[d->size] = '\0';
}
VByteArray::VByteArray(const VByteArray &other)
{
printf("***Construc of copy\n");
d = 0;
reallocData(other.d->size);
::memcpy(d->str, other.d->str, other.d->size);
d->str[d->size] = '\0';
}
VByteArray::~VByteArray()
{
printf("***Destroy %s\n", d->str);
clear();
}
void VByteArray::clear()
{
vFree(d);
d = 0;
}
// служебная функция для выделения/перевыделения памяти
void VByteArray::reallocData(int size)
{
if(!d) // ещё не выделяли
{
d = static_cast<VByteArrayData*>(vMalloc(sizeof(VByteArrayData)+size*sizeof(char)+1));
// check pointer
d->size = d->alloc = size;
d->str = d->array;
}
else if(size > d->alloc)
{
VByteArrayData *x = static_cast<VByteArrayData*>(vMalloc(sizeof(VByteArray)+size*sizeof(char)+1));
x->size = x->alloc = size;
x->str = x->array;
::memcpy(x->str, d->str, size*sizeof(char));
vFree(d);
d = x;
}
else if(size <= d->alloc)
{
d->str[size] = '\0';
d->size = size;
}
}
VByteArray &VByteArray::append(const char *str)
{
return append(str, vstrlen(str));
}
VByteArray &VByteArray::append(const VByteArray &ba)
{
return append(ba.d->str, ba.d->size);
}
VByteArray &VByteArray::append(const char *str, int len)
{
int oldsize = d->size;
reallocData(len + oldsize); // d->size меняется
::memcpy(d->str+oldsize, str, len);
d->str[d->size] = '\0';
return *this;
}
VByteArray &VByteArray::append(char ch)
{
reallocData(d->size + 1);
d->str[d->size-1] = ch;
d->str[d->size] = '\0';
return *this;
}
char VByteArray::at(int i) const
{
if(i < 0 || i > d->size)
{
printf("Warning: i < 0 or i > size");
return 0;
}
return d->str[i];
}
char &VByteArray::operator[](int i)
{
if(i < 0 || i > d->size)
{
printf("Error: i < 0 or i > d.size\n");
assert(1);
}
return d->str[i];
}
char &VByteArray::operator[](uint i)
{
return operator[](int(i));
}
VByteArray &VByteArray::setRawData(const char *data, uint size)
{
d = 0;
reallocData(size+1);
::memcpy(d->str, data, d->size);
d->str[d->size-1] = '\0';
return *this;
}
VByteArray &VByteArray::operator=(const char *str)
{
return setRawData(str, vstrlen(str));
}
void VByteArray::chop(int n)
{
if(n < 0 || n > d->size) // очищаем всю строку
{
printf("Warning: n < 0 or n > size");
clear();
return;
}
reallocData(d->size-n);
}
int VByteArray::indexOf(const VByteArray &ba, int from) const
{
return indexOf(ba.d->str, from);
}
int VByteArray::indexOf(const char *str, int from) const
{
int len = vstrlen(str);
for(; from < d->size; from++)
{
if(!vstrncmp(d->str+from, str, len))
return from;
}
return -1;
}
int VByteArray::indexOf(char ch, int from) const
{
for(; from < d->size; from++)
{
if(ch == d->str[from])
return from;
}
return -1;
}
VByteArray &VByteArray::remove(int pos, int len)
{
::memmove(d->str+pos, d->str+pos+len, d->size-pos-len);
reallocData(d->size-len);
return *this;
}
VByteArray &VByteArray::replace(int pos, int len, const char *after, int alen)
{
remove(pos, len);
insert(pos, after, alen);
return *this;
}
VByteArray &VByteArray::replace(const VByteArray &before, const VByteArray &after)
{
int pos = indexOf(before);
while(pos != -1)
{
replace(pos, before.d->size, after.d->str, after.d->size);
pos = indexOf(before, pos+1);
}
return *this;
}
VByteArray &VByteArray::replace(const char *before, const VByteArray &after)
{
int pos = indexOf(before);
int len = vstrlen(before);
while(pos != -1)
{
replace(pos, len, after.d->str, after.d->size);
pos = indexOf(before, pos+1);
}
return replace(pos, vstrlen(before), after.d->str, after.d->size);
}
VByteArray &VByteArray::replace(const char *before, int bsize, const char *after, int asize)
{
return replace(VByteArray(before, bsize), VByteArray(after, asize));
}
VByteArray &VByteArray::replace(char before, const char *after)
{
int pos = indexOf(before);
while(pos != -1)
{
remove(pos, 1);
insert(pos, before);
pos = indexOf(before, pos+1);
}
return *this;
}
VByteArray &VByteArray::replace(char before, char after)
{
int pos = indexOf(before);
while(pos != -1)
{
remove(pos, 1);
insert(pos, before);
pos = indexOf(before, pos+1);
}
return *this;
}
VByteArray &VByteArray::insert(int i, const char *str, int len)
{
int oldsize = d->size;
reallocData(oldsize + len);
::memmove(d->str+i+len, d->str+i, oldsize-i);
::memcpy(d->str+i, str, len);
return *this;
}
VByteArray &VByteArray::insert(int i, char ch)
{
reallocData(d->size + 1);
::memmove(d->str+i+1, d->str+i, d->size-1);
d->str[i] = ch;
return *this;
}
VByteArray VByteArray::mid(int pos, int len) const
{
int lenth = (len == -1 ? d->size - pos : len);
VByteArray ret(lenth, '\0');
for(int i=0; i<lenth; i++, pos++)
ret[i] = d->str[pos];
return ret;
}
VByteArray &VByteArray::fill(char ch, int size)
{
if(size != -1)
reallocData(size);
memset(d->str, ch, d->size);
return *this;
}
VByteArray VByteArray::left(int len) const
{
return VByteArray(d->str, len);
}
VByteArray VByteArray::toUpper() const
{
VByteArray ret(d->size, '\0');
for(int i=0; i<d->size; i++)
ret[i] = toupper(d->str[i]);
return ret;
}
VByteArray VByteArray::toLower() const
{
VByteArray ret(d->size, '\0');
for(int i=0; i<d->size; i++)
ret[i] = tolower(d->str[i]);
return ret;
}
char *vstrcpy(char *dst, const char *src)
{
if(!src) return 0;
return strcpy(dst, src);
}
char *vstrdup(const char *src)
{
if(!src) return 0;
char *dst = new char[vstrlen(src)+1];
return vstrcpy(dst, src);
}
char *vstrncpy(char *dst, const char *src, uint len)
{
if(!dst || !src)
return 0;
strncpy(dst, src, len);
if(len > 0)
dst[len-1] = '\0';
return dst;
}
int vstrcmp(const VByteArray &str1, const VByteArray &str2)
{
int l1 = str1.length();
int l2 = str2.length();
int ret = memcmp(str1, str2, vMin(l1, l2));
if (ret != 0)
return ret;
return l1 - l2;
}
int vstrcmp(const VByteArray &str1, const char *str2)
{
int l1 = str1.length();
int l2 = vstrlen(str2);
int ret = memcmp(str1, str2, vMin(l1, l2));
if(ret != 0)
return ret;
return l1 - l2;
}
int vstricmp(const char *str1, const char *str2)
{
register const uchar *s1 = reinterpret_cast<const uchar *>(str1);
register const uchar *s2 = reinterpret_cast<const uchar *>(str2);
int res;
uchar c;
if (!s1 || !s2)
return s1 ? 1 : (s2 ? -1 : 0);
for (; !(res = (c = tolower((ushort)*s1)) - tolower((ushort)*s2)); s1++, s2++)
if (!c) // strings are equal
break;
return res;
}
int vstrnicmp(const char *str1, const char *str2, uint len)
{
register const uchar *s1 = reinterpret_cast<const uchar *>(str1);
register const uchar *s2 = reinterpret_cast<const uchar *>(str2);
int res;
uchar c;
if (!s1 || !s2)
return s1 ? 1 : (s2 ? -1 : 0);
for (; len--; s1++, s2++)
{
if ((res = (c = tolower((ushort)*s1)) - tolower((ushort)*s2)))
return res;
if (!c) // strings are equal
break;
}
return 0;
} |
|
vbytearray.h
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
| #ifndef VBYTEARRAY_H
#define VBYTEARRAY_H
#include <string.h>
#include <stdio.h>
#include <iostream>
#include "vglobal.h"
class VByteArray;
/* безопасные функции для работы со строками; расширение для string.h */
inline uint vstrlen(const char *str)
{ return str ? uint(strlen(str)) : 0; }
char *vstrcpy(char *dst, const char *src);
char *vstrdup(const char *src);
inline uint qstrnlen(const char *str, uint maxlen)
{
uint length = 0;
if (str)
{
while (length < maxlen && *str++)
length++;
}
return length;
}
char *vstrncpy(char *dst, const char *src, uint len);
int vstrcmp(const VByteArray &str1, const VByteArray &str2);
int vstrcmp(const VByteArray &str1, const char *str2);
inline int vstrcmp(const char *str1, const VByteArray &str2)
{ return -vstrcmp(str2, str1); }
inline int vstrncmp(const char *str1, const char *str2, uint len)
{
return (str1 && str2) ? strncmp(str1, str2, len)
: (str1 ? 1 : (str2 ? -1 : 0));
}
inline int vstrcmp(const char *str1, const char *str2)
{ return vstrncmp(str1, str2, vstrlen(str2)); }
int vstricmp(const char *str1, const char *str2);
int vstrnicmp(const char *str1, const char *str2, uint len);
class VByteArray
{
public:
VByteArray();
VByteArray(const char *str);
VByteArray(const char *data, int size);
VByteArray(int size, char ch);
VByteArray(const VByteArray &other);
~VByteArray();
char at(int i) const;
VByteArray &append(const VByteArray &ba);
VByteArray &append(const char *str);
VByteArray &append(const char *str, int len);
VByteArray &append(char ch);
int indexOf(const VByteArray &ba, int from = 0) const;
int indexOf(const char *str, int from = 0) const;
int indexOf(char ch, int from = 0) const;
VByteArray mid(int pos, int len = -1) const;
VByteArray &remove(int pos, int len);
VByteArray &replace(int pos, int len, const VByteArray & after)
{ return replace(pos, len, after.d->str, after.d->size); }
VByteArray &replace(int pos, int len, const char * after)
{ return replace(pos, len, after, vstrlen(after)); }
VByteArray &replace(int pos, int len, const char * after, int alen);
VByteArray &replace(const VByteArray &before, const VByteArray &after);
VByteArray &replace(const char * before, const VByteArray & after);
VByteArray &replace(const char * before, int bsize, const char *after, int asize);
VByteArray &replace(const VByteArray &before, const char *after)
{ return replace(before, VByteArray(after)); }
VByteArray &replace(const char *before, const char *after)
{ return replace(VByteArray(before), VByteArray(after)); }
VByteArray &replace(char before, const VByteArray &after)
{ return replace(before, after.d->str); }
VByteArray &replace(char before, const char *after);
VByteArray &replace(char before, char after);
VByteArray &insert(int i, const VByteArray &ba)
{ return insert(i, ba.d->str, ba.d->size); }
VByteArray &insert(int i, const char *str)
{ return insert(i, str, vstrlen(str)); }
VByteArray &insert(int i, const char *str, int len);
VByteArray &insert(int i, char ch);
VByteArray &fill(char ch, int size = -1);
VByteArray left(int len) const;
VByteArray right(int len) const;
VByteArray toLower() const;
VByteArray toUpper() const;
VByteArray &setRawData(const char *data, uint size);
void clear();
void chop(int n);
char *data() { return d->str; }
const char *data() const { return d->str; }
const char *constData() const { return d->str; }
bool isEmpty() const { return d->size == 0; }
int length() const { return d->size; }
char &operator[](int i);
char &operator[](uint i);
char operator[](int i) const
{ return at(i); }
char operator[](uint i) const
{ return at(i); }
operator const void *() const { return d->str; }
operator const char *() const { return d->str; }
VByteArray &operator=(const char *str);
VByteArray &operator+=(const VByteArray &ba)
{ return append(ba); }
VByteArray &operator+=(const char *str)
{ return append(str); }
VByteArray &operator+=(char ch)
{ return append(ch); }
friend std::ostream &operator<<(std::ostream &os, const VByteArray & ba)
{
os << ba.d->str;
return os;
}
private:
void reallocData(int size);
struct VByteArrayData
{
int size;
int alloc;
char *str;
char array[1];
};
VByteArrayData *d;
};
#endif |
|
vglobal.h
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #ifndef VGLOBAL_H
#define VGLOBAL_H
#include <stdlib.h>
// define types
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned long ulong;
template <class T> T vMin(T t1, T t2) { return t1 < t2 ? t1 : t2; }
template <class T> T vMax(T t1, T t2) { return t1 > t2 ? t1 : t2; }
inline void *vMalloc(size_t size)
{ return ::malloc(size); }
inline void *vRealloc(void *ptr, size_t size)
{ return ::realloc(ptr, size); }
inline void vFree(void *ptr)
{ ::free(ptr); }
#endif |
|
Если это запустить, то в конце вылетает ошибка сегментации. Да, кода очень много, но я буду очень благодарен за любую помощь.
Добавлено через 1 час 48 минут
Вот ошибка
Код
*** glibc detected *** ./a.out: free(): invalid next size (fast): 0x0000000000605160 ***
Не понимаю, как от неё избавиться
0
|