读书人

关于gets的用法解决思路

发布时间: 2012-09-07 10:38:15 作者: rapoo

关于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;} 

读书人网 >C++

热点推荐