Skip to content

Commit 0c1b410

Browse files
committed
prevector: switch to new/delete rather than malloc/free
1 parent 3360ef3 commit 0c1b410

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/prevector.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,19 @@ class prevector {
165165
T* src = indirect;
166166
T* dst = direct_ptr(0);
167167
memcpy(dst, src, size() * sizeof(T));
168-
free(indirect);
168+
delete[] indirect;
169169
_size -= N + 1;
170170
}
171171
} else {
172+
char* new_indirect = reinterpret_cast<char*>(new T[new_capacity]);
172173
if (!is_direct()) {
173-
_union.indirect = static_cast<char*>(realloc(_union.indirect, ((size_t)sizeof(T)) * new_capacity));
174+
T* old_indirect = reinterpret_cast<T*>(_union.indirect);
175+
memcpy(new_indirect, old_indirect, size() * sizeof(T));
176+
_union.indirect = new_indirect;
174177
_union.capacity = new_capacity;
178+
delete[] old_indirect;
175179
} else {
176-
char* new_indirect = static_cast<char*>(malloc(((size_t)sizeof(T)) * new_capacity));
177-
T* src = direct_ptr(0);
178-
T* dst = reinterpret_cast<T*>(new_indirect);
179-
memcpy(dst, src, size() * sizeof(T));
180+
memcpy(new_indirect, direct_ptr(0), size() * sizeof(T));
180181
_union.indirect = new_indirect;
181182
_union.capacity = new_capacity;
182183
_size += N + 1;
@@ -428,7 +429,7 @@ class prevector {
428429
~prevector() {
429430
clear();
430431
if (!is_direct()) {
431-
free(_union.indirect);
432+
delete[] reinterpret_cast<T*>(_union.indirect);
432433
_union.indirect = NULL;
433434
}
434435
}

0 commit comments

Comments
 (0)