AnsweredAssumed Answered

Split string -> default handler

Question asked by toth.janos on Dec 4, 2013
Latest reply on Dec 4, 2013 by toth.janos
hello everybody!

I need help with my string splitter function. I use stm32f407 and CoIDE for develope. This is the function:
char** str_split(char* a_str, const char* a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
size_t index;
 
/* Count how many elements will be extracted. */
while (*tmp)
{
for (index = 0; index < strlen(a_delim); ++index)
{
if (*(a_delim+index) == *tmp)
{
count++;
last_comma = tmp;
}
}
tmp++;
}
 
 
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
 
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
 
result = malloc(sizeof(char*) * count);
 
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, a_delim);
 
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, a_delim);
}
//assert(idx == count - 1);
*(result + idx) = 0;
}
 
return result;
}

and this is a part of the code:
tokens = str_split(line, ",;");
if (tokens)
{
LINScheduleTable[frameIndex].Frame.SignalIDs_lda16[signalIndex] = atoi(*tokens);
LINScheduleTable[frameIndex].Frame.SignalOffsets_lda8[signalIndex] = atoi(*(tokens+1));
for (i = 0; *(tokens + i); i++)
{
free(*(tokens + i));
}
 
free(tokens);
}

The problem is, the split is working well, but the controller go to default handler when call free. Not in the first call, after 20-30 calls. I think the problem with the pointer, but the code seems good. Anybody have any idea?

Thanks

Outcomes