ARM embedded learning - memory allocation when C program is running

       A computer application program can be divided into two parts in memory: the code segment for storing code and the data segment for storing book data .

The code segment stores the code written by the user; the data segment can be divided into heap and stack;   

                                                  

Under the Linux system, the data segment has added a global initialized data segment (initialized data segment/data segment), including global variables, static variables (including global and local static variables) and constants (such as strings) that are explicitly initialized in the program .

        Stack : The English name is - stack, which is an area allocated and released by the compiler to store function parameters, local variables, etc. The method of calling the function and using the stack ensures that variables with the same name defined inside different functions will not be confused. The management method of the stack is FILO (First In Last Out), which is called "first in last out".

        Heap : English name - heap, generally located between the bss segment and the stack, used to dynamically allocate memory. This area is managed by the programmer, and the programmer uses the allocation and release functions provided by the operating system to use the memory in the heap area.

Memory management functions : malloc() and free() functions

         A C program has two main memory management functions:

              The malloc() function is responsible for allocating memory; the free() function releases the memory allocated by malloc(). The malloc() function returns the first address of the allocated memory.


//test.c
int g_var_a=0;                     //存放在全局已初始化数据区             
char g_var_b;                      //存放在BSS区(未初始化全局变量)
int main()
{
    int var_a;                     //存放在栈区
    char var_str[]="string1";      //存放在栈区
    char *p_str1,*p_str2;          //存放在栈区
    char *p_str3="string2";        //存放在已初始化数据区,prt_str3存放在栈区
    static int var_b=100;          //全局静态数据,存放在已初始化区
 
    p_str1=(char*)malloc(1024);    //从堆区分配1024B内存
    p_str2=(char*)malloc(2048);    //从堆区分配1024B内存
    free(p_str1);
    free(p_str2);
 
    return(0);

Practical memory allocation functions - calloc() and relloc()

          The calloc() function is used to allocate a new memory; the relloc() function is used to change the size of an already allocated memory.

 

Note: The difference between malloc() and relloc():

      The malloc() function cannot initialize the memory space after allocating the memory space, and the calloc() function will initialize the newly allocated memory space after allocating the space. Also, after using the malloc() function to allocate and match the memory, in order to ensure the validity of the data, the allocated memory area needs to be reset to 0;

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_41899773/article/details/100690496