Concepts of global variables, local variables, heap, stack, etc. and memory address allocation in C language programs


One, C language memory allocation

One, memory allocation
1, the type of memory allocation:

In C/C++, the memory is divided into 5 areas, which are stack area, heap area, global/static storage area, constant storage area, and code area.

Static memory allocation: allocated at compile time. Including: global, static global, static local three variables.

Dynamic memory allocation: Run-time allocation. Including: stack (stack): local variables. Heap: The variables used in the c language are dynamically allocated in memory. (malloc or calloc, realloc, free function)

2. Memory allocation of variables:

Stack area (stack): refers to the storage area where the variables that are allocated by the compiler when needed and are automatically cleared when not needed. For example, when a function is executed, the formal parameters of the function and the local variables in the function are allocated in the stack area. After the operation is over, the formal parameters and local variables are removed from the stack (automatically released). The stack memory allocation operation is built-in with the processor's instruction set, which is efficient but the allocated memory space is limited.

Heap area (heap): Refers to the storage area that is manually allocated and released by the programmer. If the programmer does not release this memory, the memory will always be occupied until the program is run. The system will automatically recover it. Malloc is used in the C language, and the application is free. And free up space.

Static storage area (static): The storage of global variables and static variables are put together. The initialized global variables and static variables are in the same area. This space is released by the system when the program runs.

Constant storage area (const): Constant strings are stored here. For example, the "ABC" string is stored in the constant area, and the read-only and non-writable ones stored in the constant area. const-modified global variables are also stored in the constant area, and const-modified local variables are still on the stack.

Program code area: store the binary code of the source program.

The original text comes from:
C language: memory allocation


2. Programming in ubuntu system and outputting information for verification

Code source: In C language, the memory addresses of local variables, global variables, static variables, heap, and stack
create a 1.c file and write the code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
void before()
{
    
    
 
}
 
char g_buf[16];
char g_buf2[16];
char g_buf3[16];
char g_buf4[16];
char g_i_buf[]="123";
char g_i_buf2[]="123";
char g_i_buf3[]="123";
 
void after()
{
    
    
 
}
 
int main(int argc, char **argv)
{
    
    
        char l_buf[16];
        char l_buf2[16];
        char l_buf3[16];
        static char s_buf[16];
        static char s_buf2[16];
        static char s_buf3[16];
        char *p_buf;
        char *p_buf2;
        char *p_buf3;
 
        p_buf = (char *)malloc(sizeof(char) * 16);
        p_buf2 = (char *)malloc(sizeof(char) * 16);
        p_buf3 = (char *)malloc(sizeof(char) * 16);
 
        printf("g_buf: 0x%x\n", g_buf);
        printf("g_buf2: 0x%x\n", g_buf2);
        printf("g_buf3: 0x%x\n", g_buf3);
        printf("g_buf4: 0x%x\n", g_buf4);
 
        printf("g_i_buf: 0x%x\n", g_i_buf);
        printf("g_i_buf2: 0x%x\n", g_i_buf2);
        printf("g_i_buf3: 0x%x\n", g_i_buf3);
 
        printf("l_buf: 0x%x\n", l_buf);
        printf("l_buf2: 0x%x\n", l_buf2);
        printf("l_buf3: 0x%x\n", l_buf3);
 
        printf("s_buf: 0x%x\n", s_buf);
        printf("s_buf2: 0x%x\n", s_buf2);
        printf("s_buf3: 0x%x\n", s_buf3);
 
        printf("p_buf: 0x%x\n", p_buf);
        printf("p_buf2: 0x%x\n", p_buf2);
        printf("p_buf3: 0x%x\n", p_buf3);
 
        printf("before: 0x%x\n", before);
        printf("after: 0x%x\n", after);
        printf("main: 0x%x\n", main);
 
        if (argc > 1)
        {
    
    
                strcpy(l_buf, argv[1]);
        }
        return 0;
}

Compile 1.c and run

gcc 1.c -o 1
./1

Insert picture description here
Insert picture description here
According to the figure above, we can see that the memory addresses are all consistent with

For details, please refer to:
C/C++ program memory's various variable storage areas and detailed explanations of each area. In
C language, the memory addresses of local variables, global variables, static variables, heap, and stacks
. The keyword volatile in C language is the root of the question.

Three, stm32 system verification

Using the previous serial communication code, modify main.c as follows:

#include "stm32f10x.h"
#include "bsp_usart.h"

char quanju1[16];
char quanju2[16];
char quanju3[16];
	
int main(void)
{
    
    	
	char bufen1[16];
  char bufen2[16];
  char bufen3[16];

  USART_Config();

  printf("bufen1: 0x%p\n", bufen1);
  printf("bufen2: 0x%p\n", bufen2);
  printf("bufen3: 0x%p\n", bufen3);
	 
  printf("quanju1: 0x%p\n", quanju1);
  printf("quanju2: 0x%p\n", quanju2);
  printf("quanju3: 0x%p\n", quanju3);
  while(1)
	{
    
    	
		
	}	
}

Generate hex file
Insert picture description here
Follow the same steps as the previous serial communication, click the reset button on the stm32 board, the following figure appears

Insert picture description here
Continue to modify the main.c code as follows:

#include "stm32f10x.h"
#include "bsp_usart.h"
#include <stdlib.h>

int main(void)
{
    
    	
  static char m1[16];
  static char m2[16];
  static char m3[16];
  char *n1;
  char *n2;
  char *n3;

 
  USART_Config();

  printf("m1: 0x%p\n", m1);
  printf("m2: 0x%p\n", m2);
  printf("m3: 0x%p\n", m3);
	 
  n1 = (char *)malloc(sizeof(char) * 16);
  n2 = (char *)malloc(sizeof(char) * 16);
  n3 = (char *)malloc(sizeof(char) * 16);
	
  printf("n1: 0x%p\n", n1);
  printf("n2: 0x%p\n", n2);
  printf("n3: 0x%p\n", n3);
  while(1)
	{
    
    	
		
	}	
}

Compile and generate hex file.
Insert picture description here
Serial port debugging
Insert picture description here
knows from the high memory address to the low memory address according to the serial port data. Stack area, heap area, global area (static area), constant area, code area are distributed in sequence, among which the high addresses in the global area are distributed with .bss Section, the low address is distributed with the .data section, as shown in the figure. For
Insert picture description here
details, please refer to:
[IoT] Detailed explanation of STM32 memory allocation
based on STM32 analysis stack, heap, global area, constant area, code area, RAM, ROM
STM32 KEIL stack settings

Guess you like

Origin blog.csdn.net/aiwr_/article/details/110431441
Recommended