So basically I cannot figure out what is the problem in my code when I try to free memory for allocated char**. I created char** then allocated memory for it, then I assigned pointer to this char** to the member of my struct. When I am trying to free memory using struct member's pointer I am getting error: malloc: error for object 0x796568796568: pointer being freed was not allocated*
Here are some parts of the code:
Struct part:
struct TokenizerT_ {
char **currentToken;
char **tokens;
}tokenizer;
Allocation part (done in separate function):
char **words;
words = (char **)malloc(sizeof(char*) * (numberOfWords + 1));
for (int i = 0; i < numberOfWords; i++)
words[i] = (char *)malloc(strlen(ts)+1);
tokenizer.tokens = words;
tokenizer.currentToken = words;
Freeing part:
int i = 0;
while(*(*tk).tokens){
free((*tk).tokens[i]);
i++;
}
free((*tk).tokens);
*(*tk).tokens
never changes. It's equivalent towhile(tk->tokens[0] != NULL)
. – humodz