1, Experimental purpose
Write a C program, review the concepts of global variables, local variables, heap and stack, and program and verify them in Ubuntu (x86) system and STM32(Keil) (stm32 sends the serial port printf information to the host computer serial port assistant through serial port). The allocation addresses of heap, stack, global and local variables in C programs under Ubuntu and stm32 are summarized and compared
2, Memory allocation
Distribution mode
Memory allocation method
In C language, objects can allocate memory space statically or dynamically.
Static allocation: the compiler allocates when processing program source code.
Dynamic allocation: the program calls malloc library function to apply for allocation during execution.
Static memory allocation is carried out before program execution, so it is more efficient, while dynamic memory allocation can flexibly deal with unknowns.
The main differences between static and dynamic memory allocation are as follows:
Static objects are named variables that can be manipulated directly; A dynamic object is a variable without a name, which needs to be operated indirectly through a pointer.
The allocation and release of static objects are automatically processed by the compiler; The allocation and release of dynamic objects must be explicitly managed by programmers through malloc() and free functions (new and delete operators in C + +).
Storage area
Call form: free(void*ptr); Function: release a memory space pointed to by ptr. ptr is an arbitrary type of pointer variable, which points to the first address of the released area. The released area should be the area allocated by malloc or calloc, alloca functions.
In C language, the storage area can be divided into global area, constant area, code area, stack and heap; As shown in the figure below (pictures borrowed from others are helpful for understanding):
3, Experimental process
1. Verification under stm32
Based on the previous serial communication code, the following modifications are made
main.c
#include "usart.h" #include <stdio.h> #include <stdlib.h> 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) { uart_init(115200); while(1) { //Define local variables int a=2; static int inits_local_c=2, uninits_local_c; int init_local_d = 1; char *p; char str[10] = "zls"; //Define constant string char *var1 = "1234567890"; char *var2 = "qwertyuiop"; //Dynamic allocation int *p1=malloc(4); int *p2=malloc(4); output(a); //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; } }
2. Verification under Ubuntu
establish. c Documents
gedit test.c
Add the following 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] = "lmy"; //Define constant string char *var1 = "1234567890"; char *var2 = "qwertyuiop"; //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; }
Run code after compilation
3, Summary
Ubuntu's address values in the stack and heap grow from top to bottom
The address value of STM32 in stack area and heap area decreases from top to bottom
This experiment allows me to review the concepts of global variables, local variables, heap and stack, have a deeper understanding of the allocation addresses of heap, stack, global and local variables in C program, and understand its underlying operation mode.
4, References
https://blog.csdn.net/liwei16611/article/details/88545248
https://blog.csdn.net/Monajim/article/details/122065825