typedef struct partition_struct {
int* elements;
int last; //last element index
int dim; //n allocated int
} partition;
typedef struct partitions_struct {
partition* partitions;
int last; //last partition index
int dim; //n allocated partition
} partitions;
why the free function of partition("free_p") gives a "munmap_chunk(): invalid pointer" error on free(p) line when called the second time from "free_ps"?.
void free_p(partition* p){
if(p==NULL) return;
if(p->elements) free(p->elements);
free(p); //<--Gives error here
}
void free_ps(partitions* ps){
if(ps==NULL) return;
for(int i=0; i<=ps->last; i++){
free_p(&ps->partitions[i]);
}
if(ps->partitions) free(ps->partitions);
free(ps);
}
Is there any correct way to free struct partitions_struct ?
I've tried to remove the free(p) when freeing the partitions struct but im concerned about mem leaks this below is the code that is working:
void free_ps(partitions* ps){
if(ps==NULL) return;
for(int i=0; i<=ps->last; i++){
free(ps->partitions[i].elements);
}
if(ps->partitions) free(ps->partitions);
free(ps);
}
entra functions for reference:
partition* partition_factory(int size){
partition* new = (partition*) malloc(sizeof(partition) * 1);
new->dim = size;
new->elements = (int*) malloc(sizeof(int) * size);
new->last = -1;
return new;
}
partitions* partitions_factory(int size){
partitions* new = (partitions*) malloc(sizeof(partitions) * 1);
new->dim = size;
new->partitions = (partition*) malloc(sizeof(partition) * size);
new->last = -1;
return new;
}
void add_el_to_p(partition* p, int el){
if((p->dim)-1 == p->last){
int new_size = p->dim * 2;
int* new_elements = (int*)realloc(p->elements, new_size * sizeof(int));
if(new_elements==NULL) {
exit(1);
} else {
p->dim = new_size;
p->elements = new_elements;
}
}
p->last++;
p->elements[p->last] = el;
}
void add_p_to_ps(partitions* ps, partition* p){
if((ps->dim)-1 == ps->last){
int new_size = ps->dim * 2;
partition* new_partitions = (partition*)realloc(ps->partitions, new_size * sizeof(partition));
if(new_partitions==NULL) {
exit(1);
} else {
ps->dim = new_size;
ps->partitions = new_partitions;
}
}
ps->last++;
ps->partitions[ps->last] = *p;
}
free(p);. It wasn't allocated on its own, so don't free it on its own.