R
riccardo
Hi,
I'm wondering what the best practice is when, within a function you need
to know if a variable was allocated statically or dynamically, in order
to eventually free it.
I'm dealing with a scheduler that consume tasks. Once a task is
consumed, the scheduler has to release the memory owned by that task,
and this is a responsibility of the scheduler only (not the one entity
that allocated the task).
Three approaches come to my mind:
1) Documentation: document the function to add a new task to the
scheduler, requesting the input argument to be a dynamically allocated task.
2) Insert a flag in the task data structure to signal its origin (static
vs dynamic).
3) malloc a task and copy data into that task. All tasks shall need to
be free @removal.
I'm currently using 3 as it seems the cleaner ans safer solution; is
this correct? Is there any other way?
RM
I'm wondering what the best practice is when, within a function you need
to know if a variable was allocated statically or dynamically, in order
to eventually free it.
I'm dealing with a scheduler that consume tasks. Once a task is
consumed, the scheduler has to release the memory owned by that task,
and this is a responsibility of the scheduler only (not the one entity
that allocated the task).
Three approaches come to my mind:
1) Documentation: document the function to add a new task to the
scheduler, requesting the input argument to be a dynamically allocated task.
2) Insert a flag in the task data structure to signal its origin (static
vs dynamic).
3) malloc a task and copy data into that task. All tasks shall need to
be free @removal.
I'm currently using 3 as it seems the cleaner ans safer solution; is
this correct? Is there any other way?
RM