当前位置:网站首页>Find the cause of program dead cycle through debugging
Find the cause of program dead cycle through debugging
2022-07-25 23:44:00 【Bin * Zai】
Tips : This article is based on VS2022x86 Environmental Science .
Through debugging, the cause of the dead cycle of the program is found
Tips : The following is the main body of this article , The following cases can be used for reference
One 、 Endless loop code display
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int i = 0; int arr[10] = { 1,2,3,4,5,6,7,8,9,10 }; for (i = 0; i <= 12; i++) { arr[i] = 0; printf("hehe\n"); } return 0; }This code will not report an error , It will print in an infinite loop hehe.
Two 、 Debug analysis
When i=9 when ,hehe It has been printed nine times , Immediately assign the last element of an integer array with a capacity of 40 bytes to zero , The tenth hehe Also about to print . If the cycle condition at this time is i<10, Then the cycle is about to end . But the cycle condition here is i<=12, After the tenth cycle , For an array that can only store ten elements , Can the program continue ?
Continue debugging the program .
At this time, it is found that the program is still in progress , Although the array is out of bounds , But after the array , Still open up an area for storage arr[10].hehe Also printed 11 Time . At the same time, it is found that under the subsequent debugging , Find out arr[11] Also successfully stored . As shown below :
Then according to this situation , When the cyclic condition i Greater than 12 when , The cycle stops , Print up to 13 Time hehe. Why doesn't the cycle come out , It has been going on ?
Then go on to debug , monitor arr[12].
At this time, we found that , Enter the thirteenth cycle , stay arr[12] Before the assignment ,arr[12] It is not a random value , It's the heel i equally , All values are 12.
Guess after the array goes out of bounds , When the back is arr[12] Is the open area related to i The address of is repeated ?
In order to confirm the above speculation , We can i The address of arr[12] Take out your address , Observe whether it is the same address .
As shown in the figure , The guess is true .arr[12] The address of i The address of is the same address .
So when arr[12] To be an assignment 0 when ,i Also assigned to zero , The cycle condition returns to the starting point , There's a dead cycle .
3、 ... and 、 reason
Local data is stored in the stack .
1. The usage habit of stack area is to use the space at the high address first , Then use the space at the lower address ,i Variables are created first , Use high address ,arr After being created , Use low address .( The value of high address in memory is large , The value of low address in memory is small , It is reflected in the above figure )
2. Arrays grow with subscripts , The address changes from low to high .
3. It can be seen from the above two , When the array is operated out of bounds , The address created after the array may cover i, There may be a dead cycle .
边栏推荐
- [QNX hypervisor 2.2 user manual]9.6 GDB
- 意向不到的Dubug妙招
- Reduce method of array
- Leetcode 0135. distribute candy
- Query commodity cases (operate data with array addition method) / key points
- What is a physical firewall? What's the effect?
- ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
- 从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
- 热部署和热加载有什么区别?
- 静态代理+动态代理
猜你喜欢

Docker 安装 Redis-5.0.12(远程访问)

What is the difference between'1 and'b1 when assigning values

下一代终端安全管理的关键特征与应用趋势

Unexpected dubug tricks

BI 系统中为什么会有很多快照表?

Leetcode 0135. distribute candy
![[Database Foundation] summary of MySQL Foundation](/img/89/e22c232b0183eaae35a9f45a40ff36.jpg)
[Database Foundation] summary of MySQL Foundation

生成随机数random学习之uniform_int_distribution,uniform_real_distribution

Inheritance (the child constructor inherits the attributes in the parent constructor)

Write a select drop-down list
随机推荐
Which securities company should a novice choose to open an account? Is it safe?
TS class
S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)
[Muduo] thread package
十大排序之快速排序
获取马蜂窝酒店数据
Docker 安装 Redis-5.0.12(远程访问)
Promise resolve callback hell, async await modifier
Es5 new method
TS interface
开放API生态系统面临的十个威胁
BI 系统中为什么会有很多快照表?
762. Prime number calculation setting in binary representation
Generating random number random learning uniform_ int_ distribution,uniform_ real_ distribution
Macro task, micro task and event cycle mechanism
【MUDUO】Thread封装
Learning exploration - waves
【MUDUO】打包EventLoop和Thread
[test technology performance test LoadRunner] detailed explanation of common functions of LoadRunner
学习探索-波浪







![[ Insert picture description here ]](/img/1e/3cdb038fcffcffd7be73211f0a950a.png)