Memory Layout of C Programs

本文详细介绍了C程序的内存布局,包括文本段、初始化数据段、未初始化数据段(BSS)、堆栈和堆等五个主要部分。文本段包含可执行指令,初始化数据段存放已初始化的全局和静态变量,未初始化数据段存放未初始化的变量,堆栈用于存储局部变量和函数调用信息,堆用于动态内存分配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 学习 Memory Layout of C Programs

1.程序内存存储区域

  A typical memory representation of C program consists of following sections.

  • Text segment
  • Initialized data segment
  • Uninitialized data segment
  • Stack
  • Heap

如下图所示:
在这里插入图片描述
1.1.Text Segment

  A text segment , also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions.

  As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it.

  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.

1.2.Initialized Data Segment

  Initialized data segment, usually called simply the Data Segment. A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer.

  Note that, data segment is not read-only, since the values of the variables can be altered at run time.

  This segment can be further classified into :

  • initialized read-only area
  • initialized read-write area.

  For instance the global string defined by char s[] = “hello world” in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global C statement like const char* string = “hello world” makes the string literal “hello world” to be stored in initialized read-only area and the character pointer variable string in initialized read-write area.

  Ex: static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment.

1.3.Uninitialized Data Segment
  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 before the program starts executing.

  uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.

  • static int i; would be contained in the BSS segment.
  • a global variable declared int j; would be contained in the BSS segment.

1.4.Stack

  The stack area traditionally adjoined the heap area and grew the opposite direction; when the stack pointer met the heap pointer, free memory was exhausted. (With modern large address spaces and virtual memory techniques they may be placed almost anywhere, but they still typically grow opposite directions.)

  The stack area contains the program stack, a LIFO structure, typically located in the higher parts of memory. On the standard PC x86 computer architecture it grows toward address zero; on some other architectures it grows the opposite direction. A “stack pointer” register tracks the top of the stack; it is adjusted each time a value is “pushed” onto the stack. The set of values pushed for one function call is termed a “stack frame”; A stack frame consists at minimum of a return address.

  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.

1.5.Heap

  Heap is the segment where dynamic memory allocation usually takes place.

  The heap area begins at the end of the BSS segment and grows to larger addresses from there.The Heap area is managed by malloc, realloc, and free, which may use the brk and sbrk system calls to adjust its size (note that the use of brk/sbrk and a single “heap area” is not required to fulfill the contract of malloc/realloc/free; they may also be implemented using mmap to reserve potentially non-contiguous regions of virtual memory into the process’ virtual address space). The Heap area is shared by all shared libraries and dynamically loaded modules in a process.

2.Check the segment

Documents/work/code/albert$ size hello
   text	   data	    bss	    dec	    hex	filename
   1256	    560	      8	   1824	    720	hello

参考资料:
https://www.geeksforgeeks.org/memory-layout-of-c-program/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值