当前位置:网站首页>Thoughts on a small program
Thoughts on a small program
2022-04-23 04:50:00 【Dream full stack program ape】
One , Code :
#include <stdio.h>
int main(int argc, char* argv[])
{
int i = 0;
int arr[3] = {
0};
for(; i <= 3; i++)
{
arr[i] = 0;
printf("hello world\n");
}
return 0;
}
The running result of this program , On some machines , Not printing four lines "hello word", It will print in an infinite loop "hello world".
Two , Memory allocation problem
In memory allocation , It will be allocated by increasing or decreasing the memory address
3、 ... and ,Linux Memory layout
The local variables in the function body exist on the stack , And it's a continuous stack . The picture below is Linux Memory layout of the process ; Stack area in high address space , Address increases from high address to low address .


3、 ... and , Code parsing
I know from the front , Variable i Follow arr On adjacent addresses , And i The address of arr The address is big ( Low address value is large ), From the memory layout ,i stay arr Below ; therefore ,arr Address assignment is from top to bottom , Array arr Cross the border just to visit i; namely arr[3] = 0; Think of i= 0; Cause infinite loop .
Four , Code validation
#include <stdio.h>
int main(int argc, char* argv[]){
int i = 0;
int arr[3] = {
0};
for(; i <= 3; i++){
printf(" Variable i The address for :%p\n", &i);
printf("m[0] The address for :%p\n", &arr[0]);
printf("m[1] The address for :%p\n", &arr[1]);
printf("m[2] The address for :%p\n", &arr[2]);
printf("m[3] The address for :%p\n", &arr[3]);
arr[i] = 0;
printf("hello world\n");
}
return 0;
}
gcc -fno-stack-protector main.c -o test
Variable i The address for :0x7ffde08ed5ec
m[0] The address for :0x7ffde08ed5e0
m[1] The address for :0x7ffde08ed5e4
m[2] The address for :0x7ffde08ed5e8
m[3] The address for :0x7ffde08ed5ec
版权声明
本文为[Dream full stack program ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220554572313.html
边栏推荐
- Recommended scheme for national production of electronic components for wireless charging
- What is a data island? Why is there still a data island in 2022?
- Progress of innovation training (III)
- What's the difference between error and exception
- Excel protects worksheets and workbooks from damage
- 做数据可视化应该避免的8个误区
- Record the blind injection script
- Improving 3D object detection with channel wise transformer
- IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
- FAQ of foreign lead and alliance Manager
猜你喜欢

Wechat payment function

Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition

New terminal play method: script guidance independent of technology stack

Learning Android II from scratch - activity

Flink's important basics

Small volume Schottky diode compatible with nsr20f30nxt5g

Leetcode 1547: minimum cost of cutting sticks

Druid -- JDBC tool class case

QML advanced (IV) - drawing custom controls

程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
随机推荐
Unity RawImage背景无缝连接移动
Open the past and let's start over.
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
leetcode008--实现strStr()函数
List remove an element
Unity camera rotation with sliding effect (rotation)
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
Progress of innovation training (III)
redis数据类型有哪些
Supplement 14: cmake practice project notes (to be continued 4 / 22)
leetcode002--将有符号整数的数字部分反转
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Detailed explanation of the differences between TCP and UDP
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
Special topic of data intensive application system design
Mysql50 basic exercises
Learning Android V from scratch - UI
Eight misunderstandings that should be avoided in data visualization
[paper reading] [3D target detection] point transformer