警告:获取函数是危险的 [英] warning:gets function is dangerous

查看:97
本文介绍了警告:获取函数是危险的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用gets功能,海湾合作委员会给了我一个警告:

When i use gets function,gcc gives me a warning:

warning:the `gets' function is dangerous and should not be used.

为什么变得并将功能危险?

Why gets and puts function dangerous?

推荐答案

如果你有code是这样的:

If you have code like this:

char s[10];
gets( s );

和运行程序的时候,你在超过10个字符类型,您将溢出缓冲区,从而导致未定义的行为。在获得()函数pventing你键入的字符没有$ P $的手段,所以应尽量避免。相反,你应该使用与fgets(),它可以限制的字符数读取,因此该缓冲区不溢出:

and you type in more than 10 characters when the program is run, you will overflow the buffer, causing undefined behaviour. The gets() function has no means of preventing you typing the characters and so should be avoided. Instead you should use fgets(), which allows you to limit the number of characters read, so that the buffer does not overflow.:

char s[10];
fgets( s, 10, stdin );

看跌期权()函数是完全安全的,的提供的你是输出字符串是空终止的。

The puts() function is perfectly safe, provided the string that you are outputting is null-terminated.

这篇关于警告:获取函数是危险的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆