乐哈哈旅游视频网:

linux 内存相关~~~~集~~

源程序如下
#include <stdio.h> /*C99标准*/
int main(void)
{
  int zippo[4][2] = { {2,4},{6,8},{1,3},{5,7} }
 
  printf("zippo=%p,zippo+1=%p\n",
          zippo,     zippo+1);
  printf("*(*(zippo+2)+1)=%d\n",*(*(zippo+2)+1));
 
  return 0;
}
系统输出如下
zippo=Ox0064fd38, zippo+1=Ox0064fd40
*(*(zippo+2)+1)=3

考虑int型占用4个字节,所以从zippo到zippo+1,这块地址内存储{2,4}这两个整数.
即共8个字节.内存地址也是从
0x0064fd38
0x0064fd39
0x0064fd3a
0x0064fd3b
0x0064fd3c
0x0064fd3d
0x0064fd3e
0x0064fd3f
Ox0064fd40
即每个地址代表一个字节,8bit.
这样是否代表,最小存储为8bit?
如果是的话,看以前的一道华为面试题,大体就是存储一项数据,求其最小的存储空间.其中有一项为性别项,它答案最小存储空间为1bit(0,1)表示,但是,按照内存空间分配的话,不能分配1bit的空间来给这个项目,最小也是1字节,所以就会浪费7bit,但是事实只能这样(如果我上面的提问是正确的话),这样是否代表,实际答案为1字节,不是1bit,而华为的答案1bit只是理论值?并不能真正为其分配1bit的空间?是否这样?
再有,C99标准中引进的_Bool类型,占用多少内存空间?是否为1byte?


==========================================================================


A> Please use C language to write a program to test Memory from address 0x0000 ~ 0xFFFF
by the following methods:

1>Please write data 0xAB to each byte, then read it back and compare the result. If all memory location test pass, then show “Memory Data Test Pass’ on the screen. If any memory location finds error, then show “Memory Data Test Fail” on screen.

2>Please use the address value as the data and write into each memory location and read it back to compare the result. For example, write the address value 0x0000 to memory location 0x0000, and value 0x0001 to location0x0002, … till 0xFFFE

1.(下面程序不能用于WINDOWS):

#include <stdio.h>

void main()
{
unsigned int i = 0;
unsigned char *p = 0x0000;
for(i=0;i<0x10000;i++)
{
*p = 0xAB;
if(0xAB != *p)break;
p++;
}
if(0x10000!=i)
printf("Memory Data Test Pass\n");
else
printf("Memory Data Test Fail\n");
return;
}


2.(下面程序不能用于WINDOWS):

#include <stdio.h>

void main()
{
int i = 0;
unsigned char *p = 0x0000;
for(i=0;i<0x8000;i++)
{
*(unsigned short *)p = i;
if(i != *(unsigned short *)p)break;
p+=2; /* 回lengxuehou: 原题地址是以字节长度计算,而1个16位地址值是需要1个short类型的值才可以存储的呵呵,所以p每次递增2,而i就是计数一共有多少个short,可以看原题最后的sample呵呵*/
}
if(0x8000 != i)
printf("Memory Data Test Pass\n");
else
printf("Memory Data Test Fail\n");
return;
}

posted on 2006-12-28 10:47  riky  阅读(625)  评论(4编辑  收藏  举报

乐哈哈旅游视频网: