Memory distribution and program operation in C language (BSS, data segment, code snippet, stack)

Source: Internet
Author: User

BSS:(BSS segment) usually refers to an area of memory that is used to store uninitialized global variables in the program. BSS is the abbreviation for English block Started by symbol. BSS segments belong to static memory allocations.

Data Segment: Data segment usually refers to an area of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation. Code Snippet:Code Segment/text segment usually refers to an area of memory that is used to store program execution code. The size of this area is determined before the program runs, and the memory area is usually read-only, and some schemas allow the code snippet to be writable, which allows the program to be modified. In a code snippet, it is also possible to include some read-only constant variables, such as String constants. The program segment is a mapping of program code in memory. A program can have more than one copy in memory. heaps (heap): The heap is used to store dynamically allocated memory segments in the process run, which are not fixed and can be dynamically expanded or scaled down. When a process calls a function such as malloc/free to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded)/freed memory is removed from the heap (heap is reduced) Stacks (Stack): Stacks are also called stacks, which store local variables of the program (but do not include static declared variables, static means that variables are stored in the data segment). In addition, the stack is used to pass parameters and return values when a function is called. Due to the advanced first-out features of the stack, the stack is particularly handy for saving/recovering call sites. Storage dynamic memory allocation, need to be manually assigned by the programmer, manual release is a typical C memory space distribution map in Apue

For example:

#include <stdio.h>

int g1=0, g2=0, g3=0;

int max (int i)
{
    int m1=0,m2,m3=0,*p_max;
    static n1_max=0,n2_max,n3_max=0;
     P_max = (int*) malloc (10);
    printf ("Print max program address \ n");
    printf ("In max:0x%08x\n\n", Max);
    printf ("Print max incoming parameter address \ n");
    printf ("In max:0x%08x\n\n", &i);
    printf ("Print static variable address \ n" in Max function);
    printf ("0x%08x\n", &n1_max),//print the memory address of each local variable
    printf ("0x%08x\n", &n2_max);
    printf ("0x%08x\n\n", &n3_max);
    printf ("Print the local variable address \ n" in the Max function);
    printf ("0x%08x\n", &m1),//print the memory address of each local variable
    printf ("0x%08x\n", &m2 );
    printf ("0x%08x\n\n", &m3);
    printf (the "Print Max function in malloc assigned address \ n");
    printf ("0x%08x\n\n", P_max);//print the memory address of each local variable

if (i) return 1;
else return 0;
}

int main (int argc, char **argv)
{
static int s1=0, S2, s3=0;
int v1=0, v2, v3=0;
int *p;
p = (int*) malloc (10);

printf ("Print the memory address of each global variable (initialized) \ n");
printf ("0x%08x\n", &AMP;G1); Print memory addresses for each global variable
printf ("0x%08x\n", &AMP;G2);
printf ("0x%08x\n\n", &g3);
printf ("======================\n");
printf ("Print Program initial program main address \ n");
printf ("main:0x%08x\n\n", main);
printf ("Print main parameter address \ n");
printf ("argv:0x%08x\n\n", argv);
printf ("Print the memory address of each static variable \ n");
printf ("0x%08x\n", &AMP;S1); Print memory addresses for each static variable
printf ("0x%08x\n", &AMP;S2);
printf ("0x%08x\n\n", &AMP;S3);
printf ("Print the memory address of each local variable \ n");
printf ("0x%08x\n", &AMP;V1); Print memory addresses for each local variable
printf ("0x%08x\n", &v2);
printf ("0x%08x\n\n", &v3);
printf ("Print the heap address for malloc allocation \ n");
printf ("malloc:0x%08x\n\n", p);
printf ("======================\n");
Max (v1);
printf ("======================\n");
printf ("Print sub function start address \ n");
printf ("max:0x%08x\n\n", Max);
return 0;
}

Printing results:

You can roughly see the distribution of the entire program in-memory:
It can be seen that the passed parameters, local variables, are distributed at the top of the stack, and grow downward as the number of child functions increases.
The calling address of the function (function run code), global variables, static variables are present at the lower portion of the allocated memory, and the malloc allocated heap exists on top of those memory and grows upward.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In the operating system, a process is a live specimen of the program code that is in the execution period (including system resources, of course), actually executing. So how is the logical address space of the process divided?

Reference:

Figure 1 makes a simple description (under Linux)


On the left is the execution file of the Unix/linux system, and the right is the partitioning of the corresponding process logical address space.



The first is the stack area, where the stack is automatically allocated by the compiler, the value of the parameter that holds the function, the value of the local variable, and so on. It operates in a manner similar to a stack in a data structure. The application of the stack is automatically assigned by the system, such as the application of a local variable int h in the function, while judging whether the requested space is less than the remaining space of the stack, if less, in the stack for it to open up space, to provide memory for the program, otherwise it will report the exception prompt stack overflow.
Next is the heap, where the heap is typically released by the programmer, and if the programmer does not release it, it may be reclaimed by the OS at the end of the program. Note that it is not the same as the heap in the data structure, but the distribution is similar to the linked list. Heap applications are handled by programmers themselves, using malloc functions in C, and the new operator in C + +, but the heap application process is complicated: when the system receives the application, it iterates through the list of idle memory addresses in order to find the first heap node that is larger than the requested space. The node is then removed from the list of idle nodes, and the space of the node is assigned to the program, and it should be noted here that in some cases the initial address of the newly requested memory block records the size of the allocated memory block so that the memory space is freed correctly when the delete is especially delete[].
Next is the global data area (static zone), where the storage of global variables and static variables is placed in a block, initialized global variables and static variables in one area, uninitialized global variables and uninitialized static variables in another adjacent area. In addition to the literal constant area, the constant string is placed here, the program is released after the end of the system.
Finally, the program code area, put the function body of the binary code.

To illustrate:
int a = 0; Global initialization Zone


Char *p1; Global uninitialized Zone


int main ()

{

int b; Stack

Char s[] = "ABC"; Stack

Char *p2; Stack

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

static int c = 0;//global (Static) initialization area

P1 = (char *) malloc (10);

P2 = (char *) malloc (20); Areas that are allocated 10 and 20 bytes are in the heap area.

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

return 0;

}

Memory distribution and program operation in C language (BSS, data segment, code snippet, stack)

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.