Code Room
Code reviewHardcr-g373
Subject Memory safetyLevel Senior–Staff~22 minCommon in Code quality & review interviewsIndustries Software development

Question

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.

Talk through your review
Code to reviewc
int buf_grow(Buffer *b, size_t need) {    size_t newcap = b->cap ? b->cap * 2 : 16;    while (newcap < need) newcap *= 2;    b->data = realloc(b->data, newcap);   // grow in place    if (!b->data) {        free(b->data);        return -1;    }    b->cap = newcap;    return 0;}
Run or narrate your approach, then ask the coach.