user-0 提问: malloc 的疑惑

回复:
user: user-1
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-2
content: 引用 8 楼 daxiaoyuyu 的回复:[quote=引用 1 楼 u011779875 的回复:] 问题1  tmpCenter.value = (double) i;  (pVecCenter + i)->value = (double) i; ->是指针指向其成员的运算符 .是结构体的成员运算符 为什么不能用 *(pVecCenter + i).value = (double) i; 呢？[/quote] 可以这么用吧 但是你要这么用吧 我没测试 我也很久没敲这样的代码了 . 和*的优先级是.大于* 且结和性不同 (*(pVecCenter + i)).value = (double) i;

user: user-0
content: 引用 1 楼 u011779875 的回复:问题1  tmpCenter.value = (double) i;  (pVecCenter + i)->value = (double) i; ->是指针指向其成员的运算符 .是结构体的成员运算符 为什么不能用 *(pVecCenter + i).value = (double) i; 呢？

user: user-3
content: 跟Associativity无关，是优先级的问题，.比*在这里的优先级高。  这样就可以： (*(pVecCenter + i)).value = (double) i;   更常见的是：  (pVecCenter + i))->value = (double) i;

user: user-4
content: 这程序的逻辑怎么这么别扭。。。

user: user-5
content: 引用 5 楼 max_min_ 的回复:每次malloc一定长度的空间大小的时候，其实实际上会比这个多几个字节的内存空间的 这个空间就是用来存放free结束的标志量的！所以每次free的时候给出首地址，都能正确释放对应的内存空间地址！  ++

user: user-6
content: 每次malloc一定长度的空间大小的时候，其实实际上会比这个多几个字节的内存空间的 这个空间就是用来存放free结束的标志量的！所以每次free的时候给出首地址，都能正确释放对应的内存空间地址！

user: user-1
content: 参考Linux的malloc和free相关源代码。

user: user-7
content: 申请堆上的空间，对于申请多少，free掉的是不是开始申请的，这是由系统负责完成的，我们是看不到的。

user: user-2
content: 问题2： 用 free(pVecCenter) 就能把分配的内存删掉了吗？free怎么会知道分配的内存空间有多少？难道说指针pVecCenter能够知道分配的内存空间的范围吗？  free是可以把pVecCenter 释放掉的 只要你释放的是正确的申请的地方 就会释放掉  free怎么会知道分配内存的多少？ 这个是malloc 貌似 啊 我敢说我一定对 我的理解是malloc管理一个内存区域的缓冲区的链表 你进行内存申请的时候 会对这个管理的内存缓冲区 空闲的地方进行申请 它会自动标示 你申请了哪个地方  这也就是为什么你申请不释放有时候不会出问题  也是为什么C++的new 与delete C语言的malloc 与free 要配对使用 它们管理的内存链表是不同的 所以应该是这样的

user: user-2
content: 问题1  tmpCenter.value = (double) i;  (pVecCenter + i)->value = (double) i; ->是指针指向其成员的运算符 .是结构体的成员运算符