<分区>
在我看来,人们,尤其是在学习 C 编程语言时,仍在使用 gets
从标准输入读取数据的函数。尽管如此,它现在已从 C11 标准中删除1,并且在 cppreference 上有免责声明。阅读:
The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function has been deprecated in the third corrigendum to the C99 standard and removed altogether in the C11 standard. fgets() and gets_s() are the recommended replacements.
Never use gets().
然而,这似乎并不是一个新问题,它是由更现代的编程哲学提出来的。它总是会被破坏并导致程序崩溃,我不明白“限制标准输入上出现的内容的环境”可能意味着什么。
那么,它在过去曾经有用吗?或者它被添加到以前的标准和 C 的准标准版本中的原因是什么?
(1) ... 或至少更改为具有一个附加参数,指示要读取的最大长度。然而,我询问的是旧签名,只收到一个指针。