linux c 内存布局,linux中C程序的内存布局

本文介绍了计算机程序执行中的关键内存区域:文本段(常包含CPU执行的指令)、初始化数据段(存储初始化变量)、未初始化数据段(存储未赋值变量)、堆栈(保存函数调用信息)和堆(动态内存分配)。理解这些结构有助于开发者掌握程序内存管理的基础。
摘要由CSDN通过智能技术生成

个人比较懒,就不翻译了,直接上英文,来自于《Advanced Programming In The Unix Environment》

1. Text segment, consisting of the machine instructions that the CPU executes. Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.

2. Initialized data segment, usually called simply the data segment, containing variables that are specifically initialized in the program. For example, the C

declaration

int maxcount = 99;

appearing outside any function causes this variable to be stored in the initialized data segment with its initial value.

3. Uninitialized data segment, often called the ‘‘bss’’ segment, named after an ancient assembler operator that stood for ‘‘block started by symbol.’’ Data in this segment is initialized by the kernel to arithmetic 0 or null pointers before the program starts executing. The C declaration

long sum[1000];

appearing outside any function causes this variable to be stored in the uninitialized data segment.

4. Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.

5. Heap, where dynamic memory allocation usually takes place. Historically, the heap has been located between the uninitialized data and the stack.

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值