当前位置:网站首页>Analysis of memory structure of C program code
Analysis of memory structure of C program code
2022-07-27 07:25:00 【hawanglc】
The internal structure of the program is segmented , It is generally divided into code segments 、 Pile up 、 Stack 、 Data segments, etc . It can be proved by the following code :
#include <stdio.h>
int globalIntA=10;
void variableInStack()
{
printf("%s:%d:%s location is %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&__FUNCTION__);
printf("%s:%d:%s global globalIntA is %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&globalIntA);
int localIntB=100;
printf("%s:%d:%s local localIntB is %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&localIntB);
}
void variableInHeap()
{
printf("%s:%d:%s location is %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&__FUNCTION__);
char *description = malloc(10* sizeof(char));
if( description == NULL )
{
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else
{
strcpy( description, "IN HEAP");
}
printf("Description: %s\n", description );
printf("%s:%d:%s malloc description is %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&description);
printf("%s:%d:%s malloc *description is %p\n",__FILE__,__LINE__,__FUNCTION__,&(*description));
free((void*)description);
int localIntB=100;
printf("%s:%d:%s local localIntB is %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&localIntB);
static int stcIntC;
printf("%s:%d:%s static int stcIntC is %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&stcIntC);
}
int globalIntB;
int main(int argc, char ** argv)
{
printf("%s:%d:%s process begin @ %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&__FUNCTION__);
printf("%s:%d:%s global globalIntB is %p\n",__FILE__,__LINE__,__FUNCTION__,(void*)&globalIntB);
printf("====================\n");
variableInStack();
printf("====================\n");
variableInHeap();
return EXIT_SUCCESS;
}
The result of the program execution is as follows :
/Users/huGuohua/xcode/c_study/c_study/main.c:30:main process begin @ 0x100003f5e
/Users/huGuohua/xcode/c_study/c_study/main.c:31:main global globalIntB is 0x10000810c
====================
/Users/huGuohua/xcode/c_study/c_study/memoryAddress.h:18:variableInStack location is 0x100003e04
/Users/huGuohua/xcode/c_study/c_study/memoryAddress.h:19:variableInStack global globalIntA is 0x1000080d8
/Users/huGuohua/xcode/c_study/c_study/memoryAddress.h:21:variableInStack local localIntB is 0x7ffeefbff42c
====================
/Users/huGuohua/xcode/c_study/c_study/memoryAddress.h:26:variableInHeap location is 0x100003e56
Description: IN HEAP
/Users/huGuohua/xcode/c_study/c_study/memoryAddress.h:38:variableInHeap malloc description is 0x7ffeefbff428
/Users/huGuohua/xcode/c_study/c_study/memoryAddress.h:39:variableInHeap malloc *description is 0x10181e6b0
/Users/huGuohua/xcode/c_study/c_study/memoryAddress.h:42:variableInHeap local localIntB is 0x7ffeefbff424
/Users/huGuohua/xcode/c_study/c_study/memoryAddress.h:44:variableInHeap static int stcIntC is 0x100008108
Program ended with exit code: 0
Based on the above results , The following rules can be found :
The addresses of functions are together , In the code segment ;
The addresses of variables inside the function are together , On the stack ;
Global variables are placed in data segments , Static variables are also stored in the data segment ;
If a piece of memory is used malloc Created , Then it's in the pile ;
These are the segments of the process in memory :
Code snippets store executable code 、 String constant 、 const data ;
The data segment stores the initialized global variables 、 Static variables ;
Stack stores local variables 、 Function parameter ;
Heap is used for dynamic memory allocation ;
边栏推荐
猜你喜欢
随机推荐
一款开源 OA 办公自动化系统
在Perl程序中暴露Prometheus指标
MySQL index failure and solution practice
MySQL quickly compares database table data
Linear table -- stack and queue
Routing between VLANs (explanation + verification)
Flutter实战-请求封装(一)
vlan间路由(讲解+验证)
Excuse me, is there a big delay in individual capture when someone uses Oracle xStream? How to solve the delay problem
优炫数据库主要线程有哪些?
sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
Chapter 6 Shell Logic and Arithmetic
VLAN trunk实验
"Weilai Cup" 2022 Niuke summer multi school training camp 1
Which C4d cloud rendering platform to cooperate with?
Synchronized锁
Calledprocesserror during pre commit install
【WSL2】配置连接 USB 设备并使用主机的 USB 摄像头
在mac中使用docker来搭建oracle数据库服务器
Li Mu hands-on learning, in-depth learning, V2 transformer and code implementation









