Skip to content

Comment on Making a StringBuffer in C, and questioning my sanity

Comments

There is a way for this not to terminate:

  while (new_capacity < required) {
    new_capacity *= 2;
  }
1. All variables are unsigned (due to being size_t). So we don't worry about overflow UB.

2. new_capacity * 2 always produces an even number, whether truncating or not.

3. Supppose required is SIZE_MAX, the highest value of size_t; note that this is an odd number.

4. Therefore new_capacity * 2 is always < required; loop does not terminate.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.