深入理解计算机系统
不说废话了,直接进入主题。
本学期一直在搞算法,从五大经典算法,到图论的一些算法,期间准备数学建模的比赛,还甚至接触了一点三大智能算法的影子(确切的说只接触了遗传算法),算法中时间复杂度是十分关键的东西。之后看了不周山的博客,上面写道:
“”
“学习算法的时候肯定离不开思考时间复杂度和空间复杂度,但 O(n3)O(n3) 真的很糟糕,O(1)O(1) 真的就很好吗?虽然在单纯的算法分析中是如此,但是在计算机系统中,算法只是一小部分。假设一个 O(1)O(1) 的算法会导致死锁,虽然看起来比 O(n3)O(n3) 的算法好得多,然而真正执行起来,可能就是无尽的等待了。
程序执行并不是一锤子买卖,从算法到数据表示再到程序流程,从内存到缓存再到运算器。不理解计算机系统本身,不理解程序是如何编译执行,又怎么能够写出好程序呢?
”
到这里才些许明白这本书的巨大意义,故而,写下系列博客。
//该程序的本质是用char类型来代替一个字节(因为char本身就是一个字节),然后,一个char代表着一个字节,这样,通过将char类型的数据转换,打印出来了各种类型在机器中的代码表示,当然,也同时可以展示出机器的大端和小端模式。也可以展示出数据的具体存放。
/* 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显示一下指针的类型,比较少见
printf("\n");
}
void show_int(int x) {
show_bytes((byte_pointer) &x, sizeof(int)); //sizeof()万分注意,不是一个函数,而是一个运算符
}
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[])//带参数的main函数,前面代表参数的个数,后面将参数放进制定的字符串中
//这样我们由此得出了两种main(),一种在虚拟机中直接在运行时的参数直接进行指定参数
//另一种则是在运行后再次输入当然结果没有什么不同
{
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
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
*/
因为这应该是很久之前的所做的,相对而言写的比较粗略,大概是将其大意讲清楚,之后,会陆陆续续的将自己在看书时发现的一些好玩的东西和一些CMU镇校神课上的一些现阶段可以做的实验,发上来。同时为自己五一食言,说自己在那时会发7,8篇博客结果一篇都没发而忏悔,实在是自己陷入数据结构和DFS的漩涡中,没有自拔,忘记了这件事。(最后真心评价一下浙大mooc的数据结构真心不错,一些课后讨论和课上讲的一些思路看法与学校里讲的不太相同,确实是深入浅出,尤其在树和图的那部分,比我用的教材讲的精妙许多,有时间也会发上几篇博客上来)