I. Memory, memory
1. Memory partition: high (large memory address) stack bottom stack zone heap Zone
Static zone constant Zone
Low (small memory address) code Zone 2. STACK: data structure (1) features: advanced post-release (2) Operations: push (pop) (3) stack from high to low allocation space char a = 0; char B = 0; char c = 0; printf ("a: % p \ n", & ); printf ("B: % p \ n", & B); printf ("c: % p \ n", & c); (4) stack from low to high using char n1 = 1; char n2 = 0; short * p = & n2; printf ("% d \ n", * p); (5) function: The system allocates stack int fun1 () {int n1 = 200; return n1;
} Int fun2 () {int n1; return n1;} int main () {fun1 (); int m = fun2 (); printf ("% d \ n ", m );
} After the main function is allocated to the stack, it runs to fun1 and redistributes the stack. After the function is executed, it marks n1 as deleted, but the data is retained. After the function is executed to fun2, it marks the same, obtain the data retained in the previous stack. Then, m copies the data and outputs m = 200. (6) equal signs are executed from right to left (7) Mark deletion (no data deletion) (8) memory mechanism: data will not be cleared, but only overwrite (9) Note: write the variable to assign the initial value (10) system allocation and reclaim the memory (11) the variable created in the function is allocated in the stack area int a = 10; // a is stored in the stack area of the memory (the system recycles the result when curly braces exist. heap (1) dynamic memory allocation, (2) keyword malloc, release memory free (); (3) allocated and recycled by the user (programmer). If the programmer does not recycle the memory, therefore, the less memory is used, the less memory is used, and the program crashes. If not, the program will be safe and sound. (4) When the program exits, the heap memory is reclaimed by the system (keyword free) 4. static zone (Global Zone) (1) variables defined in the external body of the function are opened up by the system, and the program running process is always there until the program exits and is recycled by the system. (2) variables are not defined in any function body int x = 8; // x is stored in the static zone int main () {}; (3) features: ① only initialize once ② If no initial value is assigned during initialization, the default value is 0 ③ static variable space is released only when the program exits (the space will always exist during the program running) (4) Purpose: ① extend the life cycle of a variable ② do not increase the scope of the variable (5) Keyword: staticfor (int I = 0; I <5; I ++) {// int m = 0; static int m = 0; // determines whether a variable exists in the static zone. The result is equivalent to placing int m = 0 out of the for loop.
Printf ("% d \ n", m ++ );
} 5. the data in the constant area (1) is stored in the constant area, and the content in the constant area cannot be changed (2) floating point number, integer number, character number int a = 10; // 10 there is a constant area. 6. code area (1) the compiling area where the source code is stored. All written code, the function is compiled into binary and placed in the code area (2) the CPU commands generated after all statements are compiled are stored in the Code Area