Did you read the docs on realloc, in particular
Reallocates the given area of memory. It must be previously allocated by malloc(), calloc() or
realloc()and not yet freed with a call to free orrealloc. Otherwise, the results are undefined.
https://en.cppreference.com/w/c/memory/realloc
I can’t seem to find where you explicitly allocate memory in your code