memset的不灌装阵列 [英] memset not filling array

查看:186
本文介绍了memset的不灌装阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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为unsigned int的简单的typedef

ps: u32 is a simple typedef of unsigned int

编辑:


  • 用gcc 4.3.4编译

  • string.h中包含

推荐答案

要memset的第二个参数的类型为int类型,但是是的真是一个unsigned char型。 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.

这篇关于memset的不灌装阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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