3 ms·
The trailing 0 char?
by akomtu 3y ago
The trailing 0 char?
- tedunangst 3y agoIn theory, it should be (len - 1) x 2 + 1 in that case.
- jltsiren 3y agoWith an allocation of size n, you have capacity for n-1 characters and the trailing 0, and the trailing 0 does not count as a character. If you double the size of the allocation to 2n, the capacity increases to 2(n-1) + 1.
- tedunangst 3y agoSo why does it increase the size of the allocation to 2n + 1?
- jltsiren 3y agoAllocation size is capacity + 1. If you double the allocation size to 2 * (capacity + 1), the new capacity is 2 * capacity + 1.
- anonymoushn 3y agoSurely 2 * (capacity + 1) = 2 * capacity + 2
- jltsiren 3y agoThat is the new allocation size. The new capacity is one character less. The allocation must include space for the trailing 0, which is not a part of the string and is hence not included in the length/capacity numbers.
- anonymoushn 3y agoI see, thanks for your patience :)
- ace2358 3y agoGreat thread, I was scratching me head a bit too.