Another thing to know is that it actually modifies the original string. In fact strtok does not allocate memory. You may understand it better if you draw the string as a sequence of boxes. To understand how strtok works, one first need to know what a static variable is. This link explains it quite well The key to the operation of strtok is preserving the location of the last seperator between seccessive calls that's why strtok continues to parse the very original string that is passed to it when it is invoked with a null pointer in successive calls..
Have a look at my own strtok implementation, called zStrtok , which has a sligtly different functionality than the one provided by strtok. The code is from a string processing library I maintain on Github , called zString. This is how i implemented strtok, Not that great but after working 2 hr on it finally got it worked. It does support multiple delimiters. Moreover strtok is destructive i.
Submit Next Question. By signing up, you agree to our Terms of Use and Privacy Policy. Forgot Password? This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy. Another possibility is to provide your own implementation of strtok that does not modify the initial arguments.
The Linux Programmer's Manual man page on strtok 3 [ Linux ] states:. Never use this function. This function modifies its first argument. The identity of the delimiting character is lost. This function cannot be used on constant strings. The improper use of strtok is likely to result in truncated data, producing unexpected results later in program execution. Search for vulnerabilities resulting from the violation of this rule on the CERT website. Answer 1: A string passed to strtok is no longer safe, as strtok modifies it.
Theoretically you should never refer to a string after passing it to strtok. The CCE accomplishes this by giving strtok a copy of the string and then freeing the copy. Whoops, the CS isn't actually freeing the copy OT: It's int main void by the way. Add a comment. Active Oldest Votes. Improve this answer. Floris Floris Sean Sean Andy Thomas Andy Thomas Most modern runtimes store the state in thread local storage.
Which means that it is thread safe but not safe when used re-entrantly. Thanks for the correction. JuanR JuanR 5, 17 17 silver badges 28 28 bronze badges. The Overflow Blog.
0コメント