欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CSAPP学习日志1:show_bytes

程序员文章站 2022-06-29 18:58:34
...

代码全览

/* 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;
}

typedef unsigned char* byte_pointer不带参数的运行结果:

calling show_twocomp
0x7ffec6b013a4	0x39
0x7ffec6b013a5	0x30

0x7ffec6b013a6	0xc7
0x7ffec6b013a7	0xcf

Calling simple_show_a
0x7ffec6b0139c	0x21

0x7ffec6b0139c	0x21
0x7ffec6b0139d	0x43

0x7ffec6b0139c	0x21
0x7ffec6b0139d	0x43
0x7ffec6b0139e	0x65

Calling simple_show_b
0x7ffec6b0139c	0x78

0x7ffec6b0139c	0x78
0x7ffec6b0139d	0x56

0x7ffec6b0139c	0x78
0x7ffec6b0139d	0x56
0x7ffec6b0139e	0x34

Calling float_eg
For x = 3490593
0x7ffec6b0137c	0x21
0x7ffec6b0137d	0x43
0x7ffec6b0137e	0x35
0x7ffec6b0137f	0x00

0x7ffec6b0137c	0x84
0x7ffec6b0137d	0x0c
0x7ffec6b0137e	0x55
0x7ffec6b0137f	0x4a

For x = 3510593
0x7ffec6b0137c	0x41
0x7ffec6b0137d	0x91
0x7ffec6b0137e	0x35
0x7ffec6b0137f	0x00

0x7ffec6b0137c	0x04
0x7ffec6b0137d	0x45
0x7ffec6b0137e	0x56
0x7ffec6b0137f	0x4a

Calling string_ueg
0x55e9a92e9d34	0x41
0x55e9a92e9d35	0x42
0x55e9a92e9d36	0x43
0x55e9a92e9d37	0x44
0x55e9a92e9d38	0x45
0x55e9a92e9d39	0x46

Calling string_leg
0x55e9a92e9d3b	0x61
0x55e9a92e9d3c	0x62
0x55e9a92e9d3d	0x63
0x55e9a92e9d3e	0x64
0x55e9a92e9d3f	0x65
0x55e9a92e9d40	0x66

改为 typedef char *byte_pointer 的运行结果:

calling show_twocomp
0x7ffe25b91464	0x39
0x7ffe25b91465	0x30

0x7ffe25b91466	0xffffffc7
0x7ffe25b91467	0xffffffcf

Calling simple_show_a
0x7ffe25b9145c	0x21

0x7ffe25b9145c	0x21
0x7ffe25b9145d	0x43

0x7ffe25b9145c	0x21
0x7ffe25b9145d	0x43
0x7ffe25b9145e	0x65

Calling simple_show_b
0x7ffe25b9145c	0x78

0x7ffe25b9145c	0x78
0x7ffe25b9145d	0x56

0x7ffe25b9145c	0x78
0x7ffe25b9145d	0x56
0x7ffe25b9145e	0x34

Calling float_eg
For x = 3490593
0x7ffe25b9143c	0x21
0x7ffe25b9143d	0x43
0x7ffe25b9143e	0x35
0x7ffe25b9143f	0x00

0x7ffe25b9143c	0xffffff84
0x7ffe25b9143d	0x0c
0x7ffe25b9143e	0x55
0x7ffe25b9143f	0x4a

For x = 3510593
0x7ffe25b9143c	0x41
0x7ffe25b9143d	0xffffff91
0x7ffe25b9143e	0x35
0x7ffe25b9143f	0x00

0x7ffe25b9143c	0x04
0x7ffe25b9143d	0x45
0x7ffe25b9143e	0x56
0x7ffe25b9143f	0x4a

Calling string_ueg
0x56542d245d34	0x41
0x56542d245d35	0x42
0x56542d245d36	0x43
0x56542d245d37	0x44
0x56542d245d38	0x45
0x56542d245d39	0x46

Calling string_leg
0x56542d245d3b	0x61
0x56542d245d3c	0x62
0x56542d245d3d	0x63
0x56542d245d3e	0x64
0x56542d245d3f	0x65
0x56542d245d40	0x66

改为 typedef int *byte_pointer 的运行结果:

calling show_twocomp
0x7fffda172ef4	0xcfc73039
0x7fffda172ef8	0x369ab800

0x7fffda172ef6	0xb800cfc7
0x7fffda172efa	0xe5cd369a

Calling simple_show_a
0x7fffda172eec	0x87654321

0x7fffda172eec	0x87654321
0x7fffda172ef0	0xda172eec

0x7fffda172eec	0x87654321
0x7fffda172ef0	0xda172eec
0x7fffda172ef4	0x7fff

Calling simple_show_b
0x7fffda172eec	0x12345678

0x7fffda172eec	0x12345678
0x7fffda172ef0	0xda172eec

0x7fffda172eec	0x12345678
0x7fffda172ef0	0xda172eec
0x7fffda172ef4	0x7fff

Calling float_eg
For x = 3490593
0x7fffda172ecc	0x354321
0x7fffda172ed0	0xda172f00
0x7fffda172ed4	0x7fff
0x7fffda172ed8	0x43fd0a6c

0x7fffda172ecc	0x4a550c84
0x7fffda172ed0	0xda172f00
0x7fffda172ed4	0x7fff
0x7fffda172ed8	0x43fd0a7c

For x = 3510593
0x7fffda172ecc	0x359141
0x7fffda172ed0	0xda172f00
0x7fffda172ed4	0x7fff
0x7fffda172ed8	0x43fd0aad

0x7fffda172ecc	0x4a564504
0x7fffda172ed0	0xda172f00
0x7fffda172ed4	0x7fff
0x7fffda172ed8	0x43fd0abd

Calling string_ueg
0x55c943fd0d44	0x44434241
0x55c943fd0d48	0x61004645
0x55c943fd0d4c	0x65646362
0x55c943fd0d50	0x61630066
0x55c943fd0d54	0x6e696c6c
0x55c943fd0d58	0x65742067

Calling string_leg
0x55c943fd0d4b	0x64636261
0x55c943fd0d4f	0x63006665
0x55c943fd0d53	0x696c6c61
0x55c943fd0d57	0x7420676e
0x55c943fd0d5b	0x5f747365
0x55c943fd0d5f	0x776f6873

一:头文件:①#include <stdio.h>包含了标准的库函数如输入scanf和输出printf;

②#include <stdlib.h>里面定义了五种类型、一些宏和通用工具函数。 类型例如size_t、wchar_t、div_t、ldiv_t和lldiv_t; 宏例如EXIT_FAILURE、EXIT_SUCCESS、RAND_MAX和MB_CUR_MAX等等;常用的函数如malloc()、calloc()、 realloc()、free()、system()、atoi()、atol()、rand()、srand()、exit()等等;

③#include <string.h>里面包含了strcpy(字符串拷贝)、strcat(字符串连接)、strcmp(比较两个字符串)等函数。

二:typedef unsigned char byte_pointer;:typedef的作用是为数据类型取一个新名字,这里是将unsigned char命名为byte_pointer;
size_t在64位系统下为unsigned long int,通常我们用sizeof()操作得到的就是size_t类型。

三:调用show_bytes时将传入无符号字符对象的指针和该对象的字节数,然后通过for循环的printf("%p\t0x%.2x\n", &start[i], start[i]); 进行格式化输出,以两位16进制数输出,start是对象的首地址,start[i]代表从start[0]开始第i个位置处的字节。

四:int main(int argc, char *argv[])其中第一个参数是记录在命令行输入的字符串个数,第二个参数是存放命令行上的字符串。

五:程序的运行结果还可以看出机器是大端存储还是小端存储方式。

六:关于unsigned char*、char*、int*三种不同指针类型运行结果不同的思考:

unsigned char* 带参数15213的运行结果:

CSAPP学习日志1:show_bytes

char* 带参数15213的运行结果:

CSAPP学习日志1:show_bytes

int* 带参数15213的运行结果:

CSAPP学习日志1:show_bytes
由图一和图二对比:当所表示的字节的值超过了char的表数范围(-128~127)时,byte本无符号的说法,但将byte的值赋给int、long等其它类型时,系统会最高位进行扩展。因此使用符号类型(char),可能会造成数据错误。

由图一和图三对比:由于start被解释成int型,所以 程序每次读取4个字节并以16进制打印,因此造成数据错误。