6 ms·
a.capacity <<= 1u; decltype(a.data) tmp = (decltype(a.data)) realloc(a.data, sizeof(a.data[0]) * a.capacity); assert(tmp != NULL); That's not ideal. Imagin
by halayli 4y ago
a.capacity <<= 1u;
decltype(a.data) tmp = (decltype(a.data)) realloc(a.data, sizeof(a.data[0]) * a.capacity);
assert(tmp != NULL);
That's not ideal. Imagine you are at 32GB capacity, the next realloc will ask for 64GB which is pretty excessive.
- euclaise 4y agoMy understanding is that this is the typical behavior of dynamic arrays
- rurban 4y agoOnly for bad dynamic array implementations. Good ones might resize by the golden ratio, and enlarge by blocksize if larger
- eqvinox 4y agoThis isn't suitable for arrays that large regardless of the resizing multiplier. realloc() generally allocates a new block of memory of the new size, copies the entire content over, and then frees the old block. Only sometimes you get to be lucky and have enough spare room after the existing data in the virtual address map to not need that copy. By the point this copying becomes relevant, a continuous dynamic array like this becomes fundamentally the wrong data structure.