ARM learning notes--Thoughts caused by variables in the program

Publisher:幸福满溢Latest update time:2016-07-19 Source: eefocusKeywords:arm  program  variable Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When I was writing code today, I encountered a strange problem. When defining a local variable array in a function, if the array is large, the program will run wild. The program will run wild in the following places:

/*This is the code that runs away*/
uint32_t UserCode[244];
UserCode[0]=1;
/*Code over*/

If the array is small, there is no problem. Then I checked the information online and sorted it out here:
1. Since local variables are created statically on the stack, the size of the stack limits the size of local variables. If the stack is too small, an overflow will occur during execution, and the program will crash. Here is a conceptual question: What is the difference between static creation and dynamic creation? Local variables occupy stack space, so what about other types of variables? Let's look at this question.
2. The difference between static creation and dynamic creation
My personal understanding: static creation is divided into two parts, one is at compile time, when the static variables and global variables we define will be automatically divided into memory space; the other is at runtime, that is, local variables, a fixed size of space is generated in the stack area; why is it static? Because the size of these variables or array spaces is known in advance. And what about dynamic creation? We use malloc and free to create and release memory space, and their size is allocated on demand, so it is dynamically created and allocated on the heap.
3. Variable types are divided into: local variables, static local variables, global variables, and static global variables. Their storage can be analyzed based on the way they are created. Static and global variables are created during compilation, so they are in the ordinary memory area. Local variables are created at runtime, on the stack, but is there any duplication between the space on the stack and the ordinary memory area? I am not clear about this at the moment, and I need to continue to deepen my understanding. In terms of scope: local variables are created when a function is called, and the system destroys them when they return; global variables can be used in all unit files of the project; static global variables can only be used in the current unit file; static local variables are only created once, and no operations are performed on them when the function returns, so the next time you enter the function, they retain the previous value and will not be cleared.
4. The difference between the heap and the stack: The heap is used to dynamically allocate space, etc., and is created and operated by the programmer; the stack is automatically allocated by the compiler, and is mainly used to store function parameter values ​​and local variables. The function call or interrupt stacking mentioned in daily life refers to the stack allocated by the compiler here.
 
OK, now let's go back to the problematic code and see how to solve it. Since we found out that the stack is insufficient, we can solve it in two other ways:
1. Change the variable array type: global or static, so that the compiler allocates memory
2. Use malloc to dynamically allocate space: Since malloc does not occupy the stack area, we can also try to change it to a global variable first.
Keywords:arm  program  variable Reference address:ARM learning notes--Thoughts caused by variables in the program

Previous article:Analysis of MINI2440 startup configuration file /etc/init.d/rcS
Next article:Arm Learning Notes--ADC Programming

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号