在 C 中,memcpy 如何处理有符号整数参数?

发布于 2024-09-06 12:45:51 字数 218 浏览 3 评论 0原文

在 C 中,如果我提供一个有符号整数(特别是负整数)作为 memcpy 函数的第三个参数,会发生什么?

示例:

 memcpy(destBuf, source, -100*sizeof(source))

-100*sizeof(source) 的结果会被 memcpy 解释为无符号吗?

谢谢!

In C, what will happen if I supply a signed integer especifically a negative integer as the 3rd argument to the memcpy function?

Example:

 memcpy(destBuf, source, -100*sizeof(source))

Will the result of -100*sizeof(source) be interpreted by memcpy as unsigned?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮年 2024-09-13 12:45:51

最后一个参数是无符号的。因此,通过执行 -100 * sizeof( source ) 你将得到一个巨大的数字(这将环绕,即溢出)。

这相当于执行“4,294,967,196 * sizeof( source )”。

编辑:实际上我刚刚意识到那是错误的。它将执行 -100 * sizeof( source ) 然后将其转换为无符号。例如,如果 sizeof( source ) 为 4,那么它将把 -400 转换为无符号,并为您提供 0xFFFFFE70 (4,294,966,896)。

the last parameter is unsigned. so by doing -100 * sizeof( source ) you'll get a huge number (That will wrap around, ie overflow).

This is equivalent to doing "4,294,967,196 * sizeof( source )".

Edit: Actually thats wrong I just realised. It will do -100 * sizeof( source ) and then convert it to an unsigned. For example if sizeof( source ) is 4 then it will convert -400 to unsigned and give you 0xFFFFFE70 (4,294,966,896).

爱情眠于流年 2024-09-13 12:45:51

不,您将复制大量数据,因为 -100 被视为无符号数,是一个巨大的无符号数,例如 FFFFFF9C,约为 40 亿左右...事实上,您将其乘以 sizeof(source) 可能减少一点...

No, you will copy a big amount of data since -100 seen as an unsigned number is a huge unsigned number, something like FFFFFF9C, which is 4 billions or so... the fact that you are multiplying it to sizeof(source) may reduce it a bit ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文