I'm learning C and want to create a simple function which keeps track of a list of paths.
I start with a default path ["/bin/"] and have a function "change_paths" which updates this list based on some input, so for example ["/bin/"] -> ["/temp/", "/auxillary/"].
However, I encounter problems when I try to free my memory at the end. Specifically, fsanitize tells me that the 6 bytes from my default PATH paths[0] = malloc(6 * sizeof(char)); are not freed properly.
What should I do to correctly free this memory if I use change_paths arbitrarily many times with arbitrarily many inputs (including possibly 0 new PATHs)? Is this structure appropriate for storing data with this mechanism?
Note: I've edited this code for brevity, so even though the line "char** new_paths = {"/temp/", "/auxillary/"};" is not technically correct, please think of it as working. I just want to give an example and I'm not sure how to define a char** manually.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void change_paths(int *num_paths, char*** paths, int num_new_paths, char** new_paths){
*paths = realloc(*paths, num_new_paths * sizeof(char*));
for (int i = 0; i < num_new_paths; i++){
*paths[i] = malloc((strlen(new_paths[i]) + 1) * sizeof(char));
strcpy(*paths[i], new_paths[i]);
}
}
int main(int argc, char *argv[]){
// A Default PATH
char** paths = malloc(1 * sizeof(char*));
paths[0] = malloc(6 * sizeof(char)); // <- HOW TO FREE THIS AFTER REALLOC OF paths?
strcpy(paths[0], "/bin/");
int num_paths = 1;
// A new set of PATHs
int num_new_paths = 2;
char** new_paths = {"/temp/", "/auxillary/"};
change_paths(&num_paths, &paths, num_new_paths, new_paths);
// Free memory
for(int i = 0; i < num_new_paths; i++){
free(paths[i]);
}
free(paths);
return 0;
}
*paths[i]should be(*paths)[i]. Otherwise, (nonexistent) 2nd element ofpathswill be accessed.