In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to solve the memory allocation problem of Ubuntu and stm32 in C program, which has a certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.
1. Introduction to the concept of memory partitioning 1.1. Memory footprint of the CumberCraft + compiler.
1. Stack
The compiler automatically allocates and releases, storing the parameter values of the function, the values of local variables, and so on. It operates in a manner similar to the stack in the data structure.
2. Stacking area (heap)
Generally, it is allocated and released by the programmer. If the programmer does not release it, the program may be reclaimed by OS at the end of the program. Unlike a heap in a data structure, it is allocated in a manner similar to a linked list.
3. Global area (static zone) (static)
The global variables and static variables are stored together, the initialized global variables and static variables are in the same area, and the uninitialized global variables and uninitialized static variables are in the adjacent area. When the program ends, the variable is released by the system.
4. Text constant area
Stores constant strings. When the program ends, the constant string is released by the system.
5. Program code area
The binary code that holds the body of the function.
1. Allocate from static storage area. The memory program has been allocated when it is compiled, and this memory block exists throughout the run of the program. For example, global variable, static variable.
2. Create it on the stack. When the function is executed, the storage units of local variables within the function can be created on the stack, and these storage units are automatically released at the end of the function execution. The stack memory allocation operation is built into the instruction set of the processor, which is very efficient, but the allocated memory capacity is limited.
3. Allocation from the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for when to release memory with free or delete. The lifetime of dynamic memory is determined by programmers and is very flexible to use, but if space is allocated on the heap, it is the responsibility to recycle it, otherwise the running program will have memory leaks, and frequent allocation and release of heap space of different sizes will result in in-heap fragments.
1.2, stack and heap, global / static storage and constant storage comparison
1. Stack area: mainly used to store local variables, pass parameters, and store the return address of the function. The .esp always points to the top of the stack, and the more data in the stack, the smaller the value of esp.
2, heap area: used to store dynamically allocated objects, when you use malloc and new for allocation, the space you get is in the heap. Dynamically allocated memory areas are accompanied by allocation information, so you can free and delete them.
3. Data area: global, static and constant are allocated in the data area, which includes bss (uninitialized data area) and initialized data area.
Note: the heap grows to the high memory address; the stack grows to the low memory address; the heap and the stack grow in the opposite direction, and there is a tipping point between the stack and the stack, called stkbrk.
1.3, picture caption
Add:
Stack: stack, store Automatic Variables, grow from high to low according to memory address, its maximum size is determined by compile time, the speed is fast, but the freedom is poor, the maximum space is not large. Heap: heap, freely applied for space, growing from low to high according to memory address, its size is determined by the upper limit of system memory / virtual memory, the speed is slow, but the freedom is large, and the available space is large.
Each thread has its own stack, but the heap space is shared.
Reference: https://www.icode9.com/content-1-772915.html
II. C language programming demonstration 1.1. Ubuntu test code implementation
1 、 main.c:
# include # include / / define the global variable int init_global_a = 1 leading int uninits_global_b;void output (int a) {printf ("hello"); printf ("% d", a); printf ("\ n");} int main () {/ / define the local variable int Static int inits_local_c=2, uninits_local_c; int init_local_d = 1; output (a); char * p; char str [10] = "lyy"; / / define the constant string char * var1 = "1234567890"; char * var2 = "qwertyuiop"; / / dynamically allocate int * p1=malloc (4); int * p2=malloc (4); / / release free (p1); free (p2) Printf ("stack area-variable address\ n"); printf ("AGV% p\ n", & a); printf ("init_local_d:%p\ n", & init_local_d); printf ("str:%p% p\ n", & p); printf ("str:%p\ n", str) Printf ("\ nheap-dynamic application address\ n"); printf ("% p\ n", p1); printf ("% p\ n", p2); printf ("\ nglobal zone-global and static variables\ n"); printf ("\ n.bss segment\ 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 segment\ 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 ("\ ntext constant area\ n") Printf ("literal constant address:% p\ n", var1); printf ("literal constant address:% p\ n", var2); printf ("\ ncode area\ n"); printf ("program area address:% p\ n", & main); printf ("function address:% p\ n", & output); return 0;}
2. Use the command to create a main.c file:
Gedit main.c
3. Copy and paste the above code
4. Compile and execute
Gcc main.c-o main
. / main
Analysis shows that it can be concluded from the above figure that the stack memory address grows from high to low, and the stack memory address grows from low to high. And the memory of the whole program is allocated from the highest to the lowest address.
1.2. STM32 verification code implementation
Open the serial communication experiment code that I have done before, and modify it. Later, I will also give you the link to close the serial port first.
1. Code changes
Modify bsp_usart.h:
Add a header file:
# include # include
Modify bsp_usart.c:
Override the fputc function:
Int fputc (int ch, FILE * f) {USART_SendData (DEBUG_USARTx, (uint8_t) ch); while (USART_GetFlagStatus (DEBUG_USARTx, USART_FLAG_TXE) = = RESET); return (ch);}
Main.c:
# include "stm32f10x.h" # include "bsp_usart.h" / / add the bsp_usart.h header file int init_global_a = 1 position int uninit_global_a;static int inits_global_b = 2 security static int uninits_global_b; void output (int a) {printf ("hello"); printf ("% d", a); printf ("\ n") } int main (void) {/ / define the local variable int astat2; static int inits_local_c=2, uninits_local_c; int init_local_d = 1; char * p; char str [10] = "lyy"; / / define the constant string char * var1 = "1234567890"; char * var2 = "qwertyuiop" / / dynamic allocation of int * p1=malloc (4); int * p2=malloc (4); USART_Config (); / / Serial port initialization output (a); / / release of free (p1); free (p2); printf ("stack area-variable address\ n"); printf ("aura% p\ n", & a) Printf ("init_local_d:%p\ n", & init_local_d); printf ("printf% p\ n", & p); printf ("str:%p\ n", str); printf ("\ nheap area-dynamic application address\ n") Printf ("% p\ n", p1); printf ("% p\ n", p2); printf ("\ nglobal zone-global and static variables\ n"); printf ("\ n.bss segment\ n"); printf ("global external no initial 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 segment\ 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 inits_local_c:%p\ n", & inits_local_c); printf ("\ nliteral constant area\ n"); printf ("literal constant address:% p\ n", var1) Printf ("literal constant address:% p\ n", var2); printf ("\ nCode area\ n"); printf ("Program area address:% p\ n", & main); printf ("function address:% p\ n", & output); return 0;}
2. Compile output
3. Burning recording
Open the serial debugging assistant. After opening the serial port, press the RESET key to display the result:
4. Analysis and explanation
Like Ubuntu, the address value of the stack area of stm32 decreases from top to bottom, while the stack area grows from top to bottom. From the perspective of each area, the address value decreases gradually from top to bottom, that is, the address of the stack area is high, and the address of the code area is low.
1.3.Observation of stm32 storage under keil
Storage location of stm32 data
1. RAM (random access memory)
The stored contents can be randomly read and written through instructions. The data stored in RAM will be lost in the event of a power loss, so it can only be stored when it is powered on and running. Among them, RAM can be divided into two types, one is Dynamic RAM (DRAM dynamic random access memory), the other is Static RAM (SRAM). The stack, heap, and global area (.bss segment, .data segment) are all stored in RAM.
2. ROM (read only memory)
The data can only be read from it and cannot be written at will. Compared with RAM, ROM has the disadvantage of slow reading and writing. However, because it has the advantage of keeping the data unchanged after power off, it is commonly used to store programs and data written at one time, such as the chip of the main version of the BIOS program is the ROM memory. The contents of the code area and constant area are not allowed to be modified, so they are stored in ROM.
View:
Analysis description:
You can see from the picture that the address allocation of ROM starts from 0x8000000, and the whole size is 0x40000, which is used to store the code area and the text constant area. The address allocation of RAM starts from 0x20000000, and its size is 0xC000, which is used to store stack, heap, global area (.bss segment, .data segment). Compared with the code result display, we can also see that the corresponding part of the address is corresponding to the setting.
Thank you for reading this article carefully. I hope the article "how to solve the memory allocation problem of Ubuntu and stm32 in C program" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.