C language memory management and Memory Management

Source: Internet
Author: User

C language memory management and Memory Management

The C language defines four memory areas:

1. Code area: it stores the program code and is read-only.

2. Global variables and static variables (static storage area) are allocated when the program is compiled, all variables (such as global variables, static variables, and string constants) exist during the entire running of the program. The variables allocated in this region are released at the end of the program, it has a long life cycle and is mainly used to transmit information between functions.

3. Create a function on the stack. When a function is executed, the storage units of local variables in the function can be created in the stack. The function is automatically released after execution ends. Stack memory allocation computation is built into the processor's Instruction Set with high efficiency. The disadvantage is that the allocated memory capacity is limited. In Linux, 8192 K (8 M, providing ulimit-s viewing) is supported ).

In this storage mode, variable memory is automatically allocated and released for convenient calling. However, the stack capacity is limited. When the corresponding range ends, local variables cannot be used any more.

4. In the heap area, some operation objects can be determined only when the program is running (for example, the employee name in a struct must be manually input by the user and the memory size must be known only after the user input is complete ), in this way, the compiler cannot allocate space in advance during compilation. It can only be allocated when the program is running, also known as dynamic allocation. When the program runs, it uses the malloc function to apply for any amount of memory. The programmer is responsible for releasing the memory (using the free function ). The life cycle of dynamic memory is determined by us. It is flexible to use, but there are many problems.

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.