2009年7月8日星期三

Reading note - memory management

book1: Memory as a Programming concept in c and c++ 已经打印了.
book2: c++ pointers and dynamic menory management. Michael C. Daconta.

Memory location: a container that can store a binary number.
Address: a unique binary number assigned to every memory location. 特别的二进制数。
Pointer: a memory location that stores an address
Variable:a named memory location that can store a value of certain data type..[book2 chapter2]

There is absolutely no difference in functionality between pointers and references. The difference is that with references, the compiler handles the details of passing pointers and dereferencing them instead of you doing it yourself.
1. A reference is an alias for a variable, is not an object, while a pointer is an object. An object has two main attributes: its storage class and its type. The storage class determines the lifetime of this chunk of storage, while the type determines the meaning of values found in the object.
2. References exist only in the compiler and not after compilation. They have symbol table entries only.
3. 初始化时候要给出初值,除了在class中是在初始化列表中的,不能做数学操作.

Array name, is a label, the label of the first address, is not the same as a pointer.

Three memory spaces: [book1,2]
1. global: for global variables(函数之外没有被static关键字的, 函数之内有static关键字的,类声明中static的variables), allocated at compile.
2. stack: activation frame when a function is called, the local automatic variables.
3. free store: binary heap(system heap or free store) at os level keeped by os memory management, dynamic list of free segments at process level keeped by process memory manager.[book1 chapter4].

malloc/new.
malloc 自己用sizeof觉得分配的字节数,返回的是void*,分配得到的空间之内是垃圾数据;
而new在这三方面都有所不同。

Pass by value: cope the variable's content to the activation frame(function stack).
Pass by reference: cope the variable's address to the activation frame.

在datastructure中找linklist的笔记, 也涉及pointer的使用, 学会画图来表示, 32bit里面是variable的地址呢,还是pointer的地址呢?这就是一级pointer和二级pointer的区别
特别是那个push(struct node *head, int data)的为什么错了, 而需要改成push(struct node **head, int data)?
一定要学着画图, 并且结合以下的概念,
函数传递struct node,那么是将整个structure copy一份到被调用函数的activation frame;
如果函数传递的是struct node*,那么是将structure的地址,也就是指针本身的context copy一份到被调用函数的activation frame;
如果函数传递的是struct node**,那么是将指针本身的address copy一份到被调用函数的activation frame, 这样就修改到指针本身了.

没有评论:

发表评论