Memory classification and so on

Source: Internet
Author: User

The memory needs to be precisely understood during C/C ++ and embedded development. The memory categories frequently to be operated can be as follows: Stack and heap), constant area, static area (global area), and code area.

Stack ZoneThe compiler automatically allocates and releases function parameters and local variable values. operations are similar to stack operations in data structures.

Heap AreaDistribution and release by the programmer. If the programmer does not release the program, the program may be withdrawn by the system after the program is run. It is different from the concept of heap in the data structure. The distribution method is similar to the linked list, you can use malloc and new to apply,

Global zone (static zone)Stores global variables, Global static variables, and local static variables. global variables and static variables are stored in one partition. initialized global variables and static variables are stored in one partition, uninitialized global variables and static variables are in another adjacent area. The program is released by the system.

Constant AreaStores strings, and is released by the system after the program ends.

Code AreaStores the binary code of the function body.

Int QA = 10; // The Global initialization zone, which is stored in the global zone (static zone)
Char * PV; // The Global uninitialized zone, which is stored in the global zone (static zone)
Main ()
{
Int A; // local variable, stored in the stack
Char * pA; // Stack
Char * P = "asdasdasd"; // P is stored in the stack and string is stored in the constant zone.
Static int B = 1; // global (static) initialization Zone
Pa = (char *) malloc (10); // The allocated 10 bytes exist in the heap,
Strcpy (Pa, "3156564654"); // the strings and numbers following them are stored in the constant area,
}

The memory space of a process can be logically divided into three zones: Code zone, dynamic data zone, and static data zone.

The Dynamic Data zone is the "stack", the stack is a linear structure, and the stack is a chain structure. Each thread of a process has its own private "stack". Therefore, although the Code of each thread is the same, the data of local variables does not interfere with each other.


/*************************************** **************************************** ****************************/

Executable programs include BSS segments, data segments, and code segments (also called text segments ).

BSSBlock started by symbol is usually a memory area used to store uninitialized global variables and static variables in the program. Features: read/write, BSS segments are automatically cleared before the program is executed. Therefore, the uninitialized global variables are 0 before the program is executed. BSS segments belong to static memory allocation.
Note that, unlike data segments, BSS stores uninitialized global variables and static variables,Data SegmentIt stores the initialized global and static variables. The data segment belongs to the static memory allocation.


Memory classification and so on

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.