How memory is laid out for C programming programs

Source: Internet
Author: User

Focus on the following topics:

The composition of the various paragraphs in the C language program

Features and common errors in the C language Program connection process

How to run C language programs

One: Storage area of C language Program

The C language Code (text file) forms an executable program (binary file) that needs to be compiled-compiled-connected three stages. The compilation process of C language text file generation assembler, the assembly process to the assembler to form a binary machine code, the connection process will be generated by the source file binary machine code files into a file.

The C language program is compiled-connected and will form a unified file, which consists of several parts. While the program is running, it produces several other parts, each of which represents a different storage area:

1. Snippet (code or text)

The code snippet consists of the machine code executed in the program. In the C language, the program statements are compiled to form machine code. During the execution of the program, the CPU's program counter points to each machine code in the code snippet and runs sequentially by the processor.

2. Read-only segment (RO data)

Read-only data segments are data that the program uses that are not changed, and use this data in a way that looks like a table-type operation, because these variables do not need to be changed, so they only need to be placed in read-only memory.

3. The read-write data segment has been initialized (RW data)

Initialized data is declared in the program, and has the initial value of the variable, these variables need to occupy memory space, when the program executes, they need to be in a writable memory area, and have an initial value for the program to read and write.

4. Uninitialized data segment (BSS)

Uninitialized data is declared in the program, but there are no initialized variables that do not need to occupy memory space before the program runs.

5. Heaps (heap)

Heap memory occurs only when the program is running, and is typically assigned and freed by programmers. In the case of an operating system, if the program is not released, the operating system may reclaim memory after the program (such as a process) ends.

6. Stacks (Stack)

Stack memory only appears when the program is running, the variables used inside the function, the parameters of the function, and the return value will use the stack space, and the stack space is automatically allocated and freed by the compiler.

Memory layout of the C language destination file

See an example:

int a = 0; Global initialization zone,. Data segment

static int b=20; Global initialization zone,. Data segment

Char *p1; Global uninitialized zone. BSS segment

const int A = 10; . Rodata Segment

void Main (void)

{

int b; Stack

Char s[] = "ABC"; Stack

Char *p2; Stack

static int c = 0; Global (static) initialization area. Data segment

Char *p3 = "123456"; 123456\0 in the constant area, p3 on the stack.

P1 = (char*) malloc (10);//The allocated 10 and 20-byte regions are in the heap area

P2 = (char*) malloc (20);

strcpy (P1, "123456"); 123456\0 in a constant area, the compiler might optimize it to a place with the "123456" that P3 points to

}

Code snippets, read-only data segments, read and write data segments, uninitialized data segments belong to static regions, and heaps and stacks are dynamic regions. Code snippets, read-only data segments, and read-write data segments will be generated after the link, and uninitialized data segments will be opened when the program is initialized, and the heap and stack will be allocated and disposed of in the run of the program. The C language Program is divided into two states: Image and runtime. In the image that is formed after the compilation-connection, only the code snippet (Text), the read-only data segment (RO data), and the read-write data segment (RW data) are included. Uninitialized data segments (BSS) are generated dynamically before the program runs, and the heap (heap) region and stack area are also dynamically formed when the program runs. In general, in a static image file, each part is called a section, and the parts at run time are called segments (Segment). If not detailed, it can be collectively referred to as segments.

Knowledge Points:

After compiling and connecting, the C language generates code snippets (Text), read-only data segments (RO data), and read-write data segments (RW data). At run time, in addition to the above three regions, the uninitialized data segment (BSS) zone and Heap (heap) region and stack (stack) regions are included.

II: Paragraph of the C language program

1. Snippet (code or text)

The code snippet is generated by each function, and each statement of the function will eventually be compiled Yi and assembled to generate the binary machine code (the machine code for which architecture is specific is determined by the compiler).

2. Read-only segment (RO data)

Read-only data segments are generated by the data used in the program, which is characterized by the fact that it does not need to be changed in the run, so the compiler places the data segment into a read-only section. Read-only global variables in the C language, read-only local variables, constants used in programs, and so on are put into the read-only data area at compile time.

Note: Define the global variable const char a[100]={"ABCDEFG"}; A read-only data area of size 100 bytes will be generated and initialized with "ABCDEFG". If the definition is: const char a[]={"ABCDEFG"}, a 8-byte read-only data segment (and ' + ') is generated based on the length of the string, so a full initialization is generally required in the read-only data segment.

3. Read-write Data segment (RW)

The read-write data segment represents a portion of a data area that can be read or written in the target file, and in some cases they are also referred to as initialized data segments, which are part of the data segment and the code snippet that belongs to the static area of the program, but is writable in the same way as read-only data segments. Generally initialized global variables and local static variables are placed in the read-write data segment, such as: Define the static char b[100]={"ABCDEFG"} in the function, the read-write data area is characterized by the program must be initialized, if only defined, no initial value, will not generate read-write data area, Instead, it is positioned as an uninitialized data area (BSS). If a global variable (a variable defined outside the function) is added to the static modifier, this means that it can only be used within a file, not by another file.

4. Uninitialized data segment (BSS)

Like a read-write data segment, it is also part of a static data area, but the data in that segment is not initialized. So it is only identified in the target file, not really a paragraph in the target file, which will be generated at run time. Uninitialized data segments are generated only during the initialization phase of the run, so its size does not affect the size of the target file.

In the C language program, the use of variables also have the following points to note:

1. Variables defined in the function body are usually on the stack and do not need to be managed in the program, and are handled by the Yi.

2. The memory space allocated by the function allocating memory, such as Malloc,calloc,realloc, is on the heap, and the program must be guaranteed to release with free, otherwise a memory leak will occur.

3. All functions are defined as global variables, and static variables are placed in the global zone either inside or outside the function.

4. Variables defined with const will be placed in the program's read-only data area.

Third: The use of the middle of the program

The following is a simple example to illustrate the corresponding relationship between variables and segments in C language. C Language Program in the global Zone (static area), the actual corresponding to the following paragraphs: RO Data; RW Data; BSS Data.

Generally speaking, a directly defined global variable is in the uninitialized data area, and if the variable has initialization it is in the initialized data area (RW data), plus the const is placed in the read-only data area.

const char ro[] = {"This was read only data"}; Read-only data area

Static char rw_1[] ={"This is global read write data"}; Read-write Data segment initialized

Char bss_1[100]; Uninitialized data segment

const char *ptrconst = "constant data"; String in a read-only data segment

int main ()

{

Short B; On the stack, occupy 2 bytes

Char a[100]; Open 100 bytes on the stack, whose value is its first address

Char s[]= "ABCDEFG"; s on the stack, occupies 4 bytes, "ABCDEFG" itself is placed in a read-only data store, accounting for 8 bytes

Char *p1; P1 on the stack, taking up 4 bytes

Char *p2= "123456"; P2 on the stack, P2 point to the content can not be changed, "123456" in the read-only data area

static char rw_2[]={"This is local read write data"};//locally initialized read-write segment

static Char bss_2[100]; Partially uninitialized data segment

static int c = 0; Global (static) initialization zone

p1= (char *) malloc (* sizeof (char)); Allocating memory areas in the heap area

strcpy (P1, "xxxx"); "XXXX" is placed in a read-only data area, accounting for 5 bytes

Free (p1); Use free to release the memory pointed to by P1

return 0;

}

The read-write data segment contains a global variable of the memory initialization static char rw_1[] and a local static variable static rw_2[]. The difference is that when you make a Yi, it is used inside the function or in the entire file. For rw_1[] Whether or not static modification, it will be placed in the read-write data area, but can be referenced by other files or not. For the latter is not the same, it is a local static variable, placed in the read-write data area, if there is no static modification, its meaning completely changed, it will be open in the stack space of local variables, rather than static variables, here rw_1[],rw_2[] no specific values, Indicates that the static area size is the same as the subsequent string length.

For uninitialized data area bss_1[100] and bss_2[100], the difference is that the former is a global variable and can be used in all files, while the latter is a local variable that is used only inside the function. Uninitialized data segments do not set subsequent initialization values, so the size of the range must be specified using numeric values, and the Yi will set the length of the BSS to be increased by size.

The stack space is primarily used for storage of the following 3 data:

1. Dynamic variables inside the function

2. Parameters of the function

3. return value of function

The stack space is dynamically opened and recycled. In the process of function call, if the function call hierarchy is more, the required stack space is also gradually increased, for the parameters of the transfer and return value, if the use of large structure, the use of the stack space will be relatively large.

How memory is laid out for C programming programs

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.