As per my understanding realloc may be extending block of already given memory (if it is possible) or, otherwise, allocate a completely new block of memory and copy given input. Can someone explain why the below code is throwing an double free error?
void test(int *b) {
b = realloc(b, 10 * sizeof(int));
b[0] = 1;
}
int main(int argc, char const *argv[])
{
int *a = malloc(4 * sizeof(int));
printf("%d\n", a[0] );
test(a);
printf("%d\n", a[0] );
free(a);
return 0;
}
Output:
$ ./a.out
0
0
free(): double free detected in tcache 2
Aborted (core dumped)