1, Memory allocation of C program
stack area
It is automatically allocated and released by the compiler to store the parameter values of functions and the values of local variables. Its operation is similar to the stack in the data structure.
heap
Generally, it is allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS at the end of the program. It is different from the heap in the data structure, and the allocation method is similar to the linked list.
Global area (static area)
Global variables and static variables are stored together. Initialized global variables and static variables are in one area, and uninitialized global variables and uninitialized static variables are in another adjacent area. When the program ends, the variable is released by the system.
Text constant area
Store constant strings. When the program ends, the constant string is released by the system.
Program code area
Binary code for storing function body
Storage area diagram:
In an STM32 program code, stack area, heap area, global area (static area), constant area and code area are successively distributed from memory high address to memory low address, in which. bss segment is distributed in the global area and. data segment is distributed in the low address
2, Program and verify in Ubuntu (x86) system and STM32(Keil) respectively
code:
#include <stdio.h> #include <stdlib.h> //Define global variables int init_global_a = 1; int uninit_global_a; static int inits_global_b = 2; static int uninits_global_b; void output(int a) { printf("hello"); printf("%d",a); printf("\n"); } int main( ) { //Define local variables int a=2; static int inits_local_c=2, uninits_local_c; int init_local_d = 1; output(a); char *p; char str[10] = "gt"; //Define constant string char *var1 = "1234567890"; char *var2 = "haohaoshahaizi"; //Dynamic allocation int *p1=malloc(4); int *p2=malloc(4); //release free(p1); free(p2); printf("Stack area-Variable address\n"); printf(" a : %p\n", &a); printf(" init_local_d: %p\n", &init_local_d); printf(" p: %p\n", &p); printf(" str: %p\n", str); printf("\n Heap area-Dynamic application address\n"); printf(" %p\n", p1); printf(" %p\n", p2); printf("\n Global area-Global and static variables\n"); printf("\n.bss paragraph\n"); printf("Global external no initial value uninit_global_a: %p\n", &uninit_global_a); printf("Static external no initial value uninits_global_b: %p\n", &uninits_global_b); printf("Static internal no initial value uninits_local_c: %p\n", &uninits_local_c); printf("\n.data paragraph\n"); printf("Global external initial value init_global_a: %p\n", &init_global_a); printf("Static external initial value inits_global_b: %p\n", &inits_global_b); printf("Static internal initial value inits_local_c: %p\n", &inits_local_c); printf("\n Text constant area\n"); printf("Literal constant address : %p\n",var1); printf("Literal constant address : %p\n",var2); printf("\n Code area\n"); printf("Program area address : %p\n",&main); printf("Function address : %p\n",&output); return 0; }
1. Run on Ubuntu:
Obviously, Ubuntu's address values in the stack and heap grow from top to bottom.
3. Keil running
Link: https://pan.baidu.com/s/1IjFyR5RdYeF_CMmDgTVv9w
Extraction code: 6666
Modify main function
#include "led.h" #include "delay.h" #include "key.h" #include "sys.h" #include "usart.h" #include <stdio.h> #include <stdlib.h> //Define global variables int init_global_a = 1; int uninit_global_a; static int inits_global_b = 2; static int uninits_global_b; void output(int a) { printf("hello"); printf("%d",a); printf("\n"); } int main(void) { u16 t; u16 len; u16 times=0; delay_init(); //Delay function initialization NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set NVIC interrupt packet 2: 2-bit preemption priority and 2-bit response priority uart_init(115200); //The serial port is initialized to 115200 LED_Init(); //LED port initialization KEY_Init(); //Initialize the hardware interface connected with the key while(1) { //Define local variables int a=2; static int inits_local_c=2, uninits_local_c; int init_local_d = 1; output(a); char *p; char str[10] = "yaoyao"; //Define constant string char *var1 = "1234567890"; char *var2 = "abcdefghij"; //Dynamic allocation int *p1=malloc(4); int *p2=malloc(4); //release free(p1); free(p2); printf("Stack area-Variable address\n"); printf(" a: %p\n", &a); printf(" init_local_d: %p\n", &init_local_d); printf(" p: %p\n", &p); printf(" str: %p\n", str); printf("\n Heap area-Dynamic application address\n"); printf(" %p\n", p1); printf(" %p\n", p2); printf("\n Global area-Global and static variables\n"); printf("\n.bss paragraph\n"); printf("Global external no initial value uninit_global_a: %p\n", &uninit_global_a); printf("Static external no initial value uninits_global_b: %p\n", &uninits_global_b); printf("Static internal no initial value uninits_local_c: %p\n", &uninits_local_c); printf("\n.data paragraph\n"); printf("Global external initial value init_global_a: %p\n", &init_global_a); printf("Static external initial value inits_global_b: %p\n", &inits_global_b); printf("Static internal initial value inits_local_c: %p\n", &inits_local_c); printf("\n Text constant area\n"); printf("Literal constant address : %p\n",var1); printf("Literal constant address : %p\n",var2); printf("\n Code area\n"); printf("Program area address : %p\n",&main); printf("Function address : %p\n",&output); return 0; } }
Click on the magic wand
freedom from error
. Serial port burning
Successfully burned.
Open the serial port assistant and press the RESET key to view the results
III. summary
Having a certain understanding of the memory distribution of c program is also equivalent to reviewing the knowledge of global variables, local variables, heap, stack and so on. Know the heap and stack space allocation: the stack extends to the low address and the heap extends to the high address.
IV. reference website
https://blog.csdn.net/qq_43279579/article/details/110308101
https://blog.csdn.net/qq_48273416/article/details/122007407
https://blog.csdn.net/weixin_33796177/article/details/90125379