《CSAPP》读书杂记 - Chapter 2. Representing and Manipulating Information
1. 一段查看地址内容的代码
代码:
#include <stdio.h> typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, int len)
{
int i;
for(i = ; i < len; i++)
{
printf(" %.2x", 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 *));
} void test_show_bytes(int val)
{
int ival = val;
float fval = (float)ival;
int *pval = &ival;
printf(" %x\n", pval);
show_int(ival);
show_float(fval);
show_pointer(pval);
} int main()
{
test_show_bytes();
}
运行结果:
56683c18
39 30 00 00
00 e4 40 46
18 3c 68 56 ff 7f 00 00
其中函数show_int()、show_float()的调用比较好理解。
show_int()中&x取到实际地址。为使获取地址指向数据中每个字节的内容,需要进行类型转换,即把int *转换成unsigned char *,但是类型转换后已经不知道数据的长度,所以需要人工传入数据长度的参数,即 sizeof(int)。此后,把每次指针指向的内容(unsigned char形式)以十六进制形式输出;若指针是以char形式表示,则转换为十六进制时会认为有符号,所以会出现补全ffffff的情况。show_float()类似。
int *pval = &val 运行后,pval为指针,64位,其内容指向val的地址。printf(" %x\n", pval)是以十六进制形式输出地址,但是由于 %x只能表示32位,所以去掉高32位,变成 56 68 3c 18,小端模式输出后即为 18 3C 68 56
对于show_pointer函数,传递的是 int*指针,实参强制转换为void*指针,而&x转换为指向void*指针的指针,即void**指针,要访问void*指针的内容,需要void*指针的地址,即void**指针和void*指针的长度。之后情况与上面类似。
2. 问题2.27
写一个判断无符号加法是否溢出的函数
int uadd_ok(unsigned x, unsigned y)
{
unsigned sum = x + y;
return sum >= x;
}
若无溢出,则 sum >= x 恒成立
若溢出,则 sum = x + y - 2^w,由于 y < 2^w,则 sum < x
《CSAPP》读书杂记 - Chapter 2. Representing and Manipulating Information的更多相关文章
- chapter 2: Representing and manipulating information
C allows conversion between unsigned and signed. The rule is that the underlying bit representation ...
- Chap 2 Representing and Manipulating Information (CS:APP)
-------------------------------------------------------------------------------------------------- A ...
- CSAPP读书随笔之一:为什么汇编器会将call指令中的引用的初始值设置为-4
CSAPP,即<深入理解计算机系统:程序员视角>第三版,是一本好书,但读起来确需要具备相当的基本功.而且,有的表述(中译文)还不太直白. 比如,第463页提到,(对于32位系统)为什么汇编 ...
- MySQL Crash Course #13# Chapter 21. Creating and Manipulating Tables
之前 manipulate 表里的数据,现在则是 manipulate 表本身. INDEX 创建多列构成的主键 自动增长的规定 查看上一次插入的自增 id 尽量用默认值替代 NULL 外键不可以跨引 ...
- csapp读书笔记-并发编程
这是基础,理解不能有偏差 如果线程/进程的逻辑控制流在时间上重叠,那么就是并发的.我们可以将并发看成是一种os内核用来运行多个应用程序的实例,但是并发不仅在内核,在应用程序中的角色也很重要. 在应用级 ...
- 【Reading Note】算法读书杂记
1 排序 排序基本信息 稳定性:排序前大的数在排序后,大的数依然保持不变就是稳定排序,反之不稳定 内外排序:根据待排序的记录是否放在内存里面区分的.诸如:插入排序(直接插入&希尔).交换排序( ...
- 【Reading Note】Python读书杂记
赋值 >>> list=[] >>> app=[list,list,list] >>> app [[], [], []] >>> ...
- 《OS X Mountain Lion》 读书杂记
OS X是一个类UNIX操作系统,由底层的Darwin和上层的OS X应用程序框架(Cocoa, Carbon, Quartz等)及Aqua用户界面组成.其中Darwin是一个开源.完整的POSIX- ...
- CSAPP 读书笔记 - 2.31练习题
根据等式(2-14) 假如w = 4 数值范围在-8 ~ 7之间 2^w = 16 x = 5, y = 4的情况下面 x + y = 9 >=2 ^(w-1) 属于第一种情况 sum = x ...
随机推荐
- Material Design之FloatingActionButton的使用
FloatingActionButton是继承至ImageView,所以FloatingActionButton拥有ImageView的全部属性. CoordinatorLayout能够用来配合Flo ...
- Do we need other languages other than C and C++?
There were hundreds of or thousands of programming languages created since the invention of computer ...
- C#中byte[] 与指针
本文假定读者熟悉byte[].指针. C#是类型安全的,默认是不允许使用指针,但是针对C\C++或者其他语言的程序员(delphi)转为使用C#的的工作人员,不适用指针觉得很别扭.下面介绍一下基础的指 ...
- repeater 一个td多个div显示图片
<table class="table table-bordered table-responsive"> <tbody> <asp:Repeater ...
- [DevExpress]图表开发工具类 ChartUtils
/// <summary> /// 基于.NET 3.5的Chart工具类;对应的DevExpress版本:12.1.7; /// </summary> public stat ...
- 【转】Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask
方法一:(java习惯,在android不推荐使用) 刚刚开始接触android线程编程的时候,习惯好像java一样,试图用下面的代码解决问题 new Thread( new Runnable() { ...
- int指令(软件中断指令)
INT(软件中断指令)是CALL指令的一种特殊形式.call指令调用调用的子程序是用户程序的一部分,而INT指令调用的操作系统提供的子程序或者其他特殊的子程序. 中断服务子程序和标准过程的最大区别是 ...
- sfs
http://tieba.baidu.com/p/3397811202 http://mooc.guokr.com/post/610664/ http://home.ustc.edu.cn/~boj/ ...
- Server2008系统 FTP下载“当前的安全设置不允许”的解决方法
IE -> Internet选项 -> 安全 -> Internte -> 自定义级别 设置 -> 下载 -> 文件下载 -> 启动
- VirtualBox安装linux增强工具报错
错误提示: Building the OpenGL support module [FAILED] 解决办法 cd /media/VBOXADDITIO ...