csapp学习笔记(清晰揭示数在计算机中怎么存储)

先上例程,这是一段csapp上的经典程序,清晰地揭示了数在计算机内是怎么存储的,这里博主自己附上了一些注释,方便大家理解。

/* 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]); /*%p输出地址(指针),一次输出一个指针的值和其地址*/
}

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[])/*第一个参数为参数个数,第二为指向每次参数数组的指针。gcc时若只输入./a.out相当于仅
输入一个参数a。out,则会运行else后面的代码,在/a。out后再写参数就是算第二个参数*/
{
    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;
}
/*不带参数的运行结果
calling show_twocomp
0xbfa18b4c	0x39
0xbfa18b4d	0x30

0xbfa18b4e	0xc7
0xbfa18b4f	0xcf

Calling simple_show_a
0xbfa18b48	0x21

0xbfa18b48	0x21
0xbfa18b49	0x43

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

Calling simple_show_b
0xbfa18b48	0x78

0xbfa18b48	0x78
0xbfa18b49	0x56

0xbfa18b48	0x78
0xbfa18b49	0x56
0xbfa18b4a	0x34

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

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

For x = 3510593
0xbfa18b30	0x41
0xbfa18b31	0x91
0xbfa18b32	0x35
0xbfa18b33	0x00

0xbfa18b30	0x04
0xbfa18b31	0x45
0xbfa18b32	0x56
0xbfa18b33	0x4a

Calling string_ueg
0x8048940	0x41
0x8048941	0x42
0x8048942	0x43
0x8048943	0x44
0x8048944	0x45
0x8048945	0x46

Calling string_leg
0x8048947	0x61
0x8048948	0x62
0x8048949	0x63
0x804894a	0x64
0x804894b	0x65
0x804894c	0x66

gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 1073741824(2的30次方)
calling test_show_bytes
Stack variable ival = 1073741824
(int)ival:
0xbfd40020	0x00
0xbfd40021	0x00
0xbfd40022	0x00
0xbfd40023	0x40

(float)ival:
0xbfd40020	0x00
0xbfd40021	0x00
0xbfd40022	0x80
0xbfd40023	0x4e

&ival:
0xbfd40020	0x34
0xbfd40021	0x00
0xbfd40022	0xd4
0xbfd40023	0xbf

*/
/*改为:typedef int *byte_pointer的结果
gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 1073741824
calling test_show_bytes
Stack variable ival = 1073741824
(int)ival:
0xbfae9b90	0x40000000
0xbfae9b94	0x40000000
0xbfae9b98	0x40000000
0xbfae9b9c	0xb7521940

(float)ival:
0xbfae9b90	0x4e800000
0xbfae9b94	0x40000000
0xbfae9b98	0x40000000
0xbfae9b9c	0x4e800000

&ival:
0xbfae9b90	0xbfae9ba0
0xbfae9b94	0x40000000
0xbfae9b98	0x40000000
0xbfae9b9c	0x4e800000

改为:typedef char *byte_pointer的结果
gec@ubuntu:/mnt/hgfs/share/csapp_code$ ./a.out 1073741824
calling test_show_bytes
Stack variable ival = 1073741824
(int)ival:
0xbfbe3150	0x00
0xbfbe3151	0x00
0xbfbe3152	0x00
0xbfbe3153	0x40

(float)ival:
0xbfbe3150	0x00
0xbfbe3151	0x00
0xbfbe3152	0xffffff80
0xbfbe3153	0x4e

&ival:
0xbfbe3150	0x60
0xbfbe3151	0x31
0xbfbe3152	0xffffffbe
0xbfbe3153	0xffffffbf
*/
/*calling test_show_bytes
Stack variable ival = 15213
(int)ival:
0x7fff2b9ed9cc	0x6d
0x7fff2b9ed9cd	0x3b
0x7fff2b9ed9ce	0x00
0x7fff2b9ed9cf	0x00

(float)ival:
0x7fff2b9ed9cc	0x00
0x7fff2b9ed9cd	0xb4
0x7fff2b9ed9ce	0x6d
0x7fff2b9ed9cf	0x46

&ival:
0x7fff2b9ed9c8	0xfc
0x7fff2b9ed9c9	0xd9
0x7fff2b9ed9ca	0x9e
0x7fff2b9ed9cb	0x2b
0x7fff2b9ed9cc	0xff
0x7fff2b9ed9cd	0x7f
0x7fff2b9ed9ce	0x00
0x7fff2b9ed9cf	0x00

*/

我们可以看到第九行下面还有两句类似的被注释掉的代码(如下图),这里重点解释下为什么第九行是对的,而第十行和第十一行不对。

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

因为随着byte_pointer 的变化指针类型会随着变化了。指针类型不同的区别就在于加1后移动的字节数,指向int型移动4个字节,char则一个字节,所以第11行就是明显错误的了,程序的目的只是输出每个字节的值与地址用int型指针无法达到目标。
int型指针输出int型如下
(int)ival:
0xbfae9b90 0x40000000
0xbfae9b94 0x40000000
0xbfae9b98 0x40000000
0xbfae9b9c 0xb7521940
而用unsighed char的正确的输出如下
(int)ival:
0xbfd40020 0x00
0xbfd40021 0x00
0xbfd40022 0x00
0xbfd40023 0x40
可以看到上面的讲述和输出的结果可以相互印证
那 char 型指针为什么错了呢,下面是用char型指针对应float型变量
(float)ival:
0xbfbe3150 0x00
0xbfbe3151 0x00
0xbfbe3152 0xffffff80
0xbfbe3153 0x4e
和正确的比较一下
(float)ival:
0xbfd40020 0x00
0xbfd40021 0x00
0xbfd40022 0x80
0xbfd40023 0x4e
错误的原因很显然了,Ox80作为一个地址其实是省略了前面的6个零的。我们想象有符号的指针一看Ox80这个数,哎,符号为1,这是个负数啊,那要用负数的表数方式当然得在前面补1啊,所以变成了0xffffff80。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值