current position:Home>Memory allocation of C program based on Ubuntu and stm32

Memory allocation of C program based on Ubuntu and stm32

2022-01-27 03:01:25 ~~92

( One )C Program memory allocation

1、 The stack area (stack)
Release is automatically allocated by the compiler , Stores the parameter values of the function , The value of a local variable, etc . It operates like a stack in a data structure .
2、 Heap area (heap)
Release is usually assigned by the programmer , If programmers don't release , At the end of the program, the OS Recycling . It's different from the heap in the data structure , The distribution is similar to a linked list .
3、 Global area ( Static zone )(static)
Global and static variables are stored in one block , Initialized global and static variables in one area , Uninitialized global variables 、 Uninitialized static variables are in another adjacent area . When the program is finished , Variables are released by the system .
4、 Text constant area
Store constant string . When the program is finished , Constant strings are released by the system .
5、 Program code area
Store the binary code of the function body .

( Two )Ubuntu and STM32 Next pair C Program output information for verification

Open... In the virtual machine Ubuntu, Then customize the path , Enter the following command in the terminal :
gedit test.c
This command creates a new test.c file

#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( )
{   
	// Defining 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] = "lyy";
    // 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(" The 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(" There is no initial value outside the whole world  uninit_global_a:%p\n", &uninit_global_a);
    printf(" The static external has no initial value  uninits_global_b:%p\n", &uninits_global_b);
    printf(" There is no initial value inside the static state  uninits_local_c:%p\n", &uninits_local_c);
    printf("\n.data paragraph \n");
    printf(" There is an initial value outside the whole world  init_global_a:%p\n", &init_global_a);
    printf(" Static external has initial value  inits_global_b:%p\n", &inits_global_b);
    printf(" Static interior has 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 section \n");
    printf(" Program area address        :%p\n",&main);
    printf(" Function address          :%p\n",&output);
    return 0;
}



Then use the command :
gcc test.c Compile

Input ls You can see a green a.out Executable file .

 Insert picture description here
Input ./a.out Order to execute
 Insert picture description here
As shown in the figure, you can see the variable address of the stack area and the variable address of the heap area .
You can see that the address of the stack area and the address of the heap area are gradually getting larger .

( 3、 ... and )stm32 Verification on development board

Here I use the previous one keil Project code
Reference resources Last one .
Modify the code
ustart.c:

#include "usart.h"
	  	 
// send UASRT Serial port available printf Function send 
// stay usart.h It can be replaced in the document printf Serial port number of the function 	  
#if 1
#pragma import(__use_no_semihosting)             
// Support functions required by the standard library                  
struct __FILE {
	int handle; 
}; 
FILE __stdout;       
// Definition _sys_exit() To avoid using semi host mode     
void _sys_exit(int x){ 
	x = x; 
} 
// redefinition fputc function  
int fputc(int ch, FILE *f){      
	while((USART_n->SR&0X40)==0);// Cycle to send , Until it's sent    
    USART_n->DR = (u8) ch;      
	return ch;
}
#endif 


void _ttywrch(int ch)
{
ch = ch;
}

/*
USART1 Serial port related procedures 
*/
 
#if EN_USART1   //USART1 Use and shield selection 
u8 USART1_RX_BUF[USART1_REC_LEN];     // Receive buffer , Maximum USART_REC_LEN Bytes .
// Reception status 
//bit15,	 Receive completion flag 
//bit14,	 Received 0x0d
//bit13~0,	 Number of valid bytes received 
u16 USART1_RX_STA=0;       // Receive status flag 	  

/*
USART1 A dedicated printf function 
 When turned on at the same time 2 When there are more than serial ports ,printf Function can only be used for one of them , Other serial ports should be created independently printf function 
 Calling method :USART1_printf("123"); // towards USART2 Send characters 123
*/
//void USART1_printf (char *fmt, ...){ 
//	char buffer[USART1_REC_LEN+1];  //  Data length 
//	u8 i = 0;	
//	va_list arg_ptr;
//	va_start(arg_ptr, fmt);  
//	vsnprintf(buffer, USART1_REC_LEN+1, fmt, arg_ptr);
//	while ((i < USART1_REC_LEN) && (i < strlen(buffer))){
//        USART_SendData(USART1, (u8) buffer[i++]);
//        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); 
//	}
//	va_end(arg_ptr);
//}

void USART1_Init(u32 bound){ // A serial port 1 Initialize and start 
    //GPIO Port settings 
    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;	 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	// Can make USART1,GPIOA The clock 
     //USART1_TX   PA.9
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	// Multiplexing push pull output 
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
    //USART1_RX	  PA.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// Floating input 
    GPIO_Init(GPIOA, &GPIO_InitStructure); 
   //Usart1 NVIC  To configure 
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;// preemption 3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;		// Sub priority 3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ Channel enable 
	NVIC_Init(&NVIC_InitStructure);	// Initialize... According to the specified parameters VIC register  
   //USART  Initialize settings 
	USART_InitStructure.USART_BaudRate = bound;// Generally set as 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;// The word is 8 Bit data format 
	USART_InitStructure.USART_StopBits = USART_StopBits_1;// A stop bit 
	USART_InitStructure.USART_Parity = USART_Parity_No;// No parity bit 
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// No hardware data flow control 
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	// Transceiver mode 
    USART_Init(USART1, &USART_InitStructure); // Initialize serial port 
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// Turn on ENABLE/ close DISABLE Interrupt interrupt when data is received   Read register DR Zero clearing , It can also be reset manually by software 
    USART_Cmd(USART1, ENABLE);                    // Enable serial port  
}

void USART1_IRQHandler(void){ // A serial port 1 Interrupt service routine ( Fixed function name cannot be modified )	
	u8 Res;
	// The following is the string received USART1_RX_BUF[] The program ,(USART1_RX_STA&0x3FFF) It's the length of the data ( Not including return )
	// When (USART1_RX_STA&0xC000) When true, it indicates that the data reception is completed , Press enter in the HyperTerminal .
	// Write judgment in the main function if(USART1_RX_STA&0xC000), Then read USART1_RX_BUF[] Array , Read 0x0d 0x0a That is the end .
	// Note that after the main function processes the serial port data , To put USART1_RX_STA clear 0
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){  // Receive interrupt ( The data received must be 0x0d 0x0a ending )		
		Res =USART_ReceiveData(USART1);//(USART1->DR);	// Read received data 
		printf("%c",Res); // Put the data received in  a Symbolic variables   Send back to the computer 		
		if((USART1_RX_STA&0x8000)==0){// Reception is not complete 			
			if(USART1_RX_STA&0x4000){// received 0x0d				
				if(Res!=0x0a)USART1_RX_STA=0;// Receive error , restart 
				else USART1_RX_STA|=0x8000;	// The reception is complete  
			}else{ // I haven't received 0X0D					
				if(Res==0x0d)USART1_RX_STA|=0x4000;
				else{
					USART1_RX_BUF[USART1_RX_STA&0X3FFF]=Res ; // Put the received data into the array 
					USART1_RX_STA++;	// Data length count plus 1
					if(USART1_RX_STA>(USART1_REC_LEN-1))USART1_RX_STA=0;// Receiving data error , Start receiving again 	  
				}		 
			}
		}   		 
	} 
} 
#endif	

/*
USART2 Serial port related procedures 
*/
#if EN_USART2   //USART2 Use and shield selection 
u8 USART2_RX_BUF[USART2_REC_LEN];     // Receive buffer , Maximum USART_REC_LEN Bytes .
// Reception status 
//bit15,	 Receive completion flag 
//bit14,	 Received 0x0d
//bit13~0,	 Number of valid bytes received 
u16 USART2_RX_STA=0;       // Receive status flag 	  

/*
USART2 A dedicated printf function 
 When turned on at the same time 2 When there are more than serial ports ,printf Function can only be used for one of them , Other serial ports should be created independently printf function 
 Calling method :USART2_printf("123"); // towards USART2 Send characters 123
*/
void USART2_printf (char *fmt, ...){ 
	char buffer[USART2_REC_LEN+1];  //  Data length 
	u8 i = 0;	
	va_list arg_ptr;
	va_start(arg_ptr, fmt);  
	vsnprintf(buffer, USART2_REC_LEN+1, fmt, arg_ptr);
	while ((i < USART2_REC_LEN) && (i < strlen(buffer))){
        USART_SendData(USART2, (u8) buffer[i++]);
        while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); 
	}
	va_end(arg_ptr);
}


void USART2_Init(u32 bound){ // A serial port 1 Initialize and start 
    //GPIO Port settings 
    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
		 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); // Can make UART2 Where GPIOA The clock of 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); // Enabling serial RCC The clock 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // Set up USART2 Of RX Interface is PA3
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// Floating input 
    GPIO_Init(GPIOA, &GPIO_InitStructure); 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // Set up USART2 Of TX Interface is PA2
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	// Multiplexing push pull output 
    GPIO_Init(GPIOA, &GPIO_InitStructure);  

   //USART2  Initialize settings 
	USART_InitStructure.USART_BaudRate = bound;// Generally set as 9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;// The word is 8 Bit data format 
	USART_InitStructure.USART_StopBits = USART_StopBits_1;// A stop bit 
	USART_InitStructure.USART_Parity = USART_Parity_No;// No parity bit 
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// No hardware data flow control 
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	// Transceiver mode 
    USART_Init(USART2, &USART_InitStructure); // Initialize serial port 
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);// Turn on ENABLE/ close DISABLE interrupt 
    USART_Cmd(USART2, ENABLE);                    // Enable serial port  
   //Usart2 NVIC  To configure 
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;// preemption 3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		// priority 3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ Channel enable 
	NVIC_Init(&NVIC_InitStructure);	// Initialize... According to the specified parameters VIC register  
}

void USART2_IRQHandler(void){ // A serial port 2 Interrupt service routine ( Fixed function name cannot be modified )	
	u8 Res;
	// The following is the string received USART2_RX_BUF[] The program ,(USART2_RX_STA&0x3FFF) It's the length of the data ( Not including return )
	// When (USART2_RX_STA&0xC000) When true, it indicates that the data reception is completed , Press enter in the HyperTerminal .
	// Write judgment in the main function if(USART2_RX_STA&0xC000), Then read USART2_RX_BUF[] Array , Read 0x0d 0x0a That is the end .
	// Note that after the main function processes the serial port data , To put USART2_RX_STA clear 0
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){  // Receive interrupt ( The data received must be 0x0d 0x0a ending )		
		Res =USART_ReceiveData(USART2);//(USART1->DR);	// Read received data 
		printf("%c",Res); // Put the data received in  a Symbolic variables   Send back to the computer 		
		if((USART2_RX_STA&0x8000)==0){// Reception is not complete 			
			if(USART2_RX_STA&0x4000){// received 0x0d				
				if(Res!=0x0a)USART2_RX_STA=0;// Receive error , restart 
				else USART2_RX_STA|=0x8000;	// The reception is complete  
			}else{ // I haven't received 0X0D					
				if(Res==0x0d)USART2_RX_STA|=0x4000;
				else{
					USART2_RX_BUF[USART2_RX_STA&0X3FFF]=Res ; // Put the received data into the array 
					USART2_RX_STA++;	// Data length count plus 1
					if(USART2_RX_STA>(USART2_REC_LEN-1))USART2_RX_STA=0;// Receiving data error , Start receiving again 	  
				}		 
			}
		}   		 
	} 
} 
#endif	


#if EN_USART3   // If enabled, receive 
u8 USART3_RX_BUF[USART3_REC_LEN];     // Receive buffer , Maximum USART_REC_LEN Bytes .
// Reception status 
//bit15,	 Receive completion flag 
//bit14,	 Received 0x0d
//bit13~0,	 Number of valid bytes received 
u16 USART3_RX_STA=0;       // Receive status flag 	  

/*
USART3 A dedicated printf function 
 When turned on at the same time 2 When there are more than serial ports ,printf Function can only be used for one of them , Other serial ports should be created independently printf function 
 Calling method :USART3_printf("123"); // towards USART3 Send characters 123
*/
void USART3_printf (char *fmt, ...){ 
	char buffer[USART3_REC_LEN+1];  //  Data length 
	u8 i = 0;	
	va_list arg_ptr;
	va_start(arg_ptr, fmt);  
	vsnprintf(buffer, USART3_REC_LEN+1, fmt, arg_ptr);
	while ((i < USART3_REC_LEN) && (i < strlen(buffer))){
        USART_SendData(USART3, (u8) buffer[i++]);
        while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); 
	}
	va_end(arg_ptr);
}

void USART3_Init(u32 BaudRate){ //USART3 Initialize and start 
   GPIO_InitTypeDef GPIO_InitStructure;
   USART_InitTypeDef USART_InitStructure;
   NVIC_InitTypeDef NVIC_InitStructure; 

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); // Can make UART3 Where GPIOB The clock of 
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); // Enabling serial RCC The clock 

   // Serial port uses GPIO Mouth configuration 
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;// Set up USART3 Of RX Interface is PB11
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// Interface mode   Floating input 
   GPIO_Init(GPIOB, &GPIO_InitStructure);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;// Set up USART3 Of TX Interface is PB10
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;// Output speed 50MHz
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;// Interface mode   Multiplexing push pull output 
   GPIO_Init(GPIOB, &GPIO_InitStructure);

   // Configure serial port 
   USART_InitStructure.USART_BaudRate = BaudRate;
   USART_InitStructure.USART_WordLength = USART_WordLength_8b;// The word is 8 Bit data format 
   USART_InitStructure.USART_StopBits = USART_StopBits_1;// A stop bit 
   USART_InitStructure.USART_Parity = USART_Parity_No;// No parity bit 
   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;// No hardware data flow control 
   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	// Transceiver mode 

   USART_Init(USART3, &USART_InitStructure);// Configure serial port 3
   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);// Enable serial port to receive interrupt   
   //USART_ITConfig(USART3, USART_IT_TXE, ENABLE);// The serial port sending interrupt is enabled when sending data 
   USART_Cmd(USART3, ENABLE);// Enable serial port 3

   // Serial port interrupt configuration 
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;// allow USART3 interrupt 
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;// Interrupt level 
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
}

// A serial port 3 Interrupt service routine ( Fixed function name cannot be modified )
// Calling method :if(USART3_RX_STA&0xC000){  Add data processing program  }// The sign is 0xC000 Indicates that the received data string is complete .
void USART3_IRQHandler(void){ 	
	u8 Res;
	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){  // Receive interrupt 
		Res =USART_ReceiveData(USART3);// Read received data 
		if(Res=='S'){// Determine whether the data is STOP( Omit reading S)			
			USART3_RX_STA=1;// If it is STOP Then the flag bit is 1	  
		}else if(Res=='K'){// Determine whether the data is OK( Omit reading K)			
			USART3_RX_STA=2;// If it is OK Then the flag bit is 2	  
		}   		 
	}
} 
#endif	




/*********************************************************************************************
 *  Studio Du Yang  www.DoYoung.net
 *  Carambola Electronics  www.DoYoung.net/YT 
*********************************************************************************************/

/*
a The role of symbols :

%d  Decimal signed integer 
%u  Decimal unsigned integer 
%f  Floating point numbers 
%s  character string 
%c  Single character 
%p  Pointer value 
%e  Floating point numbers in exponential form 
%x, %X  An unsigned integer in hexadecimal form 
%o  An unsigned integer in octal 
%g  Automatically select the appropriate representation 
%p  Output address character 

*/



usart.h:

#ifndef __USART_H
#define __USART_H
//#include <stdarg.h>
//#include <stdlib.h>
#include <string.h>
#include "stdio.h"	
#include "sys.h" 


#define USART_n		USART1  // Define the use of printf Serial port of function , Other serial ports should be used USART_printf Special functions send 

#define USART1_REC_LEN  			200  	// Definition USART1 Maximum number of bytes received 
#define USART2_REC_LEN  			200  	// Definition USART2 Maximum number of bytes received 
#define USART3_REC_LEN  			200  	// Definition USART3 Maximum number of bytes received 

// When a serial port is not used, it should be prohibited , To reduce the amount of compilation 
#define EN_USART1 			1		// Can make (1)/ prohibit (0) A serial port 1
#define EN_USART2 			0		// Can make (1)/ prohibit (0) A serial port 2
#define EN_USART3 			0		// Can make (1)/ prohibit (0) A serial port 3
	  	
extern u8  USART1_RX_BUF[USART1_REC_LEN]; // Receive buffer , Maximum USART_REC_LEN Bytes . The last byte is a newline character  
extern u8  USART2_RX_BUF[USART2_REC_LEN]; // Receive buffer , Maximum USART_REC_LEN Bytes . The last byte is a newline character 
extern u8  USART3_RX_BUF[USART3_REC_LEN]; // Receive buffer , Maximum USART_REC_LEN Bytes . The last byte is a newline character 
 
extern u16 USART1_RX_STA;         		// Receive status flag 	
extern u16 USART2_RX_STA;         		// Receive status flag 	
extern u16 USART3_RX_STA;         		// Receive status flag 	

// Function declaration 
void USART1_Init(u32 bound);// A serial port 1 Initialize and start 
void USART2_Init(u32 bound);// A serial port 2 Initialize and start 
void USART3_Init(u32 bound);// A serial port 3 Initialize and start 
void USART1_printf(char* fmt,...); // A serial port 1 For the exclusive use of printf function 
void USART2_printf(char* fmt,...); // A serial port 2 For the exclusive use of printf function 
void USART3_printf(char* fmt,...); // A serial port 3 For the exclusive use of printf function 

#endif

main.c:

#include "stm32f10x.h" //STM32 The header file 
#include "sys.h"
#include "delay.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){// The main program 
	u8 a=7,b=8;
	// Initializer 
	RCC_Configuration(); // The clock is set 

	USART1_Init(115200); // Serial initialization ( The parameter is baud rate )

	// Main circulation 
	while(1){	
		int a=2;
	static int inits_local_c=2, uninits_local_c;
    int init_local_d = 1;
    output(a);
    char *p;
    char str[10] = "cleveryoga";
    // 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(" The stack area - Variable address \n\r");
    printf("                a:%p\n\r", &a);
    printf("                init_local_d:%p\n\r", &init_local_d);
    printf("                p:%p\n\r", &p);
    printf("              str:%p\n\r", str);
    printf("\n Heap area - Dynamic application address \n\r");
    printf("                   %p\n\r", p1);
    printf("                   %p\n\r", p2);
    printf("\n Global area - Global and static variables \n\r");
    printf("\n.bss paragraph \n");
    printf(" There is no initial value outside the whole world  uninit_global_a:%p\n\r", &uninit_global_a);
    printf(" The static external has no initial value  uninits_global_b:%p\n\r", &uninits_global_b);
    printf(" There is no initial value inside the static state  uninits_local_c:%p\n\r", &uninits_local_c);
    printf("\n.data paragraph \n\r");
    printf(" There is an initial value outside the whole world  init_global_a:%p\n\r", &init_global_a);
    printf(" Static external has initial value  inits_global_b:%p\n\r", &inits_global_b);
    printf(" Static interior has initial value  inits_local_c:%p\n\r", &inits_local_c);
    printf("\n Text constant area \n\r");
    printf(" Literal constant address      :%p\n\r",var1);
    printf(" Literal constant address      :%p\n\r",var2);
    printf("\n Code section \n\r");
    printf(" Program area address        :%p\n\r",&main);
    printf(" Function address          :%p\n\r",&output);


        delay_ms(1000); // Time delay 
		
	}
}

Compile the generated .hex
burn
 Insert picture description here
When the serial port assistant is opened, the following results are obtained
 Insert picture description here
The above figure shows that the stack area variable address and heap area variable address are gradually decreasing , And Ubuntu The result is the opposite .
This is because STM32 Different storage locations in .

( Four )stm32 Where data is stored

see stm32 Distribution of addresses
 Insert picture description here
As you can see in the picture above IROM The starting position of is 0x8000000, The size is 010000, This section is used to store the code area and the text constant area .
IRAM The starting position of is 0x20000000, Size is 0x5000, This area is used to store stacks 、 Pile up 、 Global area (.bss paragraph 、.data paragraph )

( 5、 ... and ) Sum up the experience

Yes C Program memory allocation has a further understanding of , Know one C What parts of program memory should include . among , It's mainly program segments 、 Data segment 、 Three parts of the stack . Under different systems , The change of address value in the region is different .

( 6、 ... and ) Reference link

https://blog.csdn.net/qq_43279579/article/details/110308101
https://blog.csdn.net/liwei16611/article/details/88545248

copyright notice
author[~~92],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270301227306.html

Random recommended