strcat的VS strncat函数 - 当它应该函数中使用? [英] strcat Vs strncat - When should which function be used?

查看:1018
本文介绍了strcat的VS strncat函数 - 当它应该函数中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些静态code分析工具所提出的建议,所有strcat的用法应与strncat函数被替换为安全目的?

Some static code analyzer tools are suggesting that all strcat usage should be replaced with strncat for safety purpose?

在一个程序中,如果我们知道目标缓冲区和源缓冲区明确的大小,是否仍建议去strncat函数?

In a program, if we know clearly the size of the target buffer and source buffers, is it still recommended to go for strncat?

此外,由于采用静态工具的建议,应该可以的strcat使用过?

Also, given the suggestions by static tools, should strcat be used ever?

推荐答案

如果你是绝对的把握源缓冲区的大小和源缓冲区包含NULL字符终止字符串,那么你可以放心地使用strcat的时候目标缓冲区足够大。

If you are absolutely sure about source buffer's size and that the source buffer contains a NULL-character terminating the string, then you can safely use strcat when the destination buffer is large enough.

我还是建议使用strncat函数,并给它的目标缓冲区大小 - 目标字符串长度 - 1

I still recommend using strncat and give it the size of the destination buffer - length of the destination string - 1

注:我编辑这一点,因为评论指出,我的previous答案是可怕的错误。

Note: I edited this since comments noted that my previous answer was horribly wrong.

这篇关于strcat的VS strncat函数 - 当它应该函数中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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