section1: Program Stack and Heap
section2: Passing and Returning by Pointer
section3: Function Pointers

section1: Program Stack and Heap

stack frame holds local(automatic) variables.

1.1 Program Stack
     The program stack is an area of memory that supports the execution of functions and is normally shared with the heap. That is , they share the same region of memory. The program stack tends to occupy the lower part of this region, while the heap uses the upper part.
     The program stack holds stack frames, sometimes called activation records or activation frames.
     Stack frames hold the parameters and local variables of function.

     e.g.
     void function2( ) {
          Ojbect *var1 = …;
          int var2;
          printf(“Program Stack Examle\n”);
     }
     
     void function1( ) {
          Object *var3 = …;
          function2( );
     }

     int main( ) {
          int var4;
          function1( );
     }
     
     
1.2 Organization of a Stack Frame 

     A stack frame consists of several elements, including:
     (1) Return address:
     (2) Storage for local data;
     (3) Storage for parameters;
     (4) Stack and base pointers: Pointers used by the runtime system to manager the stack. (not be concerned)

     A stack pointer usually pointer to the top of the stack.
     A stack base pointer (frame pointer) is often present and points to an address within the stack frame, such as return address.
     Neither of these are C pointers.

     

     

     When the stack frame is created, the parameters are pushed onto the frame in the opposite order of their declaration, followed by the local variables.
    
     stack overflow...
     Keep in mind that each thread is typically allocated its own program stack.

section2: Passing and Returning by Pointer

     Parameters, including pointers, are passed by VALUE. That is , a copy of the argument is passed to the function.

2.1 Passing Data using a Pointer

    
    
     
2.2 Passing Data by Value
     
    
     


2.3 Passing a pointer to a Constant
 
PS: lvalue refers to the operand found on the left side of the assignment operator. e.g. *pi = 100,   *pi is a lvalue.
    
    
    

2.4 Returning a Pointer
     two techniques:
     (1) Allocate memory within the function using malloc and return its address.
          The caller is responsible for deallocating the memory returned.
         
          45 * 5
          This memory will eventually need to be freed.    free(vector);

          Potential problems:
          i) Returning an uninitialised pointer;
          ii) Returning a pointer to an invalid address;
          iii) Returning a pointer to a local variable;
          iv) Returning a pointer but failing to free it. (is typified by the allocoateArray)

     (2) Pass an object to the function where it is modified.
          This makes the allocation and deallocation of the object’s memory the caller’s responsibility.
          NOT recommended.  refer to the section “Passing Null Pointers” on page 67.

2.5 Pointer to Local Data
     
     



modification:
     
     

2.6 Passing Null Pointers
    
     
     
     
2.7 Passing a Pointer to a Pointer
     When a pointer is passed to a function, it is passed by value. 
     If we want to modify the original pointer and not the copy of the pointer, we need to pass it as a pointer to a pointer.
    
     

这个例子综合了之前的几乎所有知识点。
1. main中,定义了int 类型的指针 *vector,  stack中给定地址为500, 即&vector=500, vector=null, *vector=null;
2. foo中,arr is a pointer to a pointer to int, &arr=?,  arr=&vector=500,  *arr=vector=null, **arr=*vector=null;
3.    malloc return a pointer to a void, a address = 600; 
4. *arr = vector = 600; // 相当于一般的malloc一个array; *arr, vector是一个数组的指针了
5. *arr+i :  这是对指针进行数值操作,指针往后走
6. *(*arr+i) : 数组指针的*  操作,lvalue,  indirection instead of dereferencing

e.g. A simple pointer will not work:
     
     
When vector is passed to the function, its value is copied into the parameter arr.


2.7.1 Writing your own free function
     The free function does not check the pointer passed to see whether it is NULL and does not set the pointer to NULL before it returns.

    
     main:
    
    
     output:
     

section3: Function Pointers

     A function pointer is a pointer that holds the address of a function.
     Using for executing function at runtime.

3.1 Declaring Function Pointers

     passed void and returned void:
          void (*foo)( );
examples:
     int (*f1)(double)
     void (*f2)(char*)
     double* (*f3)(int, int)

confuse functions:
     int *f4( );             // declares f4 as  a function that returns a pointer to an integer
     int (*f5) ( );          // f5 is a function pointer that returns an integer
     int* (*f6) ( );         // f6 is a function pointer that returns a pointer to integer

3.2 Using a Function Pointer

    
     少了*, faint, &符号可以省略,推荐省略。

     

    



alternative( using typedef ): 
     

3.3 Passing Function Pointers
     
     
     main:
    
    
     11, -1

3.4 Returning Function Pointers
    
    

3.5 Using an Array of Function Pointers

     

http://en.wikipedia.org/wiki/ASCII

3.6 Comparing Function Pointers

     

3.7 Casting Function Pointers