EXERCISES SECTION 5.10.3
Exercise 5.28: With the exception of the logical AND and OR, the order of eva luation of the binary operators is left undefined to permit the compiler freedom to provide an optimal implementation. The trade-off is between an efficient implementation and a potential pitfall in the use of the language by the programmer. Do you consider that an acceptable trade-off Why or why not
Exercise 5.29: Given that ptr points to a class with an int member named ival, vec is a vector holding ints, and that ival, jval, and kval are also ints, explain the behavior of each of these expressions. Which, if any, are likely to be incorrect Why How might each be corrected
(a) ptr->ival != 0 (b) ival != jval < kval
(c) ptr != 0 && *ptr++ (d) ival++ && ival
(e) vec[ival++] <= vec[ival]
free store 是C++(www.cppentry.com) 术语,指的是借助new/delete 动态管理的内存。在其他编程(www.cppentry.com)语言里,一般叫做堆(heap),例如,我们常说“在堆上分
配内存/ 对象”。但要注意,这与数据结构里讲的用于实现优先队列的堆(heap)是不同的。另外,C++(www.cppentry.com) 程序里也可以有堆(heap),一般
指的是 malloc/free 动态管理的内存,所以严格来说freestore 和heap 在C++(www.cppentry.com) 里的含义不同,不过在一般交流时可以统称为堆(heap),与
栈(stack)相对。
这里容易犯的一个错误是把方括号[] 写成了圆括号()。它们的意思完全不一样。比如原本打算分配1024 个元素的整数数组, 应写为int*pi = newint[1024];,再使用其中一些元素,例如pi[82] = 43; ,但是由于一时疏忽,误写为int* pi = new int(1024);,程序编译没有问题,但运行时会出错(如果“运气好”的话会直接崩溃)。应该尽量以std::vector 或tr1::array代替原生数组。