C'S"坏QUOT;功能与他们的"好"备择方案 [英] C's "bad" functions vs. their "good" alternatives

查看:127
本文介绍了C'S"坏QUOT;功能与他们的"好"备择方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C中的坏的功能,他们有什么好的替代品?

What are the "bad" functions in C, and what are their "good" alternatives?

为什么坏的坏了,是什么让好的更好?

Why are the bad ones bad, and what makes the good ones better?

我知道,例如,获得()是坏的,因为它没有任何形式的边界检查的。什么是它更好的选择? 与fgets()

I know, for example, gets() is "bad" because it doesn't have any form of bounds checking. What is its better alternative? fgets()?

我听说 scanf()的是坏的,但我不记得为什么。任何人都知道?什么是最好的选择呢?

I've heard scanf() is bad but I can't remember why. Anyone know? What's the best alternative?

是否有更多的?

推荐答案

在过去,大部分的字符串函数没有边界检查。当然,他们不能只是删除旧的功能,或者修改他们的签名,包括上限,这会破坏兼容性。现在,几乎每个人的这些功能​​,还存在另一种N版本。例如:

In the old days, most of the string functions had no bounds checking. Of course they couldn't just delete the old functions, or modify their signatures to include an upper bound, that would break compatibility. Now, for almost every one of those functions, there is an alternative "n" version. For example:

strcpy -> strncpy
strlen -> strnlen
strcmp -> strncmp
strcat -> strncat
strdup -> strndup
sprintf -> snprintf
wcscpy -> wcsncpy
wcslen -> wcsnlen

和其他。

修改2013年12月3日:

另请参阅 https://github.com/leafsr/gcc-poison 这是一个项目,以创建一个标题文件,导致如果您使用不安全的函数GCC报告错误。

See also https://github.com/leafsr/gcc-poison which is a project to create a header file that causes gcc to report an error if you use an unsafe function.

这篇关于C'S"坏QUOT;功能与他们的"好"备择方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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