I have a frustrating problem for which I can't find an answer to.
I have this function:
// Append character to the end of a string
void str_AppendChar(char* s, const char* ch)
{
// +2 because of 2x '\0'
char* buff = malloc(strlen(s)+strlen(ch)+2);
memset(buff, 0, sizeof(buff));
// Copy the whole string in buff
strcpy(buff, s);
// Append ch at the end of buff
int len = strlen(buff);
strcpy(buff+len, ch);
// Set end of the string
*(buff+strlen(buff)-3) = '\0';
strcpy(s, buff);
free(buff);
}
In which for some reason my program tries to execute free two time at the end.
The code in which I use AppendChar() is:(a bit ugly but bear with me)
void it_GatherCmd(cmd_Details* lineptr[], char* cmd)
{
// Used to count number of rows in lineptr
int nlines;
Detailptr p;
char ch;
char* word = (char*)malloc(sizeof(char)+256);
memset(word, 0, sizeof(word));
nlines = 0;
while ((ch = *cmd++) != '\n')
{
if (ch != ' ' && ch != '\0' )
str_AppendChar(word, &ch);
else
{
int type = dict_CheckWord(word);
if (type != -1)
{
p = it_CopyInfo(word, type);
lineptr[nlines++] = p;
}
memset(word, 0, sizeof(word));
}
}
//EDIT*
free(word);
}
And my main:
int main()
{
cmd_Details* arrCmd[MAXLINES];
char* str = "just some string";
it_GatherCmd(arrCmd, str);
printf("%s", str);
return 0;
}
AppendChar() was working without problems until I created it_GetCharCmd() and used it there. I've spent around 3 hours on this and I can't find the problem. Did some searching on the internet but the things I have found didn't exactly relate to my problem.