News for hackers. Stuff that matters.
Re:Appending to an std:string character-by-character: how do... by tedunangst on Thursday October 26, 2023 @07:32PM (#38032997)
In theory, it should be (len - 1) x 2 + 1 in that case.
Re:Appending to an std:string character-by-character: how do... by jltsiren on Thursday October 26, 2023 @08:24PM (#38033314)
With 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.
Re:Appending to an std:string character-by-character: how do... by tedunangst on Thursday October 26, 2023 @09:23PM (#38033655)
So why does it increase the size of the allocation to 2n + 1?
Re:Appending to an std:string character-by-character: how do... by jltsiren on Thursday October 26, 2023 @10:05PM (#38033868)
Allocation size is capacity + 1. If you double the allocation size to 2 * (capacity + 1), the new capacity is 2 * capacity + 1.
Re:Appending to an std:string character-by-character: how do... by anonymoushn on Friday October 27, 2023 @12:41AM (#38034766)
Surely 2 * (capacity + 1) = 2 * capacity + 2
Re:Appending to an std:string character-by-character: how do... by jltsiren on Friday October 27, 2023 @01:38AM (#38035064)
That 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.
Re:Appending to an std:string character-by-character: how do... by anonymoushn on Friday October 27, 2023 @02:27AM (#38035309)
I see, thanks for your patience :)
Re:Appending to an std:string character-by-character: how do... by ace2358 on Friday October 27, 2023 @05:31AM (#38036375)
Great thread, I was scratching me head a bit too.