Teach your layout C programming language program memory

Focus on the following:

  C language program in the memory that make up each segment

  Features of the C language program during connection and common errors

  Run C language program

  A: Storage Area C language program

  Executable form (binary) from the C language code (text file), need to be compiled - Assembler - connecting the three phases. C compilation process to generate the assembler language text file, the assembly process to form the binary machine code assembler, the connection process will separate source files generated binary machine code files are combined into one file.

  Programs written in C language compiled - Once connected, will form a unified file, which consists of several parts. Will produce several other programs running portions, each portion represents a different storage areas:

  1. The code segment (Code or Text)

  Code segments executed by a machine code program in the composition. In the C language, the program statements compiled machine code is formed. During program execution, the program counter to the CPU machine code each code segment, in turn run by the processor.

  2. The read-only data segment (RO data)

  Read-only data segments are not being changed some of the data used by the program, using a manner similar to the data look-up table type operation, since these variables do not change, it is only necessary to be placed in the ROM.

  3. initialized data section to read and write (RW data)

  Initialized data is read statement in the program, and having a variable initial value, these variables take up space of the memory during program execution needs to be located within the memory area are read-write, and with the initial value, for runtime write.

  4. uninitialized data segment (BSS)

  Uninitialized data is declared in the program, but not initialized variable, these variables do not take up space in the memory before the program runs.

  5. heap (heap)

  Heap memory only appears when the program runs, the general allocation and release by the programmer. In the case of an operating system, if the program does not release the operating system may recover memory after the program (such as a process) is completed.

  6. Stack (Stack)

  Stack memory only in running, variables used within a function, the function parameters and return values ​​using stack space, stack space is automatically allocated and freed by the compiler.

  Memory layout of the C language object files

  Look at an example:

  int a = 0; // global initialization area. data segment

  static int b = 20; // global initialization area. data segment

  char * p1; // .bss segment uninitialized global area

  const int A = 10; //.rodata段

  void main(void)

  {

  int b; // Stack

  char s[] = "abc"; //栈

  char * p2; // stack

  static int c = 0; // .data section global (static) zone initialization

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

  p1 = (char *) malloc (10); // allocate come regions 10 and 20 bytes in the heap area

  p2 = (char*) malloc(20);

  strcpy (p1, "123456"); // 123456 \ 0 in the constant region, the compiler might p3 points to it with "123456" optimization into a place

  }

  Code segment, the data segment read-only, read and write data segments, a static segment uninitialized data area, and the region is the dynamic heap and stack. Code segment, the data segment read-only and read-write data generated after the linking segments, uninitialized data section will open at program initialization, the stack and heap allocation and release operation of the program. C language program is divided into two states, and run-time image. Compiling - image formed after connection, it will only contain the code segment (the Text), read-only data segment (RO Data) and write data segment (RW Data). Before running, dynamically generated uninitialized data section (the BSS), also dynamically formed stack (Heap) and a stack area (Stack) in the runtime of the program area. Generally, in a static image file, the various parts called sections (Section), and each portion is called at run time segment (Segment). If the details are not distinguished, collectively referred to as segments.

  Knowledge points:

  After the C language is compiled and linked, the generated code segment (the Text), read-only data segment (RO Data) and write data segment (RW Data). In operation, in addition to the above three regions, further comprising uninitialized data segment (BSS) and the heap area (Heap) and a stack area (Stack) region.

  II: section C language program

  1. The code segments (code or text)

  Code segments generated by each function, a function of each statement will unravel and eventually through the binary machine code to generate the assembler (which life and architecture specific machine code by a compiler determined).

  2. The read-only data segment (RO Data)

  Read-only data segments derived from the data used in the program, the characteristics of the portion of data is not required to change during operation, the compiler will read only the data segments placed in the portion. Read-only global variables in the C language, read-only local variables, constants, etc. used in the program is placed to a read only data area at compile time.

  Note: The definition of global variables const char a [100] = { "ABCDEFG"}; The resulting size is 100 bytes of read-only data area, and using the "ABCDEFG" initialization. If defined as: const char a [] = { "ABCDEFG"}; string length is generated according to the 8 bytes of read-only data segment (also '\ 0'), so that in a read-only data segment, generally We need to do a full initialization.

  3. The read and write data segment (RW Data)

  Read and write data in the target segment represents part of a file can be read can also write data area, in some cases they are also known as initialized data segment, which is part of the data segment and code segments, like the read-only data segment belongs program in static area, but has the characteristics of writability. Typically initialized global variables and local variables are placed in the static read and write data segment, such as: definition of static char b [100] = { "ABCDEFG"} in the function; characteristics must read and write data in the program area is initialized , If only, not the initial value, the write data region is not generated, and will be positioned to uninitialized data area (BSS). If the global variable (defined outside a function of variables) added static modification, which means that can only be used within the file, can not be other files.

  4. uninitialized data segment (BSS)

  Similar to the read and write data segment, it is a static data area, but the data in the segment has not been initialized. So it will only be identified in the target file, without actually called for a target file, the section will produce at runtime. Uninitialized data segment will be generated only during the initialization phase of operation, so it does not affect the size of the target file size.

  In the C language program, use the following variables as well as the need to pay attention:

  1. Variables defined in the body of the function is usually not necessary to perform management on the stack in the program, the processing unit for Yi.

  2. Use the function malloc, calloc, realloc allocates memory and other memory space allocated on the heap, the program must ensure that the use of free release, or a memory leak occurs.

  3. All in vitro function defined global variables, static variables after the addition of a function, whether inside or outside the global area are placed.

  4. const defined variables read-only data in the discharge region of the program.

  Three: use the program in the middle of

  Here a simple example to illustrate the correspondence relationship between variables in the C language and segments. C language program in the global zone (quiescent zone), corresponding to the actual number of segments following: RO Data; RW Data; BSS Data.

  In general, the global variable defined directly in the uninitialized data area, if the variable initialization is in the initialized data region (RW Data), read-only data area const will put together.

  const char ro [] = { "this is read only data"}; // read-only data area

  static char rw_1 [] = { "this is global read write data"}; // read the initialized data section

  char BSS_1 [100]; // uninitialized data section

  const char * ptrconst = "constant data"; // string in the read only data segments

  int main ()

  {

  short b; // on the stack, occupies 2 bytes

  char a [100]; // open 100 bytes on the stack, its value is the first address

  char s [] = "abcdefg"; // s on the stack, 4 bytes, "abcdefg" placing itself in a read only data storage area, 8 bytes

  char * p1; // p1 on the stack, 4 bytes

  char * p2 = "123456"; // p2 on the stack, p2 point to the content can not be changed, "123456" in a read only data area

  static char rw_2 [] = { "this is local read write data"}; // initialized local read and write data segments

  static char BSS_2 [100]; // local uninitialized data section

  static int c = 0; // global (static) zone initialization

  = P1 (char ) the malloc (10 the sizeof (char)); // allocate a memory region in the heap area

  strcpy (p1, "xxxx"); // "XXXX" in the read-only data area, bytes representing 5

  free (p1); // use free the memory pointed to release p1

  return 0;

  }

  Read and write data memory segment contains global variables initialized static char rw_1 [] and local static variables static rw_2 []. The difference is that when unravel, is used inside a function or may be used throughout the document. For rw_1 [] with or without a static modification, which will be placed in the write data region, but can be other files referenced or not. For the latter is not the case, it is the local static variable, read and write data placed in the area, if not static modification, its meaning completely changed, it will be opened in a local variable stack space, rather than a static variable, here rw_1 [], rw_2 [the] no specific value, the same size of the decision area represents the static end of the string length.

  Uninitialized data area for BSS_1 [100] and BSS_2 [100], except that the former is a global variable, it can be used in all documents; the latter is a local variable, the function for internal use only. Uninitialized data segment is not disposed behind the initialization value must be used to specify the size of the area value, for Yi BSS will need to increase the size of the set according to the length.

  Stack space is mainly used for storing data of the following three:

  1. The dynamic variables inside a function

  2. function parameters

  3. The return value of the function

  Dynamic stack space is opened up and recycled. During the call function, if the function call hierarchy are more stack space required to gradually increase, for passing parameters and return values, if you use a larger structure, the use of stack space will be relatively large.

Guess you like

Origin blog.51cto.com/14355585/2413971