Answer the question
In order to leave comments, you need to log in
How to free memory in an array of structures in C?
I apologize in advance if some words sound crooked, as I am learning programming from foreign resources and do not always know the analogue in Russian.
There is a function that initializes an array consisting of structures. The function works, but Valgrid throws the error "12 bytes are definitely lost ..."
void init_product(struct product *pr, const char *title, const char *code,
int stock, double price)
{ char *temp;
temp = title;
temp = malloc(strlen(title) + 1);
if (temp == NULL){
return NULL;
}
strcpy(temp, title);
title = temp;
pr->title = title;
int i = 0;
while (*code) {
pr->code[i] = (*code);
code++;
i++;
if (i == 7)
break;
}
pr->code[i] = '\0';
pr->stock = stock;
pr->price = price;
}
int main()
{
struct product p;
init_product(&p, "test", "1234", 1, 0.50);
}
free(p.title)
Does not help. Thanks in advance.
Answer the question
In order to leave comments, you need to log in
There is a function that initializes an array consisting of structures.
I suspect the problem is here:
title = temp;
pr->title = title;
Do pr->title = temp; and check.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question