show-bytes程序的运行感悟

/* show-bytes - prints byte representation of data */
/* $begin show-bytes */
#include <stdio.h>
/* $end show-bytes */
#include <stdlib.h>
#include <string.h>
/* $begin show-bytes */

typedef unsigned char *byte_pointer;
//typedef char *byte_pointer;
//typedef int *byte_pointer;

void show_bytes(byte_pointer start, size_t len) {
    size_t i;
    for (i = 0; i < len; i++)
	printf("%p\t0x%.2x\n", &start[i], 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 *));
}
/* $end show-bytes */


/* $begin test-show-bytes */
void test_show_bytes(int val) {
    int ival = val;
    //float fval = (float) ival;
	double fval = (double) ival;
    int *pval = &ival;
    printf("Stack variable ival = %d\n", ival);
    printf("(int)ival:\n");
    show_int(ival);
    printf("(float)ival:\n");
    show_float(fval);
    printf("&ival:\n");
    show_pointer(pval);
}
/* $end test-show-bytes */

void simple_show_a() {
/* $begin simple-show-a */
int val = 0x87654321;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-a */
}

void simple_show_b() {
/* $begin simple-show-b */
int val = 0x12345678;
byte_pointer valp = (byte_pointer) &val;
show_bytes(valp, 1); /* A. */
show_bytes(valp, 2); /* B. */
show_bytes(valp, 3); /* C. */
/* $end simple-show-b */
}

void float_eg() {
  int x = 3490593;
  float f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

  x = 3510593;
  f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

}

void string_ueg() {
/* $begin show-ustring */
const char *s = "ABCDEF";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-ustring */
}

void string_leg() {
/* $begin show-lstring */
const char *s = "abcdef";
show_bytes((byte_pointer) s, strlen(s)); 
/* $end show-lstring */
}

void show_twocomp() 
{
/* $begin show-twocomp */
    short x = 12345; 
    short mx = -x; 
    
    show_bytes((byte_pointer) &x, sizeof(short)); 
    show_bytes((byte_pointer) &mx, sizeof(short)); 
/* $end show-twocomp */
}

int main(int argc, char *argv[])
{
    int val = 12345;
    if (argc > 1) {
        val = strtol(argv[1], NULL, 0);
	printf("calling test_show_bytes\n");
	test_show_bytes(val);
    } else {
	printf("calling show_twocomp\n");
	show_twocomp();
	printf("Calling simple_show_a\n");
	simple_show_a();
	printf("Calling simple_show_b\n");
	simple_show_b();
	printf("Calling float_eg\n");
	float_eg();
	printf("Calling string_ueg\n");
	string_ueg();
	printf("Calling string_leg\n");
	string_leg();
    }
    return 0;
}

这一段C代码,使用了强制类型转换来访问和打印不同程序对象的字节表示,开始的typedef将数据类型 byte_pointer 定义为了一个指向 unsigned char 的指针。这样每一个字节都被认为是一个非负整数。第一个例程的输入是一个字节序列的地址,它用一个字节指针以及一个字节数来指示,该字节数指定为数据类型size_t,表示数据结构大小的首选数据类型。在这个程序中的show_bytes打印出每个以十六进制表示的字节,这样在不同的机器内更好的分辨该机器是小端模式还是大端模式,更好的让我们清晰的了解到不同类型的数据在机器的内部是如何储存的。在这个程序中使用了C语言中的sizeof来确定了对象所需的字节数,sizeof(T)会返回储存一个类型为T的对象使用的字节数。

calling show_twocomp
0xbfa18b4c	0x39
0xbfa18b4d	0x30

测试数12345的十六进制表示为0x00003039 ,而我的机器在储存该数据的时候能明显看到低地址储存的是地位数据0x39,则我的机器在储存数据时采用的小端模式。


0xbfa18b4e	0xc7
0xbfa18b4f	0xcf

这两个地址的数据储存放的是-12345的十六进制的表示,而负数在机器内部的表示是通过补码的形式储存起来的,而还是由小端模式储存起来的,则印证了本机器为小端模式储存。

Calling simple_show_a
0xbfa18b48	0x21

0xbfa18b48	0x21
0xbfa18b49	0x43

0xbfa18b48	0x21
0xbfa18b49	0x43
0xbfa18b4a	0x65

这一段表示了测试数0x87654321 在分别每8位截取时的表示,第一次截取第一个8位数据,然后第二次截取两个8位数据,从而更清晰的看清楚数据在机器内部的储存,然后通过把储存地址和数据放在一起,让人更方便去比较,同时我们可以发现,在机器内部对于64位机而言,一个储存单元可以存放8位的2进制数,因此每一个储存单元所对应的数据都是两位的十六进制数。同时,在考虑机器的数据储存模式的时候,也就是考虑机器是小端模式还是大端模式的时候,要注意,我们是从最先开始被存放的单元进行计算看自己的数据被存放在哪一个单元的,而在数据真实存放时,我们也不清楚最开始的单元会是哪一个,就比如这个测试数,第一个储存单元为0xbfa18b48 然后按照小端模式储存,但是这个储存单元是电脑随机分配的。

void float_eg() {
  int x = 3490593;
  float f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

  x = 3510593;
  f = (float) x;
  printf("For x = %d\n", x);
  show_int(x);
  show_float(f);

Calling float_eg
For x = 3490593
0xbfa18b30	0x21
0xbfa18b31	0x43
0xbfa18b32	0x35
0xbfa18b33	0x00

0xbfa18b30	0x84
0xbfa18b31	0x0c
0xbfa18b32	0x55
0xbfa18b33	0x4a

由这个实验结果我们可以看到,虽然从int强制转换为float不会溢出,但是有可能会导致舍入。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值