当前位置:网站首页>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
边栏推荐
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
- leetcode003--判断一个整数是否为回文数
- JS détermine si la chaîne de nombres contient des caractères
- Arduino UNO r3+LCD1602+DHT11
- Sword finger offer: the path with a certain value in the binary tree (backtracking)
- leetcode007--判断字符串中的括号是否匹配
- Unity攝像頭跟隨鼠標旋轉
- L2-011 play binary tree (build tree + BFS)
- Unity摄像头跟随鼠标旋转
- Jetpack -- lifecycle usage and source code analysis
猜你喜欢
test
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
Innovative practice of short video content understanding and generation technology in meituan
Recommended scheme for national production of electronic components for wireless charging
Leetcode 1547: minimum cost of cutting sticks
Spark FAQ sorting - must see before interview
Wechat payment function
Installation and deployment of Flink and wordcount test
POI export message list (including pictures)
随机推荐
unity摄像机旋转带有滑动效果(自转)
Recommended scheme for national production of electronic components for wireless charging
做数据可视化应该避免的8个误区
Innovation training (V) configuration information
No such file or directory problem while executing shell
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
Innovation training (10)
MySQL - index
Innovation training (XII) reptile
js 判斷數字字符串中是否含有字符
Repair of self calibration SPC failure of Tektronix oscilloscope dpo3054
leetcode008--实现strStr()函数
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
leetcode005--原地删除数组中的重复元素
Unity camera rotation with sliding effect (rotation)
QML advanced (IV) - drawing custom controls
redis和mysql区别
Record the ThreadPoolExecutor main thread waiting for sub threads
Opencv + clion face recognition + face model training
Open the past and let's start over.