当前位置:网站首页>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
边栏推荐
- Special topic of data intensive application system design
- 简单的拖拽物体到物品栏
- Spark optimization
- CLion+OpenCV identify ID number - detect ID number
- Record the ThreadPoolExecutor main thread waiting for sub threads
- leetcode009--用二分查找在数组中搜索目标值
- Sword finger offer: symmetric binary tree (recursive iteration leetcode 101)
- C# List字段排序含有数字和字符
- Leetcode004 -- Roman numeral to integer
- Spark small case - RDD, broadcast
猜你喜欢

Wine (COM) - basic concept

用LCR表完美测试无线充电系统中的线圈

Innovative practice of short video content understanding and generation technology in meituan

数据孤岛是什么?为什么2022年仍然存在数据孤岛?
![Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]](/img/99/095063b72390adea6250f7b760d78c.png)
Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]

使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘

Innovation training (V) configuration information

【数据库】MySQL多表查询(一)

POI export message list (including pictures)

Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
随机推荐
Recommended scheme of national manufactured electronic components
L2-011 玩转二叉树(建树+BFS)
C language: Advanced pointer
Innovation training (XI) airline ticket crawling company information
leetcode007--判断字符串中的括号是否匹配
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
La caméra Unity tourne avec la souris
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
leetcode002--将有符号整数的数字部分反转
【数据库】MySQL多表查询(一)
Implementation of switching windows and capturing data in selenium mode
Druid -- JDBC tool class case
Innovation training (II) task division
Sword finger offer: push in and pop-up sequence of stack
Getprop property
Practice and exploration of knowledge map visualization technology in meituan
Leetcode004 -- Roman numeral to integer
Opencv + clion face recognition + face model training
Unity3d practical skills - theoretical knowledge base (I)
MySQL time function query