我一生都无法弄清楚我做错了什么。
T* tokenizer = create(argv[1], argv[2]);
destroy(tokenizer);
这是结构:
struct T_
{
char *sep_set;
char *str;
char *token;
int *last_index;
};
typedef struct T_ T;
这是创建函数:
T *create(char *separators, char *ts)
{
T *tk = malloc(sizeof(struct T_));
tk->sep_set = malloc(sizeof(char)*strlen(separators));
tk->str = malloc(sizeof(char)*strlen(ts));
tk->last_index = malloc(sizeof(int));
tk->sep_set = separators;
tk->str = ts;
*tk->last_index = 0;
return tk;
}
void destroy(T *tk)
{
free(tk->sep_set);
free(tk->str);
free(tk->last_index);
free(tk);
}
我的错误是:
==12302== Invalid free() / delete / delete[] / realloc()
==12302== at 0x4C273F0: free (vg_replace_malloc.c:446)
==12302== by 0x400649: destroy (t.c:58)
==12302== by 0x40088C: main (t.c:145)
==12302== Address 0x7ff0006e7 is on thread 1's stack
==12302==
==12302== Invalid free() / delete / delete[] / realloc()
==12302== at 0x4C273F0: free (vg_replace_malloc.c:446)
==12302== by 0x400659: destroy (t.c:59)
==12302== by 0x40088C: main (t.c:145)
==12302== Address 0x7ff0006ec is on thread 1's stack
第 58 和 59 行是
free(tk->sep_set);
free(tk->str);
任何帮助将非常感激!