C Language Study Notes-heap and stack-unorganized, C Language Study Notes

Source: Internet
Author: User

C Language Study Notes-heap and stack-unorganized, C Language Study Notes

C Language notes

Stack is an advanced memory structure. All automatic variables and function parameters are automatically released from the stack by the compiler. When an automatic variable exceeds its scope, automatically pops up from the stack. The C language compiler automatically allocates and releases the inbound and outbound stacks. The stack is not very large, generally in K units. Stack Overflow: When the stack space is full, but the variables are still pressed to the stack memory, it is called Stack Overflow. High Speed and High Efficiency heap is the same as stack. heap is also a memory area that can be modified at any time during the running process, but it is not as advanced as stack.
The heap is a large container with a much larger capacity than the stack. in C language, the application and release of the internal space must be completed manually by code. The heap must be released after use. Slow speed and low efficiency.
Heap allocation and release-malloc () and free () (defined in stdlib. h)
Static: stores global and static variables. After the program ends, the system automatically releases it. Code zone (code)-stores the program code of the function and cannot be modified during execution. The address of a stack variable cannot be returned through the return value of the function. That is, the variable address defined in the function cannot be the return value of the function. A function can return a heap address through the return value, but it must be used with free () later. // ************************************ Int * geta () // error. The stack address cannot be returned as the function {int a = 0; return &;
}//************************************* Int * getb () // correct. The requested heap space can be used as the function return value. Use {int * p = malloc (); return p with the free () function;
} // ************************************ Int * getc () // correct. The static variable is in the static zone. The program runs and the address remains valid. You cannot use free () to release {static int a = 0; return & ;} // *********************************** void getheap (int * p) // p is a form parameter defined in the stack. After the function is executed, p is released, and the heap space pointed to by p is not released, the address of the specific heap space pointed to by p is lost. {P = malloc () ;}int main () {int * p = NULL; getheap (p );
......
Free (p );
Return 0;
} // Error //********************************** * void getheap (int ** p) // correct {* p = malloc ();} int main () {int * p = NULL; getheap (p );
......
Free (p );
Return 0;
} // *********************************** Int * getheap (int * p) // correct {p = malloc (); return p;} int main () {int * p = NULL; p = getheap (p );
......
Free (p );
Return 0;
} // *********************************** 2, stack and memory ing each thread has its own stack, when the maximum size of the output stack is fixed and exceeds the limit, the stack overflow variable will exit the function range. The data on the stack will be automatically released and the memory on the stack must be manually released. int main () {int I = 0; scanf ("% d", & I); int array [I]; // error. When defining an array, the array length must be a constant, not a variable.
Int * array = malloc ();] clearly knows how much memory is occupied by data and the amount of data is very small-the stack space is used and the memory is not sure how much memory is needed, a large amount of data-the heap space is used ): controlled by programmers, the use of malloc/free operation stack (stack): pre-set size, automatic allocation and release of heap and stack occupied memory space memory ing: stack: stack top increases from high address to low address storage non-static local variables, function parameters, return address C language function parameter list is from right to left into the stack of the heap allocation and release in check the memory usage of C language programs in Linux: compile a C language program, compile and run the program, run ps-u test -- view the process PID, cd/proc, cd PID, and cat maps -- display the memory usage, cat smaps -- display more detailed memory usage. When the operating system manages the memory, the minimum unit is not the byte, but the memory page. The memory page size is generally 4K32-bit. The system can manage a maximum of 4 GB memory. The calloc () function defines a memory block in the heap space, and initialize it to 0; realloc (NULL, 5); equivalent to malloc (5 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.