user-0 提问: 对APUE 习题7.10不解

回复:
user: user-1
content: 意犹未尽，补充一下，如果编译的时候开了优化，对于这段代码，执行的结果应该是随机的，但如果在： val=5; ptr=&val; 中间加一个print("%d\n",val); 执行的结果仍然是6。这是因为gcc对自变量和寄存器变量的优化方式的问题。 APUE是好书，但是有的地方说的不是很清楚，而且有一些错误的地方，总之，实验得真理。

user: user-1
content: 意犹未尽，补充一下，如果编译的时候开了优化，对于这段代码，执行的结果应该是随机的，但如果在：val=5；

user: user-1
content: 楼主没有按照书上的代码来，书上，ptr在申明的时候就初始化了。作者想说明的问题是自变量的作用域。复合语句中的自变量作用域都局限在复合语句中，一旦跳出符合语句，就不能访问。能执行成功是因为，val把地址给了ptr。有人可能要说自变量在不是在跳出复合语句就释放了吗？尽信书不如无书，其实不是这样的，不信试试看。

user: user-2
content: 引用 1 楼 user-6 的回复:代码执行会不会出错得看程序执行时ptr的值，ptr定义没有初始化，如果if语句进入了，执行不会出错；如果if语句没有进入，那么程序会不会奔溃就不知道了。这个函数存在两个问题，1）使用未初始化的指针，是个隐患，不安全 2）指针指向已销毁不存在的变量（从语法角度），不合理+1，未初始化+临时变量

user: user-0
content: 引用 3 楼 q191201771 的回复:[quote=引用 2 楼 q191201771 的回复:] 因为ptr指向的val的声明周期出if那个大括号就结束了 吧 我打错字了 声明改成生命[/quote]  但是程序可以正常工作是为什么呢，比如 int a = f1(5); 结果a确实等于6啊

user: user-3
content: //C++ Operators //  Operators specify an evaluation to be performed on one of the following: //    One operand (unary operator) //    Two operands (binary operator) //    Three operands (ternary operator) //  The C++ language includes all C operators and adds several new operators. //  Table 1.1 lists the operators available in Microsoft C++. //  Operators follow a strict precedence which defines the evaluation order of //expressions containing these operators.  Operators associate with either the //expression on their left or the expression on their right;    this is called //“associativity.” Operators in the same group have equal precedence and are //evaluated left to right in an expression unless explicitly forced by a pair of //parentheses, ( ). //  Table 1.1 shows the precedence and associativity of C++ operators //  (from highest to lowest precedence). // //Table 1.1   C++ Operator Precedence and Associativity // The highest precedence level is at the top of the table. //+------------------+-----------------------------------------+---------------+ //| Operator         | Name or Meaning                         | Associativity | //+------------------+-----------------------------------------+---------------+ //| ::               | Scope resolution                        | None          | //| ::               | Global                                  | None          | //| [ ]              | Array subscript                         | Left to right | //| ( )              | Function call                           | Left to right | //| ( )              | Conversion                              | None          | //| .                | Member selection (object)               | Left to right | //| ->               | Member selection (pointer)              | Left to right | //| ++               | Postfix increment                       | None          | //| --               | Postfix decrement                       | None          | //| new              | Allocate object                         | None          | //| delete           | Deallocate object                       | None          | //| delete[ ]        | Deallocate object                       | None          | //| ++               | Prefix increment                        | None          | //| --               | Prefix decrement                        | None          | //| *                | Dereference                             | None          | //| &                | Address-of                              | None          | //| +                | Unary plus                              | None          | //| -                | Arithmetic negation (unary)             | None          | //| !                | Logical NOT                             | None          | //| ~                | Bitwise complement                      | None          | //| sizeof           | Size of object                          | None          | //| sizeof ( )       | Size of type                            | None          | //| typeid( )        | type name                               | None          | //| (type)           | Type cast (conversion)                  | Right to left | //| const_cast       | Type cast (conversion)                  | None          | //| dynamic_cast     | Type cast (conversion)                  | None          | //| reinterpret_cast | Type cast (conversion)                  | None          | //| static_cast      | Type cast (conversion)                  | None          | //| .*               | Apply pointer to class member (objects) | Left to right | //| ->*              | Dereference pointer to class member     | Left to right | //| *                | Multiplication                          | Left to right | //| /                | Division                                | Left to right | //| %                | Remainder (modulus)                     | Left to right | //| +                | Addition                                | Left to right | //| -                | Subtraction                             | Left to right | //| <<               | Left shift                              | Left to right | //| >>               | Right shift                             | Left to right | //| <                | Less than                               | Left to right | //| >                | Greater than                            | Left to right | //| <=               | Less than or equal to                   | Left to right | //| >=               | Greater than or equal to                | Left to right | //| ==               | Equality                                | Left to right | //| !=               | Inequality                              | Left to right | //| &                | Bitwise AND                             | Left to right | //| ^                | Bitwise exclusive OR                    | Left to right | //| |                | Bitwise OR                              | Left to right | //| &&               | Logical AND                             | Left to right | //| ||               | Logical OR                              | Left to right | //| e1?e2:e3         | Conditional                             | Right to left | //| =                | Assignment                              | Right to left | //| *=               | Multiplication assignment               | Right to left | //| /=               | Division assignment                     | Right to left | //| %=               | Modulus assignment                      | Right to left | //| +=               | Addition assignment                     | Right to left | //| -=               | Subtraction assignment                  | Right to left | //| <<=              | Left-shift assignment                   | Right to left | //| >>=              | Right-shift assignment                  | Right to left | //| &=               | Bitwise AND assignment                  | Right to left | //| |=               | Bitwise inclusive OR assignment         | Right to left | //| ^=               | Bitwise exclusive OR assignment         | Right to left | //| ,                | Comma                                   | Left to right | //+------------------+-----------------------------------------+---------------+

user: user-4
content: return (*ptr + 1);  这返回的是int，不是指针类型。

user: user-5
content: 引用 2 楼 q191201771 的回复:因为ptr指向的val的声明周期出if那个大括号就结束了 吧 我打错字了 声明改成生命

user: user-5
content: 因为ptr指向的val的声明周期出if那个大括号就结束了 吧

user: user-6
content: 代码执行会不会出错得看程序执行时ptr的值，ptr定义没有初始化，如果if语句进入了，执行不会出错；如果if语句没有进入，那么程序会不会奔溃就不知道了。这个函数存在两个问题，1）使用未初始化的指针，是个隐患，不安全 2）指针指向已销毁不存在的变量（从语法角度），不合理