C understand the language of the stack area, stack area, the static area

This link: https: //blog.csdn.net/u014470361/article/details/79297601
What is the code area, constant area, the static area (global area), heap area, stack area?
These zones have been confused, every time I hear these zones have a big head, I feel very confused. Here's combined STM32 kernel to discuss specific analysis of these zones in the end what is going on, each key variables defined in the end is what the area exists.
The following diagram, is a diagram of his summary of memory partitions. The main memory is generally divided into: the code area, constant area, the static area (global area), heap area, stack area these areas.

è¿éåå¾çæè¿ °

Code area: storing the program codes, i.e. machine instructions executed by the CPU, and is read-only.
Constant regions: storing constants (the amount of the program during operation can not be changed, for example: 10, string constant "abcde", the name of the array, etc.)
the static region (global area): static storage area and global variables are together, once the static memory area is allocated, will not be released until after the memory area of the program all over the static
heap: Called by the programmer malloc () function to take the initiative to apply, use the free () function to release memory, If the application heap memory, forgotten after the release of memory, is likely to cause a memory leak
stack area: store local variables within a function, parameter and function return values. After the data in the scope stack area over, the system will automatically manage the memory recovery (memory allocation, reclaiming memory) of the stack area, the developer does not need to manually manage. Stack area is like an inn, there are many rooms, the room is automatically assigned after guests, room guests can change, is a dynamic data changes.
STM32 below in connection with the actual code and the kernel test area and the characteristics of each keyword stored.
STM32L152 chip is used herein, default configuration memory keil V5 environment shown below:

è¿éåå¾çæè¿ °

The default ROM area is allocated start 0x8000000, an area the size is 0x20000, then this area is a read-only area can not be modified, which is stored in the code area and a constant region.
RAM area is assigned a default start 0x20000000, is an area the size of 0x4000, this region is a readable and writable area, is stored in a static area, stack area and heap area. The chip is partitioned as shown below:

è¿éåå¾çæè¿ °
Kernel-based STM32 test code
below for detailed testing to see where each partition for each keyword.

Code area
of memory code area is controlled by the system
storage region of code: compiled program function cpu instruction
address code area: The address of a function, the entry address of the program, the name of the program
name of the function is a pointer, by query function name in which the memory address of the function to store the query area.
Test void (void)
{
    the printf ( "main: P 0x% \ n-", main); // main function of storing print address
}
è¿éåå¾çæè¿ °
can be seen that the main function does 0x08002e81 stored in this address area in the code region.

The constant region
following a code to analyze:
char * P = "abcdef";
this code and char * p; p = "abcdef "; are equivalent.
Two specific meaning above defined method are: the definition of a pointer type char * p, p is stored in string constant "abcdef" in the first address, the string constant "abcdef" is located in the constant region, i.e. p pointing to the constant region. Then the content of the p region which is not directed too modification, can only be read content p * p points, of course, also possible to remove the pointer p point to other areas.
Tests are as follows:

Test void (void)
{   
    char * p = "abcdef";
    the printf ( "0x% p:% S \ n-", p, p); // print the contents of the string address pointer p and p points
    p = "qwedma ";
    the printf (" 0x% p:% S \ n-", p, p); // print the contents of the string address pointer p and p points
    p [0] = '1' ; // try to p points the first character is modified q. 1
    the printf ( "0x% p:% S \ n-", p, p); // print the contents of the string pointer p and the address pointed to by p
}
è¿éåå¾çæè¿ °
can be seen, abcdef string constants in in 0x08002ecc area, a pointer to the p region, and removing the pointer p pointing to the first address string constants qwedma 0x08002ee0. When you try to modify the first character pointed to by p, that qwedma revised to 1wedma, found printed content has not changed, the content of the constant region can not be changed.

Continue to look phrase char str [] = "abcd"; this statement is the definition of a character array, but the array str is located in the stack area, and then constant string "abcd" are copied to the stack area array str inside, then the time of str can be modified.

void Test (void)
{
    char STR [] = "ABCD";
    the printf ( "0x% P:% S \ n-", STR, STR);
    STR [0] = 'E';
    the printf ( "0x% P:% S \ n-", str, str);
}
è¿éåå¾çæè¿ °
 can see the stack area pointing str address: 0x200007c0, and directed content may be modified, the first character is a modified as e.

Static region (global area)
static int A;
int C;
void Test (void)
{
    static int B =. 1;
    B ++;
    the printf ( "B: 0x% P:% D \ n-", & B, B);
}
int main ()
{
    the printf ( "A: 0x% P:% D \ n-", & A, A);
    the printf ( "C: 0x% P:% D \ n-", & C, C);
    for (uint8_t I = 0; I <. 5; I ++)
    {
        Test ();
    }
    return 0;
}
A is a static global variable, b static local variables, c are global variables, which are stored in the static region; a and c are not initialized, are printed 0, indicating their compiler automatically initialized to 0; b is initialized in a for loop five times, but the actual effect is initialized only once b, b are behind every call to the last value, and b address has been unchanged the compiler will for the first time initialization b allocate memory, behind four times initialization is invalid.

Heap
heap area is calling malloc function to allocate memory space, this space after use need to call free () function to release.
void * mallc (size_t); input function is assigned byte size, void * return type is a pointer that points to the first address of the allocated space, void * pointer type can switch to another type of pointer.

Test void (void)
{   
    int * P1 = the malloc (4); // 4-byte space applications
    * p1 = 123; // the space assignment 123
    the printf ( "0x% P: D% \ n-", P1, * P1);
    the printf ( "0x% P \ n-", & P1);
    Free (P1);
    the printf ( "0x% P:% D \ n-", P1, * P1);
    P1 = NULL;
    the printf ( "0x% P \ n-", P1);
}

int * p1 = malloc (4) ; statement is filed 4 bytes of space, the first address space to the pointer p1, we can see that the first address is 0x200003e8, there is a heap;
printf ( "0x% the p-\ n ", & p1); pointer p1 itself requires storage, p1 itself is located in 0x200009d0 stack region;
free (p1); deallocation function free (open memory first address), the memory is marked as available and the inside content cleared to 0, but the pointer still points p1 this space. Safer approach is p1 = NULL; the p1 pointer release, avoid dangling pointers.

void Test (void)
{   
    int * P1 = the malloc (. 4);
    int * P2 = the malloc (. 4);
    the printf ( "P1: 0x% P \ n-", P1);
    the printf ( "P2: 0x% P \ n-" , P2);
}
P2 is greater than the address of the address p1 to verify heap area are grown up, the application address space will successively increase.

Stack Area
stack area allocated by the compiler automatically release, storage function parameters, local variables, and return values, real-time distribution and release program is running, the stack area managed by the operating system automatically, without manual management. After the stack area is a principle advanced, advanced to the time that is stuck in the innermost room, went in at the door, released the first out the door.

void test(void)
{   
    int a;
    int b=0;    
    printf("a:0x%p:%d\n",&a,a);
    printf("b:0x%p:%d\n",&b,b);
}

Can see the local variables defined in the address b is smaller than a, i.e., the stack area is grown downward;
a variable is not initialized, print out a garbage value, compilers do not local variables initialized to zero. However, note: If you are running in the debug mode, the runtime mechanism put your stack space all initialized to zero, which is to explain why the initial value of the local variable uninitialized usually see when debug is 0.
Use when local variables are initialized first try to avoid the confusion caused by garbage value.

The size of the end of the test
data stored in the memory, a large segment pattern and a little-endian mode.
Little endian (little-endian): low byte address is present on the low, high byte address is present on high;
big endian mode (big-endian): low byte address is present on the high, low high byte address is present, just the opposite and little-endian mode.
Another: network byte order: TCP / IP protocol layers is defined as a sequence of bytes Big-Endian, so endian TCP / IP protocol used in network byte order is usually called

static uint32_t m = 0x87654321;
char * P = (char *) & m;
void Test (void)
{   
    the printf ( "P: 0x% P:% X \ n-", P, * P);
    the printf ( "P +. 1: 0x% P:% X \ n-", P +. 1, * (P +. 1));
    the printf (" P + 2: 0x% P:% X \ n-", P + 2, * (P + 2)) ;
    the printf ( "+ P. 3: P 0x%:% X \ n-", P +. 3, * (P +. 3));
}
low byte 21 on the low address, high address in the high byte 87, and match little-endian mode.

keil code and the amount of data bytes occupied query
keil software check the batch file generation after keil compiler, you can view the code and data definitions occupied bytes in size.

è¿éåå¾çæè¿ °
As FIG, Code is occupied by the program code byte, i.e., code area;
the RO-Data representative of read-only data, program data, and string constants as defined herein, are situated, i.e., the constant region;
RW-Data is representative of read and write data initialization, the program defined and initialized global and static variables are located here, part of the static region (global area);
ZI-data representative of uninitialized data read and write, the program defines the global variable is not initialized but and static variables are located here, a static area (global area) the other part. ZI English is zero initial, program variables is used in the system and is initialized variable number of bytes 0, keil compiler default is to initialize the variables you do not have assigned a 0, these variables at runtime are saved in RAM.
Total number of bytes occupied by a program written in the ROM such Total ROM Size (Code + RO Data + RW Data), the program that is downloaded to the Flash ROM in size.

Why should exist in Rom RW, because all data in RAM after power is lost, every time data in RAM power is being re-assigned, each of these fixed value is stored in the Rom, why not include ZI segment of it, because the ZI data is 0, no need to include, as long as the program runs until the area where all ZI data can be cleared, contained inside but a waste of storage space.

Indeed, ROM instructions should be at least a function:
1. RW move the RAM from the ROM, RW as a variable, the variable is not stored in ROM.
2. The RAM area where all cleared ZI, ZI since the region is not in the Image, so it is necessary to give the corresponding program RAM area is cleared in accordance with the address and size of ZI given by the compiler. ZI is also variable, empathy: variables can not be stored in ROM.
In the initial stages of running, RO instructions in the completion of the C program to function properly access variable after these two tasks. Otherwise, the code can run without variables.
----------------
Disclaimer: This article is CSDN bloggers' night wind - "the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/u014470361/article/details/79297601

Released three original articles · won praise 3 · Views 490

Guess you like

Origin blog.csdn.net/weixin_41005658/article/details/102519349