C Language memory model (Memory organization mode) __c language

Source: Internet
Author: User
We know that the C program developed and compiled, to load memory (main memory or memory) to run (see: Load memory, let the program run), variable name, function name will correspond to an area of memory.

There are many programs running in memory, our program takes up only a part of space, this part of space can be subdivided into the following areas:
Memory Partitions Description
Program code Area Binary code that holds the function body
static data area Also known as the global data area, contains more data types, such as global variables, static variables, general constants, string constants. Where the storage of global variables and static variables is placed in one piece, initialized global variables and static variables in a region, uninitialized global variables and uninitialized static variables in another contiguous area. Constant data (general constants, string constants) is stored in another area.
Note: The memory of the static data area is released by the operating system after the program finishes.
Heap Zone (heap area) The programmer is typically assigned and released, and if the programmer does not release, the program is recycled by the operating system at the end of the run. The malloc (), calloc (), free () and other functions operate this memory, which is also the focus of this chapter.

Note: The heap in this area is not a concept of the heap in the data structure, and the distribution of the heap area is similar to that of the linked list.
Stack region (Stack area) The system is automatically allocated and released, which holds the function's parameter value, the local variable value and so on. The operation is similar to the stack in the data structure.
Command line argument area A value that holds command-line arguments and environment variables, such as those passed through the main () function.


Figure 1: Diagram of the C language memory model
Tip: About local string constants are stored in the global constant area or stack area, different compilers have different implementations, VC will local constants like local variables, storage Yu Yu (⑥ area), TC is stored in the static data area of the constant area (② area).

Note: The default value for uninitialized global variables is 0, whereas uninitialized local variables have a value of garbage (any value). Take a look at the following code:

    #include <stdio.h>
    #include <conio.h>
    int global;
    int main ()
    {
        int local;
        printf ("Global =%d\n", global);
        printf ("local =%d\n", local);
        Getch ();
        return 0;
    }

Run Result:
Global = 0
Local = 1912227604

For a better understanding of the memory model, please look at the following code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int a = 0;  Global initialization Area (④ region)
    Char *p1;  Global uninitialized region (③ area)
    int main ()
    {
        int b;  Stack area
        char s[] = "abc";  Stack area
        char *p2;  Stack area
        char *p3 = "123456";//123456\0 in a constant region (②), P3 on the stack, experiencing a different
        static int c = 0  with char s[]= "ABC"; Global initialization area
        p1 = (char *) malloc (Ten),  //heap Area
        P2 = (char *) malloc;  The heap area
        //123456\0 is placed in a constant area, but the compiler may optimize it with the "123456" that P3 points to a local
        strcpy (P1, "123456");
    }


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.