c - strtok坏了吗?或者只是棘手?

标签 c std strtok

strtok 彻底崩溃了吗?

在 StackOverflow 上有关 C 文本解析的许多问题上,有人会建议使用 strtok, 一个常见的答复是永远不应该使用 strtok,它已经被彻底破坏了。

一些发帖者声称strtok的问题仅限于多线程问题,在单线程环境中是安全的。

正确答案是什么?
有效吗?
是不是已经彻底崩溃了?
你能用例子来支持你的答案吗?

最佳答案

是的,strtok 已经无可救药地被破坏了,即使在一个简单的单线程程序中,我将用一些示例代码来演示这个失败:

让我们从一个简单的文本分析器函数开始,使用 strtok 收集有关文本句子的统计信息。 此代码将导致未定义的行为。

在此示例中,句子是一组由空格、逗号、分号和句点分隔的单词。

// Example:
//     int words, longest;
//     GetSentenceStats("There were a king with a large jaw and a queen with a plain face, on the throne of England.", &words, &longest);
// will report there are 20 words, and the longest word has 7 characters ("England").
void GetSentenceStats(const char* sentence, int* pWordCount, int* pMaxWordLen)
{
    char* delims = " ,;.";           // In a sentence, words are separated by spaces, commas, semi-colons or period.
    char* input = strdup(sentence);  // Make an local copy of the sentence, to be modified without affecting the caller.

    *pWordCount = 0;                 // Initialize the output to Zero
    *pMaxWordLen = 0;

    char* word = strtok(input, delims);
    while(word)
    {
        (*pWordCount)++;
        *pMaxWordLen = MAX(*pMaxWordLen, (int)strlen(word));
        word = strtok(NULL, delims);
    }
    free(input);
}

这个简单的函数可以工作。到目前为止还没有错误。

<小时/>

现在让我们扩充我们的库,添加一个收集文本段落统计信息的函数。
段落是由感叹号、问号和句点分隔的一组句子。

它将返回段落中的句子数以及最长句子中的单词数。
也许最重要的是,它将使用之前的函数 GetSentenceStats 来帮助

void GetParagraphStats(const char* paragraph, int* pSentenceCount, int* pMaxWords)
{
    char* delims = ".!?";             // Sentences in a paragraph are separated by Period, Question-Mark, and Exclamation.
    char* input = strdup(paragraph);  // Make an local copy of the paragraph, to be modified without affecting the caller.

    *pSentenceCount = 0;
    *pMaxWords = 0;
    char* sentence = strtok(input, delims);
    while(sentence)
    {
        (*pSentenceCount)++;

        int wordCount;
        int longestWord;
        GetSentenceStats(sentence, &wordCount, &longestWord);
        *pMaxWords = MAX(*pMaxWords, wordCount);
        sentence = strtok(NULL, delims);    // This line returns garbage data, 
    }
    free(input);
}

这个函数看起来也非常简单明了。
但它不起作用,如本示例程序所示。

int main(void)
{
    int cnt;
    int len;

    // First demonstrate that the SentenceStats function works properly:
    char *sentence = "There were a king with a large jaw and a queen with a plain face, on the throne of England."; 
    GetSentenceStats(sentence, &cnt, &len);
    printf("Word Count: %d\nLongest Word: %d\n", cnt, len);
    // Correct Answer:
    // Word Count: 20
    // Longest Word: 7   ("England")


    printf("\n\nAt this point, expected output is 20/7.\nEverything is working fine\n\n");

    char paragraph[] =  "It was the best of times!"   // Literary purists will note I have changed Dicken's original text to make a better example
                        "It was the worst of times?"
                        "It was the age of wisdom."
                        "It was the age of foolishness."
                        "We were all going direct to Heaven!";
    int sentenceCount;
    int maxWords;
    GetParagraphStats(paragraph, &sentenceCount, &maxWords);
    printf("Sentence Count: %d\nLongest Sentence: %d\n", sentenceCount, maxWords);
    // Correct Answer:
    // Sentence Count: 5
    // Longest Sentence: 7  ("We were all going direct to Heaven")

    printf("\n\nAt the end, expected output is 5/7.\nBut Actual Output is Undefined Behavior! Strtok is hopelessly broken\n");
    _getch();
    return 0;
}

strtok 的所有调用都完全正确,并且基于单独的数据。
但结果是未定义的行为!

为什么会发生这种情况?
当调用 GetParagraphStats 时,它会开始一个 strtok 循环来获取句子。 在第一句话中,它将调用 GetSentenceStatsGetSentenceStats 也将是一个 strtok 循环,丢失由 GetParagraphStats 建立的所有状态。 当GetSentenceStats返回时,调用者(GetParagraphStats)将再次调用strtok(NULL)以获取下一个句子。

但是 strtok认为这是一个继续之前操作的调用,并将继续对现在已释放的内存进行标记! 结果是可怕的未定义行为。

什么时候使用 strtok 是安全的?
即使在单线程环境中strtok只有只有当程序员/架构师确定以下两个条件时才能安全使用:

  • 使用 strtok 的函数绝不能调用任何可能也使用 strtok 的函数。
    如果它调用也使用 strtok 的子例程,则它自己对 strtok 的使用可能会被中断.

  • 使用 strtok 的函数绝不能被任何也可能使用 strtok 的函数调用。
    如果此函数曾被另一个使用 strtok 的例程调用,则此函数将中断调用者使用strtok。

在多线程环境下,使用strtok就更不可能了,因为程序员需要确保当前只有一次使用strtok线程,而且也没有其他线程使用 strtok

关于c - strtok坏了吗?或者只是棘手?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28588170/

相关文章:

c++ - 可移植网格引擎平台

c - 函数第一次调用时正确分配内存,但第二次调用时却没有正确分配内存

c++ - 未丑化名称的标准库实现源

c++ - 如何通过引用为 C++0x 传递 Lambda 表达式参数

c - 将整数文本文件解析为数组

c - C 程序中的字母猜谜游戏,错误混淆

c++ - 双值的 sprintf 格式化

c++ - 删除函数的 std::map

c - 如何在C中将一个字符串拆分为2个字符串

c - 在导致段错误的字符串文字上使用 strtol