当前位置:网站首页>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 ~~
边栏推荐
- Classification of MNIST datasets with keras
- Compound RateModel合約解析
- Arduino_ Esp32 development record
- 【集群】LVS+keepalived高可用集群
- C language inherits memory management mechanism (unfinished)
- Sdl-4 PCM playback
- Ffmpe a small demo to understand 80% of common APIs
- [Oracle database] mammy tutorial day02 use of database management tool sqlplus
- RTMP protocol
- Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...
猜你喜欢

零基础自学SQL课程 | UNION 联合查询
![[software testing] 90% of the interviewers have been brushed out of such resumes](/img/2f/bb4819b98592f750dec92d4b4dd6b7.png)
[software testing] 90% of the interviewers have been brushed out of such resumes

Nim product

How to prepare for the new PMP syllabus exam?

Post-processing of ffmpeg miscellaneous notes
![20200803 T3 my friends [divide and conquer NTT optimization recursion]](/img/35/01201e3136e3dd5cd562a0481f1ee9.jpg)
20200803 T3 my friends [divide and conquer NTT optimization recursion]

Implementation of stack (C language)
![Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]](/img/f0/9d4609a53f398636b8062c625f7d3c.jpg)
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
![[Oracle database] mammy tutorial day04 Sorting Query](/img/79/9db26aa2d9dbb5514427edf03004f4.png)
[Oracle database] mammy tutorial day04 Sorting Query
![[IOT] project management: how to build a better cross functional team?](/img/df/28dbf0f7ba75d1bb3469cc15e70538.png)
[IOT] project management: how to build a better cross functional team?
随机推荐
MFC custom string linked list
The solution of "no startup device" after running Bochs
Flask页面的分页
A correction book full of sad tears
Wc2020 course selection
C language inherits memory management mechanism (unfinished)
What is the difference between gaussdb for redis and redis?
[atcoder1998] stamp Rally
C# 微信上传Form-data
Euler's theorem and its extension (with proof)
Ffmpe a small demo to understand 80% of common APIs
multi-sig SC
You got 8K in the 3-year function test, but you were actually pretending to work hard
[codeforces1019e] raining season
Bidirectional linked list simple template (pointer version)
What exactly is PMP?
Modular linear equations (Chinese remainder theorem + general solution)
mpi
Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
C language - Growth Diary -02- function