寻址和字节序列 ---读《深入理解计算机系统》笔记

      对于跨越多字节的程序对象,我们必须建立两个规则:这个对象的地址是什么,以及在存储器中如何排列这些字节。在几乎所有的机器中,多字节对象都被存储为连续的字节序列,对象的地址是所使用字节中的最小地址

     小端法:存储器中按照从最低有效字节到最高有效字节的顺序存储对象;

     大端法:存储器中按照从最高有效字节到最低有效字节的顺序存储对象;

    不同的机器采用不同的规则,我们可以通过程序来了解自己所用机器所采用的规则。

#include<stdio.h>

typedef unsigned char *byte_pointer;  //byte_pointer定义为指向unsigned char类型的对象指针

void show_bytes(byte_pointer start,int len){

int i;

for(i=0;i<len;i++)

 printf(" %.2x",start[i]);

printf("\n");

}

void show_int(int x){

show_bytes((byte_pointer)&x,sizeof(int));

}

void show_float(float x){

show_bytes((byte_pointer)&x,sizeof(float));

}

void show_pointer(void *x){

show_bytes((byte_pointer)&x,sizeof(void *));

}

void test_show_bytes(int val)

{

 int ival=val;

float fval=float(ival);

int *pval=&ival;

show_int(ival);

show_float(fval);

show_pointer(pval);

}

posted @ 2011-08-17 17:07  llkkyy  阅读(1420)  评论(0编辑  收藏  举报