为什么不推荐使用gets()? [英] Why gets() is deprecated?

查看:35
本文介绍了为什么不推荐使用gets()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中使用 gets() 时,编译器会喊叫

While using gets() in my code, the compiler shouts

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

warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638)
[-Wdeprecated-declarations]

有什么具体原因吗?

推荐答案

谁能解释一下为什么编译器会这样显示...?

是的,因为 gets() 函数很危险,因为它存在缓冲区溢出问题.任何人都应该避免使用它.

Yes, because, the gets() function is dangerous, as it suffers from buffer overflow issue. Anyone should refrain from using that.

另外,关于 -Wdeprecated-declarations 的警告,gets() 不再是 C 标准 [C11 起].因此,C 库 compilers 不再绑定 来支持它.将来可以将其删除.为了警告开发人员潜在的陷阱并阻止进一步使用 gets(),编译器## 会发出警告消息.

Also, regarding the warning with -Wdeprecated-declarations, gets() is no longer a part of C standard [C11 onwards]. So, C libraries compilers are not bound to support that any more. It can be removed in future. To warn the developer about the potential pitfall and to discourage the further usage of gets(), the compiler## emits the warning message.

(##) 为了迂腐,警告不是由编译器 (gcc) 自己生成的,而是由 pragma 或属性引起的关于 glibcgets() 的实现,这会导致编译器发出警告.[礼貌,FUZxxl,来自欺骗的答案.]

(##) To be pedantic, the warning is not generated by the compiler (gcc) all by itself, rather caused by a pragma or attribute on the implementation of gets() in the glibc that causes the compiler to emit the warning. [Courtesy, FUZxxl, from the dupe answer.]

这篇关于为什么不推荐使用gets()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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