//char char **p; declared in .h file
size_t bs = 5;
size_t Size = sizeof(obj);
p = (char**)malloc(bs);
for (size_t i = 0; i < bs;i++){p[i] = (char*)malloc(Size);}
for (size_t j = 0; j < bs-1; j ++){p[j] = &(p[j + 1][0]); }
for (size_t i = 0; i < bs; i++){free(p[i]);}
free(p);
my code stalls up when trying to free the last element of p in the for loop. Anyone what i might be doing wrong?
EDIT: I still have the same problem even when changing it to (char*)malloc(bs sizeof(char *));
this still does not work:
size_t bs = 5;
size_t Size = sizeof(obj);
p = (char**)malloc(bs* sizeof(char *));
for (size_t i = 0; i < bs;i++){p[i] = (char*)malloc(Size);}
for (size_t j = 0; j < bs-1; j ++){p[j] = &(p[j + 1][0]); }
for (size_t i = 0; i < bs; i++){free(p[i]);}
free(p);
using new instead of malloc does not solve the issue either
However this code frees up the memory fine.
size_t bs = 5;
size_t Size = sizeof(obj);
p = (char**)malloc(bs* sizeof(char *));
for (size_t i = 0; i < bs;i++){p[i] = (char*)malloc(Size);}
for (size_t i = 0; i < bs; i++){free(p[i]);}
free(p);
so the problem seem to be something with this piece of code
for(size_t j = 0; j < bs-1; j ++){p[j] = &(p[j + 1][0]); }
I want this to be an implicit linked list, anyone know have an idea what i am doing wrong?