In preparation for my final project, I am writing a C program that handles task lists. It utilizes a nested struct. For every line it reads from a file it creates a struct of type task
. Each task
then is being put into a struct of type tasklist_container
.
My problem is that I cannot free the memory that I malloc'd like this
task* new_task = malloc(sizeof(task));
and
tasklist_container* main_tasklist = malloc(sizeof(tasklist_container));
Any ideas?
Typedefs for the mentioned structs:
// definition of task
typedef struct task
{
int id;
bool completed;
int priority;
char description[TASKLENGTH+1];
int context[TASKLISTLENGTH/WORDLENGTH];
int number_of_contexts;
int project[TASKLISTLENGTH/WORDLENGTH];
int number_of_projects;
}
task;
// definition of tasklist container
typedef struct tasklist_container
{
task* list[TASKLISTLENGTH];
int number_of_tasks;
}
tasklist_container;
Update 0:
I've got a follow-up question. My program creates a task
like
task* new_task = malloc(sizeof(task));
and then nests the new task
into a tasklist_container
with
tasklist_container tasklist = malloc(sizeof(tasklist_container));
tasklist -> list[tasklist -> number_of_tasks] = new_task;
My Question: How do I free the firstly malloc()'d memory that new_task
points to?
My understanding is that new_task
and tasklist->list[]
point to the same block of memory. Freeing new_task
would also free tasklist->list[]
. Is that correct?