This is from the documentation : http://man7.org/linux/man-pages/man3/strtok.3.html
The strtok() function parses a string into a sequence of tokens. On
the
first call to strtok() the string to be parsed should be specified in str.
In each subsequent call that should parse the same string, str should be
NULL.
As the strtok manual says :
Be cautious when using these functions. If you do use them, note that:
* These functions modify their first argument.
* These functions cannot be used on constant strings.
* The identity of the delimiting byte is lost.
* The strtok() function uses a static buffer while parsing, so it's not
thread safe. Use strtok_r() if this matters to you.
If you get illegal frees maybe it is related to this (from Valgrid manuals):You will also get this message if you try to free a pointer that doesn't point to the start of a heap block.Memcheck keeps track of the blocks allocated by your program with malloc/new, so it can know exactly whether or not the argument to free/delete is legitimate or not. Here, this test program has freed the same block twice. As with the illegal read/write errors, Memcheck attempts to make sense of the address freed. If, as here, the address is one which has previously been freed, you wil be told that -- making duplicate frees of the same block easy to spot.
And lastly have a look at this : strtok function thread safety
You can try to use strtok_r()