gpt4 book ai didi

c - 使用 gets 防止缓冲区溢出

转载 作者:太空狗 更新时间:2023-10-29 15:19:53 33 4
gpt4 key购买 nike

<分区>

gets的声明是:

char * gets ( char * str );

请注意str 的最大尺寸明显遗漏

cplusplus.com 2 :

Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).

还有:

The most recent revision of the C standard (2011) has definitively removed this function from its specification. The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3).

现在,当然,fgets 通常被推荐作为 gets 的替代品,因为它的声明如下所示:

char * fgets ( char * str, int num, FILE * stream );

确实有一个尺寸参数。这使得它比 gets 安全得多。

既然我不愿意花钱下载或购买 C11 标准,谁能解释一下弃用 gets 的原因及其意义意味着 future 的代码?当 fgets 更安全时,为什么它还存在于同一个地方?为什么它只是现在被弃用了?

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com