码迷,mamicode.com
首页 > 其他好文 > 详细

第二章 接口与实现

时间:2014-11-15 17:02:26      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   os   使用   sp   

2 Exercises

2.1 A preprocessor macro and conditional compilation directives, such as #if, could have been used to specify how division

truncates in Arith_div and Arith_mod. Explain why the explicit test -13/5 == -2 is a better way to implement this test.

如果使用预处理指令和条件编译指令去指定Arith_div和Arith_mod如何取整的话是可以的,

但这些都是与运行时环境相关的,需要针对不同运行环境编写不同实现。

而像 -13/5 == 2这样去测试的话,无论运行时环境如何,都可以在运行时由当前环境去判断应该如何取整。

 

2.2 The -13/5 == -2 test used in Arith_div and Arith_mod works as long as the compiler used to compile arith.c does arithmetic

thesame way as Arith_div and Arith_mod do when they are called. This condition might not hold, for example, if arith.c were compiledby

a cross-compiler that runs on machine X and generates code for machine Y. Without using conditional compilation directives,

fix arith.c so that such cross compilations produce code that is guaranteed to work. 

这题目感觉本身有点问题 当 -13/5 == 2 不成立时自然会执行else分支,返回的结果应该是一样的。

 

2.3 Like all ADTs in this book, the Stack interface omits the specification “it is an unchecked runtime error to pass a foreign

Stack_T to any routine in this interface.” A foreign Stack_T is one that wasnot manufactured by Stack_new. Revise stack.c

so that it can check for some occurrences of this error. One approach, for example, is to add a field to the Stack_T

structure that holds a bitpatternunique to Stack_Ts returned by Stack_new. 

1 struct T {
2   int count;
3   struct elem {
4     void *x;
5     struct elem *link;
6   } *head;
7   int identifer;
8 };

 

2.4 It’s often possible to detect certain invalid pointers. For example, a nonnull pointer is invalid if it specifies an address outside

the client’s address space, and pointers are often subject to alignment restrictions; for example, on some systems a pointer to a

double must be a multiple of eight. Devise a system-specific macro isBadPtr(p) that is one when p is an invalid pointer so that

occurrences of assert(ptr) can be replaced with assertions like assert(!isBadPtr(ptr)).

/* 判断是否在本程序空间,这个和系统有关吧,不懂实现 */
#define isBadPtr(p) (p && (p%8 == 0))


2.5 There are many viable interfaces for stacks. Design and implement some alternatives to the Stack interface. For example,

one alter native is to specify a maximum size as an argument to Stack_new.

Stack_new 增加指定最大分配空间接口,主要难点是在入栈时对当前空间大小的判断。

 

第二章 接口与实现

标签:des   style   blog   io   color   ar   os   使用   sp   

原文地址:http://www.cnblogs.com/cers/p/4099482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!