Code RoomBuffer grow fails to allocate
HardPrep Room Coding #2001

Buffer grow fails to allocate

Code reviewCode quality & reviewSenior–Staff~22 min

Review this C dynamic buffer grow routine.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 22 min
Mark a line and say what kind of problem it is.0 findings
1int buf_grow(Buffer *b, size_t need) {
2 size_t newcap = b->cap ? b->cap * 2 : 16;
3 while (newcap < need) newcap *= 2;
4 b->data = realloc(b->data, newcap); // grow in place
5 if (!b->data) {
6 free(b->data);
7 return -1;
8 }
9 b->cap = newcap;
10 return 0;
11}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.