I have a struct which i am storing an array of integers defined as:
typedef struct {
int* numArr;
int counter;
}NumberArr;
I have initialised it to be: (int*)malloc(sizeof(int))
and reallocated when i need to add a number using: num.numArr = realloc(num.numArr, sizeof(int) * (counter 1));
I am having issue with calling free for this struct. I have tried:
void free_data(NumberArr num) {
for (int i = 0; i < num.counter; i ) {
free(num.numArr[i]);
}
free(num.numArr);
}
When I do this i get an error message, saying it isn't type void pointer. but i dont know what to do to make it one so I can free it.
CodePudding user response:
This is because numArr
is a single pointer (*
). You just need to free it once. Your function free_date
parameter should be a pointer to NumberArr
, and then just pass the address of your variable like &var
.
void free_data(NumberArr *num) {
free(num->numArr);
}
The concept you are using is used while freeing double-pointer int **
.