关于gets的用法
- C/C++ code
#include <iostream>#include <string>using namespace std;int main(){ char str[5]; gets(str); cout << str; return 0;}输入:1234567
已经超过5个字符,为什么不溢出呢?
[解决办法]
这就是 gets的危害。永远不要用gets。 最高级危险函数。
[解决办法]
实际上都已经发生溢出了:
- C/C++ code
#include <iostream>#include <string>using namespace std;int main(){ char str1[5] = {0}; char str[5] = {0}; char buf[10]= {0}; gets(str); cout << "str: " <<str <<endl; cout << "str1:" <<str1 <<endl; cout << "buf :" <<buf <<endl; return 0;}