Global variables, local variables, heap, and stack in C language programs are based on verification in stm32

Global variables, local variables, heap, and stack in C language programs are based on verification in stm32

Experimental verification goal

1.
The local variables created temporarily in the stack area are stored in the stack area.
When a function is called, its entry parameters are stored in the stack area.
When the function returns, its return value is stored in the stack area.
The local variables defined by const are stored in the stack area.
2. The
heap area is used to store the dynamically distributed memory segments during program operation, which can be increased or decreased.
There can be functions such as malloc to realize dynamic distribution of memory.
The memory distributed by the malloc function must be freed for memory release, otherwise it will cause memory leaks.
3. .bss section
Uninitialized global variables are stored in the .bss section.
Global variables initialized to 0 and static variables initialized to 0 are stored in the .bss section.
The .bss section does not occupy executable file space, and its contents are initialized by the operating system.
4. .data section The
initialized global variables are stored in the .data section.
Static variables are stored in the .data section.
The .data section occupies the executable file space, and its content is initialized by the program.
Global variables defined by const are stored in the .rodata section.

Experimental procedure

The above information is based on stm32 related codes:

#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include <stdlib.h>

int k1 = 1;
int k2;
static int k3 = 2;
static int k4;



 int main(void)
 {
    
     
  static int m1=2, m2;
  int i = 1;
  char *p;
  char str[10] = "hello";
  char *var1 = "123456";
  char *var2 = "abcdef";
  int *p1=malloc(4);
  int *p2=malloc(4); 
 	u16 t;  
	u16 len;	
	u16 times=0;
	free(p1);
  free(p2);
	delay_init();	    	 //ÑÓʱº¯Êý³õʼ»¯	  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶
	uart_init(115200);	 //´®¿Ú³õʼ»¯Îª115200
 	LED_Init();			     //LED¶Ë¿Ú³õʼ»¯
	KEY_Init();          //³õʼ»¯Óë°´¼üÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
	
 	while(1)
	{
    
    	
			for(t=0;t<len;t++)
			{
    
     
				USART_SendData(USART1, USART_RX_BUF[t]);//Ïò´®¿Ú1·¢ËÍÊý¾Ý
				
				while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//µÈ´ý·¢ËͽáÊø
			}
	
			USART_RX_STA=0;
    	times++;
			if(times%500==0)
			{
    
    
				printf("\r\nǶÈëʽ´®¿ÚʵÑé\r\n");
				printf("hcr@2219491180qqcom\r\n\r\n");
	      printf("Õ»Çø-±äÁ¿µØÖ·\r\n");
				printf("                i:%p\r\n", &i);
				printf("                p:%p\r\n", &p);
				printf("              str:%p\r\n", str);
        printf("\n¶ÑÇø-¶¯Ì¬ÉêÇëµØÖ·\r\n");
        printf("                   %p\r\n", p1);
        printf("                   %p\r\n", p2);
        printf("\r\n.bss¶Î\r\n");
        printf("\nÈ«¾ÖÍⲿÎÞ³õÖµ k2£º%p\r\n", &k2);
        printf("¾²Ì¬ÍⲿÎÞ³õÖµ k4£º%p\r\n", &k4);
        printf("¾²Ì¬ÄÚ²¿ÎÞ³õÖµ m2£º%p\r\n", &m2);
        printf("\r\n.data¶Î\r\n");
        printf("\nÈ«¾ÖÍⲿÓгõÖµ k1£º%p\r\n", &k1);
        printf("¾²Ì¬ÍⲿÓгõÖµ k3£º%p\r\n", &k3);
        printf("¾²Ì¬ÄÚ²¿ÓгõÖµ m1£º%p\r\n", &m1);
        printf("\r\n³£Á¿Çø\n");
        printf("ÎÄ×Ö³£Á¿µØÖ·     £º%p\r\n",var1);
        printf("ÎÄ×Ö³£Á¿µØÖ·     £º%p\r\n",var2);
        printf("\r\n´úÂëÇø\n");
        printf("³ÌÐòÇøµØÖ·       £º%p\n",&main);
				printf("\r\n end \r\n\r\n\r\n");
			}
			
			delay_ms(10);   
		}
	}	 

Experimental steps

1. Compile the program in keil5 and burn it in the stm32 core board
Insert picture description here
2. Open the modulation assistant for serial communication to display confirmation verification
Insert picture description here

Guess you like

Origin blog.csdn.net/rude_dragon/article/details/110531377
Recommended