当前位置:网站首页>A detailed explanation of one of the causes of dead loop caused by array out of bounds in C language
A detailed explanation of one of the causes of dead loop caused by array out of bounds in C language
2022-06-11 07:44:00 【Fried tomatoes 110】
Let's take a look at this code first (VS development environment ):
#include <stdio.h>
int main()
{
int i = 0;
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
for (i = 0; i <= 12; i++)
{
arr[i] = 0;
printf("hello\n");
}
return 0;
}You can guess at VS What is the final output of the development environment , It's output 13 individual hello Or compile and report errors or something else ? Forget it , The answer is announced. , It's a dead circle , Output numerous hello. Why is this so , Let me explain it carefully ~
By debugging , We can find out arr[12] The value of is always the same as i The value of is the same , That is to say, when arr[12] When the change ,i It's going to change , And vice versa :

Here we can guess whether the addresses of the two are the same , Otherwise, why would you change me ? By looking at the addresses of both , Found that it was really the same !

??? Why? ???
Before we talk , We need to know first , In this program ,i and arr Arrays are local variables , Local variables are stored in the stack area .
The habit of using stack is to define variables in the order of code , Allocate space at the high address first , Reallocate the space at the lower address , The array grows with the subscript , The address is getting bigger . The following figure can simply show the above contents :

Pictured , If i and arr If the space between arrays is appropriate , It is possible to use arr The array was accessed backward out of bounds i, Lead to arr When a value in the array changes i The value of is also changed , This can easily lead to a dead cycle .
Finally, I would like to add , The running result of the above code is that the environment is strictly dependent on the compilation environment , The final result may be different . For example VC6.0 in ,i and arr Is a continuous , No space in the middle . stay gcc in i and arr There is a space between . stay VS2010,2013,2019 Wait ,i and arr There are 2 Space , That is, the situation mentioned above . So! , In order to avoid all kinds of unpredictable situations , You'd better pay attention when you write the program , Don't let the array access beyond the bounds ~~
边栏推荐
- 【HDU6357】Hills And Valleys(DP)
- [poj3691] DNA repair (AC automata +dp)
- Qstring to hexadecimal qstring
- Nosqlzoo question brushing-1
- 【AtCoder2304】Cleaning
- Flask页面的分页
- Use of wordcloud
- 20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
- 二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...
- QT custom control library creation
猜你喜欢

Nim product

Summary of classic interview questions

【Oracle 数据库】奶妈式教程day03 排序查询

Post-processing of ffmpeg miscellaneous notes
![20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] &](/img/90/99356e679a52890a0b88068d082bbe.jpg)
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track

Aiop introduction

群晖DS918创建m.2 固态硬盘SSD读写缓存

QObject usage skills -- control function class
![[Oracle database] mammy tutorial day04 Sorting Query](/img/79/9db26aa2d9dbb5514427edf03004f4.png)
[Oracle database] mammy tutorial day04 Sorting Query

如何准备PMP新版大纲考试?
随机推荐
C language to achieve minesweeping games
Classes and objects (Part 2)
C language - Growth Diary -01- count primes and sum
Sdl-4 PCM playback
Rabin Miller prime test
Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
[atcoder2307] tree game
Nim product
Rabin-Miller素数测试
【AtCoder1984】Wide Swap (拓扑排序转化)
VIM common commands
零基础自学SQL课程 | OUTER JOIN外连接
Euler's theorem and its extension (with proof)
二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
Summary of classic interview questions
[Oracle database] mammy tutorial day02 use of database management tool sqlplus
【AtCoder2304】Cleaning
Sort - merge sort
【Oracle 数据库】奶妈式教程day03 排序查询
群晖DS918创建m.2 固态硬盘SSD读写缓存