To answer the question with the function as-is, temp_len+1
is not large enough because you increment temp_len
in this code:
substrings[num_substrings] = (char *) realloc(substrings[num_substrings],
(temp_len + 1) * sizeof(char)); // Allocation here
// ...
temp_len++;
substrings[num_substrings][temp_len] = '\0'; // Invalid write 1 byte after
But I concur with using std::string
or std::vector
.