In order to solidify concepts from the lectures, I usually run some of the source code in my ide and play around with it a little bit.
During lecture 5 around the 55 minute mark, Professor Malan changes malloc to realloc, and gets rid of the for loop under it.
When I try running this code exactly as shown in the lectures, it returns
0
0
32808976
0
free(): double free detected in tcache 2
Aborted (core dumped)
Curious what caused this error message.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int *list = malloc(3 * sizeof(int));
if (list == NULL)
{
return 1;
}
list[0] = 1;
list[1] = 2;
list[2] = 3;
int *tmp = realloc(list, 4 * sizeof(int));
if (tmp == NULL)
{
free(list);
return 1;
}
tmp[3] = 4;
free(list);
list = tmp;
for (int i = 0; i < 4; i++)
{
printf("%i\n", list[i]);
}
free(list);
}