C program memory management

Source: Internet
Author: User

C program memory management

C program memory management

If you are familiar with the Java language, you must know that memory management in Java is completed by virtual machines. This is not the case in C/C ++. Programmers need to allocate and recycle memory space on their own. This article records the storage structure of C Programs in the memory, the common storage types of C variables and functions, and the distribution and recovery of memory. The following C programs use the GCC 4.4.7 compiler version.

Starting with a C program

File structure

I am not familiar with the following Hello. c program.

#include
 
  int main(void){        printf("Hello World\n");        return 0;}
 

Next, compile it with gcc, run the executable file, and view the storage structure of the executable file.


It can be seen that when the executable file Hello is stored (when the memory is not transferred), it is divided into three parts: the code area (text), the data area (data) and the uninitialized data area (bss. In the other three fields, dec indicates the decimal sum, hex indicates the hexadecimal sum, and filename indicates the file name. The specific description of each section is as follows:

(1)Code segment (text segment) <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/release/KOpoaO0 + sLrx/release + release/e21M/zo6i78rbUz/O1xLXY1rfS/release + 1rK/yv2 + release/dPDuMPK/release + LrNyv2 + release + release/K/b7dx/gvvrlmr9vt3h + release + 1xMirvtax5MG/release/XKvLuvtcS + release/release + 1 rHkwb + 6zc60s/XKvLuvtcS + response + NPytcTK/response + response/response + jqL7Wsr + x5MG/response + a7 + response + yc/response + NPyo6zU2rrzw + response + CjxwPjxpbWcgc3JjPQ =" http://www.2cto.com/uploadfile/Collfiles/20141002/20141002091002229.png "Alt =" \ ">

We are at Hello. c adds a code to define a constant I. Through analysis and comparison, we can find that the text area size of the code segment has increased by 4 bytes (one int type occupies 4 bytes ), other regions remain unchanged. We can see that constants are allocated in the code segment.

On the basis of the above, add a sentence, define a global variable a, and assign it a value of 2 to observe the changes in each region.

It is found through comparison that only the size of the data segment is increased by four bytes, and it also proves that the initialized global variables are allocated in the Data zone. The same is true for static variables.

On the basis of the above, we define a global variable B, but this one should not be assigned a value, observe the changes in various regions


It can be found that this time only four bytes are added to the bss region, and the uninitialized global variables are allocated to the bss region. The same is true for uninitialized static variables.

Process Structure

A program is executed as one or more processes. In fact, the data structure of the Process kernel is similar to the storage structure of the above files, mainly because the heap memory and stack memory areas are more. Shows the main layout.


Each part is described as follows:

(1) text segment): The code segment of the preceding executable file is loaded, and its location in the memory is completed by the loader.

(2) Global initialization Data zone/static Data zone (Data Segment): Load the data segment of the preceding executable file, which is located behind the executable code segment and may be unconnected. At the beginning of the program running, I applied for a space for the data segment and released the space when the program exited. Its life cycle is the entire program running period.

(3) Data zone not initialized (BSS ):The load is the BSS segment of the preceding executable file, which is located after the data segment and can be separated. Its lifecycle is the same as that of the Data Segment.

(4) Stack): The Compiler automatically allocates the release to store the parameter values, return values, and local variables of the function. The stack is dynamically allocated and released when the program is running. The stack zone is placed after the BSS and is scaled up to a limited number.

(5) Heap): Used for dynamic memory allocation. Located behind the stack area, it is limited downward expansion. Generally, programmers are allocated and released. If the program is not released, the OS is responsible for the collection at the end of the program.


(To be continued)






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.