memset不能填充数组(memset not filling array)

编程入门 行业动态 更新时间:2024-10-28 21:15:37
memset不能填充数组(memset not filling array) u32 iterations = 5; u32* ecx = (u32*)malloc(sizeof(u32) * iterations); memset(ecx, 0xBAADF00D, sizeof(u32) * iterations); printf("%.8X\n", ecx[0]); ecx[0] = 0xBAADF00D; printf("%.8X\n", ecx[0]); free(ecx);

简而言之,为什么我的输出结果如下?

0D0D0D0D BAADF00D

ps:u32是一个简单的unsigned int类型定义

编辑:

用gcc编译4.3.4 包含string.h u32 iterations = 5; u32* ecx = (u32*)malloc(sizeof(u32) * iterations); memset(ecx, 0xBAADF00D, sizeof(u32) * iterations); printf("%.8X\n", ecx[0]); ecx[0] = 0xBAADF00D; printf("%.8X\n", ecx[0]); free(ecx);

Very simply put, why is my output the following?

0D0D0D0D BAADF00D

ps: u32 is a simple typedef of unsigned int

edit:

Compiling with gcc 4.3.4 string.h is included

最满意答案

memset的第二个参数被键入为int,但实际上是一个无符号字符 。 0xBAADF00D转换为无符号字符(最低有效字节)为0x0D,因此memset用0x0D填充内存。

The second parameter to memset is typed as an int but is really an unsigned char. 0xBAADF00D converted to an unsigned char (least significant byte) is 0x0D, so memset fills memory with 0x0D.

更多推荐

本文发布于:2023-07-31 21:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1348961.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   memset   array   filling

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!