当前位置:网站首页>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
边栏推荐
- Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
- Better way to read configuration files than properties
- 拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
- Record the ThreadPoolExecutor main thread waiting for sub threads
- Progress of innovation training (III)
- POI export message list (including pictures)
- Leetcode005 -- delete duplicate elements in the array in place
- Summary of MySQL de duplication methods
- PIP3 installation requests Library - the most complete pit sorting
- QML advanced (V) - realize all kinds of cool special effects through particle simulation system
猜你喜欢
redis数据类型有哪些
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Painless upgrade of pixel series
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
Perfect test of coil in wireless charging system with LCR meter
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
What is a data island? Why is there still a data island in 2022?
做数据可视化应该避免的8个误区
Recommended scheme of national manufactured electronic components for intelligent electronic scales
MySQL queries users logged in for at least N consecutive days
随机推荐
[paper reading] [3D object detection] voxel transformer for 3D object detection
Learning Android from scratch -- Introduction
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
FAQ of foreign lead and alliance Manager
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
Unity攝像頭跟隨鼠標旋轉
The unity camera rotates with the mouse
Innovation training (XI) airline ticket crawling company information
Installation and deployment of Flink and wordcount test
Learning Android V from scratch - UI
Com alibaba. Common methods of fastjson
leetcode009--用二分查找在数组中搜索目标值
Last day of 2017
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Summary of MySQL de duplication methods
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
Unity3d practical skills - theoretical knowledge base (I)
Leetcode 1547: minimum cost of cutting sticks
QML advanced (V) - realize all kinds of cool special effects through particle simulation system