void resolve_paths(char* inputdir)
{
char** tokens = malloc(64*sizeof(char*));
char* ptr;
char* slash = "/";
int i = 0;
ptr = strtok(inputdir,slash);
while(ptr != NULL){
if(strcmp(ptr,"~") == 0){
ptr = getenv("HOME");
}
tokens[i] = (char*)calloc(64,sizeof(char));
tokens[i] = ptr;
printf("token[%i] = %s\n",i,tokens[i]);
i++;
ptr = strtok(NULL,slash);
}
int j;
printf("freeing each element of tokens\n");
for(j = 0; j < i; j++){
printf("freeing token[%i]\n",j);
free(tokens[j]);
}
free(tokens);
puts("leaving resolve_paths\n");
return;
}
My Output:
before call
token[0] = a
token[1] = b
token[2] = c
freeing each element of tokens
freeing token[0]
freeing token[1]
Error in `./a.out': free(): invalid pointer: 0x00007ff96dca12b2 ***
I guess I simply don't understand how malloc/calloc and free work. Why does this code run into a seg fault?