`
jubincn
  • 浏览: 232057 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
文章分类
社区版块
存档分类
最新评论

6.087 Practical Programming in C, lec8

 
阅读更多

Void and function pointers. Hash tables.

Void pointers

• C does not allow us to declare and use void variables.

• void can be used only as return type or parameter of afunction.

• C allows void pointers

• Question: What are some scenarios where you want to pass voidpointers?

• void pointers can be used to point to any data type

• int x; void∗ p=&x;/∗points to int ∗/

• float f ;void∗ p=&f;/∗points to float ∗/

• void pointers cannot be dereferenced. The pointers shouldalways be cast before dereferencing.

void∗ p; printf ("%d",∗p);/∗ invalid ∗/

void∗ p; int ∗px=(int∗)p; printf ("%d",∗px); /∗valid ∗/

C中的void像数学中的“0”一样,看似很简单,但其出现具有重大意义。变量之所以不能声明为void,我认为是因为变量一声明就在内存中分配空间了,既然分配了空间,那么就不能叫void了。但变量有时候需要表示空,这时只好用NULL来表示,NULL是宏,其数值一般是0

无论什么类型的指针,其都具有一个“父类型”,指针。因此void指针也是指针,指向的数据类型为“空”而已。“万物皆空”,因此任何类型的指针都可以指向空。没用具体的类型,只知道起始位置,所占内存的长度无法得知,因此也无法正确地获取数据。若要获取数据,只好再赋类型给void指针。

Function pointers

• In some programming languages, functions are first classvariables (can be passed to functions, returned from functions etc.).

• In C, function itself is not a variable. But it is possible todeclare pointer to functions.

• Question: What are some scenarios where you want to passpointers to functions?

• Declaration examples:

• int (∗fp )( int ) /∗notice the () ∗/

• int (∗fp )( void∗,void∗)

• Function pointers can be assigned, pass to and from functions,placed in arrays etc.

• Can assign to a function pointer:

int (∗fp )( void ∗, void ∗) = strcmp_wrapper; or

int (∗fp )( void ∗, void ∗) = &strcmp_wrapper;

• Can call from function pointer: (str1 and str2 arestrings)

int ret = fp( str1 , str2 ); or

int ret = (∗fp )( str1 , str2 );

函数不能“变化”,即不能像变量一样被赋值。这也挺好理解,函数的作用就是复用一组操作,从而减轻程序员的工作量和减小程序体积,防止“重复发明轮子”。但这样就不能像变量一样“变化”,即程序里使用某个方法后,不能动态地用赋值方式改变。虽然不能将方法代码直接赋值,但我们可以获取它的指针,通过指针来调用方法。无论什么样的指针,其总是一个“指针”,即可以在内存中随意保存指针,并且不会对程序体积有较大影响且不影响方法的复用。

从方法指针也可以看出,方法这种“类型”由哪些要素决定。至少包括返回类型和参数类型。

使用函数指针是因为要灵活性,即不确定用哪个方法,因此在框架中和类库中应该有大量的应用。

Callbacks

Definition: Callback is a piece of executable code passed tofunctions. In C, callbacks are implemented by passing functionpointers.

Example:

void qsort(void∗ arr , int num,int size ,int (∗fp )( void∗pa,void∗pb))

• qsort() function from the standard library can be sort anarray of any datatype.

• Question: How does it do that? callbacks.

• qsort() calls a function whenever a comparison needs to bedone.

• The function takes two arguments and returns (<0,0,>0)depending on the relative order of the two items.

回调函数是传给方法的一组可执行代码,即是一个指令流和数据流而不仅仅是数据。在函数式编程语言中,遍布回调函数,因为其运行机制就是不断地解函数,并且里面的函数是可以像变量一样赋值。

Hash table

Hash tables (hashmaps) combine linked list and arrays to providean efficient data structure for storing dynamic data. Hash tablesare commonly implemented as an array of linked lists (hash tableswith chaining).


哈希表将一个稀疏的数据分布映射到一个较为密集的分布,并且通过映射函数达到或接近随机存储的速度。hash的效率取决于映射函数。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics