I've been trying to understand this but I just can't so I turn to you for help. I will leave most of the code out, so if you need me to post more code just tell me.
The code:
1 2 3 4 5 6 7 8 9 10 11 12
buf = (char *)calloc(len, sizeof(char));
for (i = 0; i <= k; i++)
{
sprintf(&buf[strlen(buf)], ":%d ", tokens[i].line);
strcat(buf, tokens[i].word);
strcat(buf, "\n");
}
fputs(buf, f);
fclose(f);
free(buf);
The problem:
I randomly (2/3 of the time) get an exception on the line free(buf); and the link seems to be with the sprintf(&buf[strlen(buf)], ":%d ", tokens[i].line); because commenting out either of them solves the problem.