I managed to narrow my problem to this code snippet:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
void **arr;
int max = 10; //tr = 0;
int size = sizeof(int);
arr = malloc(max*sizeof(void*));
for (int i=0; i<=6; i++)
{
arr[i] = malloc(size);
memcpy(arr+i, &i, size);
}
for (int i=0; i<=6; i++)
{
printf("%d ", *(int*)(arr + i));
}
free(arr[0]);
free(arr[1]);
free(arr);
return 0;
}
It's ok when I free arr
, and it's ok when I free arr[0]
. But anything other than that I get a segfault.
Am I doing the allocations wrong? Or is it something else?
should be
for (int i=0; i<=6; i++)
{
arr[i] = malloc(size);
memcpy(arr[i], &i, size);
}
for (int i=0; i<=6; i++)
{
printf("%d ", *(int*)(arr[i]));
}