What is the correct way to use free()
in this case, where ls_options
will contain several ls_buffer_send
?
char **ls_options = (char**) malloc (200*sizeof(char));
char *ls_buffer_send = (char*) malloc (350*sizeof(char));
What is the correct way to use free()
in this case, where ls_options
will contain several ls_buffer_send
?
char **ls_options = (char**) malloc (200*sizeof(char));
char *ls_buffer_send = (char*) malloc (350*sizeof(char));
Assuming that the allocation is the way you really need it, you do not have to complicate it, just use a simple free()
:
free(ls_options);
free(ls_buffer_send);
Leverages and simplifies allocation:
char **ls_options = malloc(200);
char *ls_buffer_send = malloc(350);
Scroll through the vector by releasing the memory for each active element.
As you are declaring a fixed size for the varable should be done something like this:
int i;
for(i=0; i<200; i++){
if(ls_options[i] != NULL){
free(ls_options[i]);
}
}
free(ls_options);
But for this to work properly all elements must be set to NULL
at startup of ls_options