1

c++ 函数,strtok() cplusplus.com

如果str未正确终止,此示例是否会遭受缓冲区溢出?

/* strtok example */
/* source - cplusplus.com (see link below) */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-"); // walk the stack?
  }
  return 0;
}

如果str没有以“\0”正确终止,是不是可以

pch = strtok (NULL, " ,.-");

走堆栈?

谢谢!

4

1 回答 1

1

如果字符串不是以空值结尾的,大多数字符串处理函数都会结束。

但是,在您的代码示例中,str已终止。

于 2010-09-02T21:04:12.247 回答