Why gets() is deprecated? [duplicate]

孤者浪人 提交于 2019-11-26 14:58:46

问题


This question already has an answer here:

  • Why is the gets function so dangerous that it should not be used? 11 answers

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

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

and

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

Any specific reasons?


回答1:


Can someone explains why the compiler shows like that…?

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

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.


(##) 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.]




回答2:


  1. gets may cause buffer overflow, since it don't consider length of the data. More details are here : gets() function in C

  2. deprecated message means, this function is marked as deprecated and may remove from standard in later time. So discouraging user to use it.



来源:https://stackoverflow.com/questions/30890696/why-gets-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!