Pointer invalidated by realloc
Review this C dynamic-array append.
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 30 min
Mark a line and say what kind of problem it is.0 findings
1#include <stdlib.h>
2
3typedef struct { int *data; size_t len, cap; } Vec;
4
5int *vec_push(Vec *v, int value) {
6 if (v->len == v->cap) {
7 v->cap = v->cap ? v->cap * 2 : 4;
8 v->data = realloc(v->data, v->cap * sizeof(int));
9 }
10 v->data[v->len] = value;
11 return &v->data[v->len++];
12}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.