当前位置:网站首页>Array out of bounds
Array out of bounds
2022-07-28 20:11:00 【Boring ~】
#include<stdio.h>
int main()
{
int i = 8;
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
printf("%d", arr[12]);
return 0;
}
Catalog
One 、 Why is the array accessed out of bounds , Will visit i Variable space ?
Two 、 Why does the compiler not report an error when accessing beyond the boundary ?
One 、 Why is the array accessed out of bounds , Will visit i Variable space ?
The first point : Local data is stored in the stack , The rule of using memory in stack area is to allocate memory space with high address , Allocate memory space from high to low .
Second point : The variable we created in this problem i And integer arrays are local , Because they are all created in braces , Therefore, the memory in the stack area will be allocated , Put their data on the stack , So the compiler will be assigned to i Variable high address space , The space allocated to the array is i Below the variable space .
The third point : The subscript of the array changes as the address increases , Subscripts vary from low to high , So when the array is accessed beyond the bounds , It will be possible to access i Variable space .
Drawing interpretation :

Be careful : Each compiler allocates memory differently , stay vs2019 in , We created based on this problem i Variables and arr The space occupied by the array is different 2 An integer byte size space , But in other compilers , Maybe not 2 Bytes of space .
This array access is a coincidence , yes C A trap of language , stay 《C Pitfalls and pitfalls 》 This book talks about .
You can imagine , Array cross-border access is actually breaking into someone else's home , And this cross-border visit , I visited my relatives , But it's not my home after all .
Two 、 Why does the compiler not report an error when accessing beyond the boundary ?
The compiler will not report all the errors you write , If he will report all the mistakes , What else should programmers do ?
The compiler can be compared to a policeman , Mistakes in the program are compared to thieves , It is impossible for the police to catch all the thieves , This is unrealistic .
边栏推荐
- Communication learning static routing across regional networks
- HSETNX KEY_ Name field value usage
- [C language] function
- 一文读懂如何部署具有外部数据库的高可用 K3s
- Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
- 83.(cesium之家)cesium示例如何运行
- There is a 'single quotation mark' problem in the string when Oracle inserts data
- Getting started with enterprise distributed crawler framework
- mmo及时战斗游戏中的场景线程分配
- 通配符 SSL/TLS 证书
猜你喜欢

The privatized instant messaging platform protects the security of enterprise mobile business

Item exception handling in SSM

Codeignier framework implements restful API interface programming
![[C language] random number generation and `include < time. H > 'learning](/img/bb/3e47bf2e3b25653d9048884d65cda3.png)
[C language] random number generation and `include < time. H > 'learning

zfoo增加类似于mydog的路由

Leetcode Day1 score ranking

A chip company fell in round B
![[C language] advanced pointer exercise 1](/img/ee/c62919330edb4a0b5a2a4b027e5b5c.png)
[C language] advanced pointer exercise 1

adb remount of the / superblock failed: Permission denied

JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
随机推荐
Overcome the "fear of looking at teeth", and we use technology to change the industry
Andorid system layout, values, drawable adaptation
English Translation Spanish - batch English Translation Spanish tools free of charge
Labelme(一)
mmo及时战斗游戏中的场景线程分配
JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
How to automatically store email attachments in SharePoint
河北邯郸:拓展基层就业空间 助力高校毕业生就业
5. Difference between break and continue (easy to understand version)
A chip company fell in round B
[C language] function
Leetcode day4 the highest paid employee in the Department
Array method added in ES6
What is the process of swing event processing?
Design of air combat game based on qtgui image interface
Two methods to judge the size end
党员故事|李青艾用漫画带动农民增收致富
Source insight project import and use tutorial
C language function
Basic usage of docker