《深入理解计算机系统》学习心得二:关于show-bytes的 学习

本文介绍了一个C语言程序,通过强制类型转换来访问并打印不同数据类型的字节表示。该程序包括了整型、浮点型及指针类型的字节展示,并通过实例演示了小端法与大端法的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        此段代码,使用强制类型转换来访问和打印不同程序对象的字节表示。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 */

   代码说明

#include <stdio.h>

在程序中用到系统提供的标准函数库中的输入输出函数时,应在程序的开头写上一行:#include"stdio.h"或者是#include<stdio.h>,这样才能调用库函数。二者主要在于查找效率上有差别,#include<stdio.h>一般用包含系统文件,它是查找先从系统目录查找开始查找;#include "stdio.h"一般用包含项目文件,它是查找先从项目目录查找开始查找。

在编写C语言中,常用到printf()和scanf()函数,他们就是stdio.h中的两个标准输入输出函数,所以编程语句中如果要用到此两个函数就一定要在头文件中加入#include<stdio.h>。

#include <stdlib.h>

stdlib 头文件即standard library标准库头文件。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目录里面的stdlib.h头文件查看。

#include <string.h>

使用字符串的函数的时候需要添加此头文件。比如如下常用函数:
strlen求字符串长度
strcmp比较2个字符串是否一样
strcat字符串连接操作
strcpy字符串拷贝操作
strncat字符串连接操作(前n个字符)
strncpy字符串拷贝操作(前n个字符)
strchr 查询子串
strstr 查询字串

typedef unsigned char *byte_pointer;

我们用typedef将数据类型byte_pointer定义为一个指向类型为“unsigned char”的对象的指针。这样一个字节指针引用一个字节序列,其中每个字节都会被认为是一个非负整数。

void show_bytes(byte_pointer start, size_t len)

这个函数有一个类型为byte_pointer的参数start。在c语言中,我们能够用数组表示法来引用指针,同时我们也能用指针表示法来引用数组参数。在这个例子中,引用start[i]表示我们想要读取以start指向的位置为起始的第i个位置处的字节。

第一个例程show-bytes的输入是一个字节序列的地址,它用一个字节指针以及一个字节数来指示。该字节数指定为数据类型size_t,表示数据结构大小的首选数据类型。

printf("%p\t0x%.2x\n", &start[i], start[i]);

C格式化指令“%.2x”表明整数必须用至少两个数字的十六进制输出。 在格式串里,每个以“%”开始的字符序列都表示如何格式化下一个参数。 “%p”表示指针输出。“%d”表示输出一个十进制整数。“%f”是输出一个浮点数。“%c”是输出一个字符,其编码由参数给出。

Tab是制表符,就是"\t",作用是预留8个字符的显示宽度,用于对齐

详情请见:c语言printf()输出格式大全(转载)

printf("\n");

在c语言中,换行(\n)就是光标下移一行却不会移到这一行的开头,回车(\r)就是回到当前行的开头却不向下移一行.
Enter键按下后会执行\n\r这样就是我们看到的一般意义的回车了,所以你用16进制文件查看方式看一个文本,就会在行尾发现"\n\r"

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

过程show-int 、show-float和show_pointer展示了如何使用程序show_bytes来分别输出类型为int、float和void *的c程序对象的字节表示。可以观察到他们仅仅传递给show_bytes一个指向它们参数x的指针&x,且这个指针被强制转化为“unsigned char *”。这种强制类型转换告诉编译器,程序应该把这个指针看成指向一个字节序列,而不是指向一个原始数据类型的对象。然后,这个指针会被看成是对象使用的最低字节地址。强制转换类型运算符可以将一种数据类型转换成另一种。因此,强制类型转换(byte_pointer)&x表示无论指针&x以前是什么类型,他现在就是一个指向数据类型为unsigned char 的指针。

这些过程使用c语言的运算符sizeof来确定对象使用的字节数。一般来说,表达式sizeof(T)返回存储一个类型为T的对象所需要的字节数。使用sizeof而不是一个固定的值,是向编写在不同机器类型上可移植的代码迈进了一步。

一、C语言变量名的命名规则:(可以字母,数字,下划线混合使用) 
1. 只能以字母或下划线开始; 
2. 不能以数字开始; 
3. 一般小写; 
4. 关键字不允许用(eg:int float=2//error  float 为保留字不允许用);

二、函数名的命名规则 
1.见名知意; 
2.自定义函数函数名首字母大写(库函数里的函数名都是以小写字母定义,为了区分库函数和自定义函数,避免冲突)。 

三、宏定义里面的变量 全大写 
eg: 
#define SIZE 100(后面函数所有出现的SIZE全用100代替,它在所有函数执行前先执行)

原文:变量函数命名规则(转载)

/* $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运行结果

不带参数的运行结果:./bytes 
unsigned char不带参数的运行结果:./bytes 
不带参数的运行结果
unsigned char不带参数的运行结果:./bytes 

 

./bytes 65535
unsigned char带一个参数的运行结果   ./bytes 65535
带一个参数的运行结果  ./bytes 3.14
unsigned char带一个参数的运行结果  ./bytes 3.14
带一个参数的运行结果 ./bytes -32768
unsigned char带一个参数的运行结果 ./bytes -32768

 

char 不带参数的运行结果 ./bytes
char 不带参数的运行结果 ./bytes
char 不带参数的运行结果 ./bytes
char 不带参数的运行结果 ./bytes
带一个参数的运行结果 ./bytes 65535
char带一个参数的运行结果 ./bytes 65535

 

typedef int *byte_pointer的运行结果

 

int 不带参数的运行结果 ./bytes
int 不带参数的运行结果 ./bytes
int 不带参数的运行结果 ./bytes
int 不带参数的运行结果 ./bytes
int 带一个参数的运行结果 ./bytes 65535
int 带一个参数的运行结果 ./bytes 65535

 

解答为何要以unsigned char作为类型:

通过对测试结果进行分析,当定义指针为int型时,地址会加4;当定义指针类型为有符号的char型时, 假如遇到最高位为1时,则会出现补符号位置的形式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值