memset

关于用memset()赋值的一个思考  

2008-04-19 23:17:18|  分类: C/C++ |  标签:original  学习  转载   |举报 |字号 订阅

 
 
今天CSDN上有个网友问了如下一个问题,如下:
#include <memory.h>
#include <iostream.h>
void main()
{int j=0;
int ial[50];
int ia2[500];
memset(ial,1,50*sizeof(int));
memset(ia2,1,500*sizeof(int));
for(int i=0;i <50;i++)
{cout < <ial[i];
j++;
if(j%5==0)
cout < <endl;}
}
为什么这个结构是
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
1684300916843009168430091684300916843009
结果为什么不是都是1啊

要明白为什么,很容易,找一下memset的资料就行了,如下:
void * memset ( void * ptr, int value, size_t num );
<cstring>
Fill block of memory
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char). Parameters
ptr
    Pointer to the block of memory to fill.
value
    Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num
    Number of bytes to be set to the value.
Return Value
ptr is returned.
最重要的问题是怎样用memset给那样的数组赋值呢?我也做了相关的尝试,有失败,但最后还是成功了。
我的解决方法如下:
#include <cstring> 
#include
<iostream>

usingnamespace std;

int main()
{
int j=0;
int ial[50];
ial[
0]=23;
char*pt=(char*)ial;
/*cout<<ial[0]<<endl;
memset(pt,1,1);

pt+=1;
memset(pt,0,3);
cout<<ial[0]<<endl;
*/



for(int i=0;i<50;i++)
{
memset(pt
+4*i,1,1);
memset(pt
+4*i+1,0,3);
}



for(int i=0;i <50;i++)
{
cout
<<ial[i]<<endl;
j
++;
if(j%5==0)
cout
<<endl;
}

return0;
}

char*pt=(char*)ial; 这里有两个要注意的问题,一个是首先要转换成步长为1的。否则直接用int型的传值有那么步长是4,绝对错误。
 
memset(pt+4*i,1,1);
memset(pt
+4*i+1,0,3);

上面的两个写法是不是很令人费解?的确,为什么不是

memset(pt+4*i,0,3);
memset(pt
+4*i+1,1,1);

呢?
如果你学过汇编或者对intel的内存模型有点熟悉的话,可能你已经知道答案了。
Big-Endian 和Little-Endian
的问题,我找了一下资料如下,说明这个问题:
from:http://www.cppblog.com/future0906/archive/2005/11/19/1193.html
所谓的Little-Endian,就是我们在学习汇编时候的高高低低原则,而Bit-Endian就是刚刚相反,例如:12345678h这个数据,在不同机器中存储是不同的

                 Big-Endian                                      Little-Endian

0字节            12h                                                    78h

1字节            34h                                                    56h

2字节            56h                                                    34h

3字节            78h                                                    21h

Little-Endian主要用在我们现在的PC的CPU中,Big-Endian则应用在目前的Mac机器中(注意:是指Power系列 处理器)

from:http://dev.csdn.net/article/60/60401.shtm

Big endian machine: It thinks the first byte it reads is the biggest.
Little endian machine: It thinks the first byte it reads is the littlest.
举个例子,从内存地址0x0000开始有以下数据
 0x0000     0x12
 0x0001     0x34
 0x0002     0xab
 0x0003     0xcd
如果我们去读取一个地址为0x0000的四个字节变量,若字节序为big-endian,则读出
结果为0x1234abcd;若字节序位little-endian,则读出结果为0xcdab3412.
如果我们将0x1234abcd写入到以0x0000开始的内存中,则结果为
                big-endian     little-endian
0x0000     0x12              0xcd
0x0001     0x23              0xab
0x0002     0xab              0x34
0x0003     0xcd              0x12
x86系列CPU都是little-endian的字节序.

posted @ 2014-04-12 15:14  money_lady  阅读(135)  评论(0编辑  收藏  举报