C 内存布局 Memory Layout of C Programs

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

  1. Text segment
  2. Initialized data segment
  3. Uninitialized data segment
  4. Stack
  5. Heap

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.

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 and 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

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.

For instance a variable declared static int i; would be contained in the BSS segment.

For instance a global variable declared int j; would be contained in the BSS segment.

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.

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.

Examples.

The size(1) command reports the sizes (in bytes) of the text, data, and bss segments. ( for more details please refer man page of size(1) )

  1. Check the following simple C program
#include <stdio.h> int main(void)
{ return 0;
}
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text       data        bss        dec        hex    filename
960        248         8          1216       4c0    memory-layout
  1. Let us add one global variable in program, now check the size of bss (highlighted in red color).
#include <stdio.h> int global; /* Uninitialized variable stored in bss*/int main(void)
{ return 0;
}
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text       data        bss        dec        hex    filename
960        248         12         1220       4c4    memory-layout
#last
960        248         8          1216       4c0    memory-layout
  1. Let us add one static variable which is also stored in bss.
#include <stdio.h> int global; /* Uninitialized variable stored in bss*/int main(void)
{ static int i; /* Uninitialized static variable stored in bss */return 0;
} 
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text       data        bss        dec        hex    filename
960        248         16         1224       4c8    memory-layout
#last
960        248         12         1220       4c4    memory-layout
  1. Let us initialize the static variable which will then be stored in Data Segment (DS)
#include <stdio.h> int global; /* Uninitialized variable stored in bss*/int main(void)
{ static int i = 100; /* Initialized static variable stored in DS*/return 0;
}
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text       data        bss        dec        hex    filename
960        252         12         1224       4c8    memory-layout
#last
960        248         12         1220       4c4    memory-layout
  1. Let us initialize the global variable which will then be stored in Data Segment (DS)
#include <stdio.h> int global = 10; /* initialized global variable stored in DS*/int main(void)
{ static int i = 100; /* Initialized static variable stored in DS*/return 0;
}
[narendra@CentOS]$ gcc memory-layout.c -o memory-layout
[narendra@CentOS]$ size memory-layout
text       data        bss        dec        hex    filename
960        256         8          1224       4c8    memory-layout
#last
960        248         8          1216       4c0    memory-layout

C 内存布局 Memory Layout of C Programs相关推荐

  1. .net托管环境下struct实例字段的内存布局(Layout)和大小(Size)

    在C/C++中,struct类型中的成员的一旦声明,则实例中成员在内存中的布局(Layout)顺序就定下来了,即与成员声明的顺序相同,并且在默认情况下总是按照结构中占用空间最大的成员进行对齐(Alig ...

  2. JVM学习笔记之-对象的实例化,内存布局与访问定位,直接内存(Direct Memory)

    对象的实例化 对象的内存布局 图解 对象的访问定位 句柄访问 好处 reference中存储稳定句柄地址,对象被移动(垃圾收集时移动对象很普遍)时只会改变句柄中实例数据指针即可,reference本身 ...

  3. JOL(java object layout --java 对象内存布局)

    JOL(java object layout --java 对象内存布局) ⚠⚠⚠本文以java普通对象为切入点,分析java的对象内存布局,数组见文末 maven地址

  4. JOL(java object layout): java 对象内存布局

    我们天天都在使用java来new对象,但估计很少有人知道new出来的对象到底长的什么样子?对于普通的java程序员来说,可能从来没有考虑过java中对象的问题,不懂这些也可以写好代码.今天,给大家介绍 ...

  5. C语言layout文件有用吗,C语言内存模型 (C memory layout)

    一. 内存模型 1. .text 代码区(code section).由编译器链接器生成的可执行指令,程序执行时由加载器(loader)从可执行文件拷贝到内存中.为了安全考虑,防止别的区域更改代码区数 ...

  6. Kernel Memory Layout on ARM Linux

    这是内核自带的文档,讲解ARM芯片的内存是如何布局的!比较简单,对于初学者可以看一下!但要想深入理解Linux内存管理,建议还是找几本好书看看,如深入理解Linux虚拟内存,嵌入系统分析,Linux内 ...

  7. 操作系统实验五·xv6系统内存布局修改·栈空间位置变更与栈不足时扩充

    xv6系统内存布局修改·栈空间位置变更与栈不足时扩充 1.实验目的 2.实验内容 3. 实验手册 4. 实验环境 5. 程序设计和实现 5.1系统函数修改 5.2 编译运行 6. 实验结果和分析 1. ...

  8. slub object 内存布局

    我在 https://blog.csdn.net/wowricky/article/details/83218126 介绍了一种内存池,它的实现类似于linux 中打开slub_debug (1. m ...

  9. 一个由进程内存布局异常引起的问题

    前段时间业务反映某类服务器上更新了 bash 之后,ssh 连上去偶发登陆失败,客户端吐出错误信息如下所示: 图 - 0 该版本 bash 为部门这边所定制,但是实现上与原生版并没有不同,那么这些错误 ...

最新文章

  1. ios 如何对UITableView中的内容进行排序
  2. Qt 3D的未来展望
  3. php cli获取参数,php cli模式下获取参数的方法
  4. 系统分区 ,硬盘格式化,
  5. GPU程序在GameByro中的使用
  6. Kendo UI使用方法与教程
  7. 用matlab画旋转抛物面_MAELAB (1)画出旋转抛物面z=x^2 y^2 编程(2)matlab 画出锥面z=(x^2+y^2)^(1/2)编程...
  8. 466. 回文日期 Java题解 (模拟)
  9. 中小企业常遇到这些问题,看APS系统是如何解决的
  10. 关于 JWT、JWS、JWE
  11. MATLAB中常见数字信号处理相关函数汇总
  12. spd耗材管理流程图_医用耗材SPD管理模式详解
  13. java 时区 不正确_Java中的时区不匹配
  14. gitbook 插件 SEO
  15. MacDroid for Mac(安卓手机文件传输助手)
  16. Docke--基础篇
  17. Proteus教程——过零检测器
  18. 开发一款类似于搜索助推于生活的平民软件
  19. LINUX下LAMP与PHPWIND
  20. 股票投资 - 股票的安全边际

热门文章

  1. 计算机提示pdf不能加载,Win7打开PDF文件提示无法自定义打开程序的解法
  2. The ENU localization is not supported by this SQL Server media
  3. 富斯i6航模遥控器配apm(pix)飞控mission planner疑难杂症解决策略(上)
  4. c# - 作业4:中国象棋
  5. 如何给对方邮箱发照片_怎样给邮箱发照片 - 卡饭网
  6. OA与BPM究竟有啥区别
  7. DNS抗压集群服务器部署、远程加密更新DNS、花生壳(DDNS)--动态域名解析功能实现
  8. 怎样从盘面看主力动向
  9. IDEA自动生成实体类
  10. 10万亿的新房市场,为何没有一家现象级的电商?