Code Area
Code area codes, when the program is loaded into memory by the operating system, all executable code is loaded into the code area, also called the code snippet, this block of memory can not be modified during the run.
Static Zone
All global variables and static variables in the program are stored in the static area.
Stack Area
Stack stack is an advanced memory structure, all automatic variables, function parameters are automatically released by the compiler stack, when an automatic variable beyond its scope, automatically ejected from the stack. For automatic variables, when to stack, when out of the stack, is not required program control, by the C language compiler. Implementation of the stack is not very large, generally in K units.
When the stack space is full, but also to the stack memory pressure variable, this is called the stack. Overflow for a 32-bit operating system, the maximum management of 4G memory, where 1 G is for the operating system itself, the remaining 3G is to the user program, a user program can theoretically use 3G of memory space.
Note: The order of function parameters in the C language is from right to left.
Heap Area
Heap heap, like a stack, is a memory area that can be modified at any time while the program is running, but not in the same order as the stack. Heap is a large container, its capacity is much larger than the stack, but in the C language, heap memory space application and release need to manually through the code to complete.
code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#Include<stdio.h>
int c =0;Static Zone
voidTest(int A,int b)Formal parameters A, B are all in the stack area { printf"%d,%d\n", &a, &b); }
int *Geta()The return value of the function is a pointer { int a =100;Stack area Return &a; }The scope of int A is this {}
IntMain() { int *p = Geta ();This gets the address of a temporary stack variable that is invalid after the function Geta call is complete. *p =100; printf"%d\n", *p); Staticint d = 0; //static zone int a = 0; //stack area int b = 0; printf ( test (A, b); return 0; /* Output 100 2619740, 2619728, 9404720, 9404724, 9376059 2619512, 2619516 */ |
Heap Usage Considerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#Include<stdio.h> #Include<stdlib.h>
int *Geta()Error, the address of a stack variable cannot be returned by the return value of the function { int a =0; Return &a; }
int *Geta1()You can return a heap address from the return value of a function, but remember, be sure to free { int *p = (int *)malloc (sizeof (int));Applied for a heap of space return p; }
int *Geta2()Legal, but remember, you can't use free here. { Staticint a =0;The variable is in the static area, the program has been in existence during operation Return &a; }
voidGetheap(int *p) { printf"P =%p\n", &p); p = (int *)malloc (sizeof (int) *10); }After Getheap execution, p disappears, causing the address number of the specific heap space he points to disappear. A memory leak has occurred here.
void getheap1 (int **p) { *p = (int *) malloc (sizeof ( int) * 10); } //the operation is correct int main () { int *p = null; printf ( "P =%p\n", &p); getheap (p); //the argument has not changed any getheap1 (&p); //gets the address of the heap memory |
C language Memory distribution