当前位置:网站首页>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 ~~
边栏推荐
- Cartland number application
- 【AtCoder2307】Tree Game(博弈)
- C language judging big end and small end [consortium or pointer] big end and small end conversion
- Flask页面的分页
- 【AtCoder2304】Cleaning
- [atcoder2376] black and white tree (game)
- Implementation of queue (C language)
- 20200803 T3 my friends [divide and conquer NTT optimization recursion]
- 【AtCoder2305】Decrementing(博弈)
- 【IoT】智能硬件:如何获取硬件产品的wifi信号强度
猜你喜欢

零基础自学SQL课程 | OUTER JOIN外连接

C language inherits memory management mechanism (unfinished)

C language - Growth Diary -02- function

Simple configuration of vscade

After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry

RTMP protocol
![[atcoder1980] mystious light (mathematical simulation)](/img/c0/7de31b36e11ff71328d927c1d1c2d3.png)
[atcoder1980] mystious light (mathematical simulation)

【IoT】智能硬件:如何获取硬件产品的wifi信号强度

C language - Growth Diary -01- count primes and sum
![[codeforces1019e] raining season](/img/8e/4a96954ee7dae5f81eaae05b5a075b.png)
[codeforces1019e] raining season
随机推荐
【AtCoder2304】Cleaning
Cartland number application
You got 8K in the 3-year function test, but you were actually pretending to work hard
Uoj 553 [unr 4] caproic acid set [computational geometry (points in circle → points in half plane)]
[noip2016 d1t3] changing classrooms (expectation dp+floyd) (trap of extreme thinking!)
【AtCoder2304】Cleaning
二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
放大镜子效果图
[atcoder1980] mystious light (mathematical simulation)
[cluster] lvs+keepalived high availability cluster
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
A correction book full of sad tears
Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
multi-sig SC
2021-10-24
MFC auxiliary CString string splicing
.NET C#基础(6):命名空间 - 有名字的作用域
【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
模线性方程组(中国剩余定理+通用解法)
C wechat upload form data