The computer program memory is organized into the following:

  • Data Segment (Data + BSS + Heap)
  • Stack
  • Code segment

Data

The data area contains global and static variables used by the program that are explicitly initialized with a non-zero (or non-NULL) value. This segment can be further classified into a read-only area and read-write area. For instance, the string defined by char s[] = "hello world" in C and a C statement like int debug=1 outside the "main" would be stored in initialized read-write area. And a 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: both static int i = 10 and global int i = 10 will be stored in the data segment.

data段包含程序使用的全局变量和静态变量(变量明确的使用非0或非NULL初始化).data段可以进一步的分类到read-only区和read-write区.

例如在C语言的"main"函数之外通过char s[] = "hello world"定义的字符串 和 通过int debug=1声明的变量debug将会存储在初始化的read-write区。

类似const char* string = "hello world"这样的声明,字符串"hello world"存储在初始化的read-only区, 指针变量string存储在read-write区.

BSS

The BSS segment, also known as uninitialized data, is usually adjacent to 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.

BSS段也被称为未初始化数据,它通常邻进data段,包含所有的全局变量和静态变量(变量被初始化为0 或者 没有在代码中进行显示初始化).

例如使用static int i;语句声明的变量i会被包含在BSS段.

Heap

The heap area commonly begins at the end of the .bss and .data segments 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 threads, shared libraries, and dynamically loaded modules in a process.

Heap(堆)段通常在.bss段 和.data段的结尾处开始,并且开始向打的地址增长.

Heap段是使用函数malloc\realloc\free等函数进行管理,这些函数可能会使用brk\sbrk系统调用调整Heap段的大小.

(需要注意的是 一个heap区 不是只能使用brk/sbrk来调整大小, 也可以使用mmap保留潜在的不连续区域的虚拟内存到进行的虚拟地址空间)

Heap段是一个进程中所有线程、共享库、动态加载的模块 所共享的。

Stack

The stack area contains the program stack, a LIFO structure, typically located in the higher parts of memory. 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. Automatic variables are also allocated on the stack.

The stack area traditionally adjoined the heap area and they grew towards each other; when the stack pointer met the heap pointer, free memory was exhausted. With large address spaces and virtual memory techniques they tend to be placed more freely, but they still typically grow in opposite directions. On the standard PC x86 architecture the stack grows toward address zero, meaning that more recent items, deeper in the call chain, are at numerically lower addresses and closer to the heap. On some other architectures it grows the opposite direction.

栈段包含了程序的栈,他是一个LIFO(后进先出)结构,通常位于内存较高的部分.一个栈指针寄存器保存着栈顶位置。

每次有值压栈,栈指针寄存器就会进行调整。为了进行函数调用而压栈一组数值 这种方式叫做stack frame(栈帧:栈帧也叫过程活动记录,是编译器用来实现函数调用的一种数据结构,从逻辑上讲,栈帧就是一个函数执行的环境:函数参数、函数的局部变量、函数执行完后返回到哪里等等。)

一个栈帧最少也会包含一个返回地址。自动变量也在堆上进行分配。

栈段习惯上邻接heap 段,他们向着对方的方向增长。当栈指针和堆指针相遇就意味着free memory耗尽了。

随着地址空间的增大和虚拟内存技术的出现,趋向于 更自由的安排 堆 和 栈 的存放放置,但是他们还是向相反的方向增长。

在标准x86结构的PC上,栈向0增长,意味着:越是新的项,在调用链中越深,地址越低,更靠近heap段。

在其他结构中栈朝着相反的方向增长。

Code

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

It has a fixed size and is usually read-only. If the text section is not read-only, then the particular architecture allows self-modifying code. Fixed-position or position independent code may be shared in memory by several processes in segmented or paged memory systems.

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

在电脑方面,code段也被称为text段,或者只是简单的称为text.

code段是目标文件或内存中程序的其中一个section(节),其中包含了可执行的命令.

code段有固定的大小,并且通常是read-only的.

如果code段不是read-only的,那么这个特殊的体系结构中允许self-modifying(自修改)代码.

固定位置的代码 或者 与位置无关的代码 可以在 段存储内存系统 和 页存储内存系统 中被几个进程共享.

//example.cpp

#include <string.h> //for strcpy
#include <stdlib.h> //for malloc int a = ; //BSS
char *p1; //BSS
int b = ; //Data int main(void)
{
int c; //Stack
char s[] = "abc"; //Stack
char *p2; //Stack
char *p3 = ""; //123456'\0' Data段,p3在Stack上。
static int d = ; //BSS
p1 = (char *)malloc(); //Heap
strcpy(p1, ""); //123456'\0'Data段,编译器可能会将它与p3所指向的"123456"优化成一个地方。 free(p1);
return ;
}

---------------------------------------------------------------------------------------------------
参考资料:

http://bbs.csdn.net/topics/390737887

http://en.wikipedia.org/wiki/Data_segment

http://en.wikipedia.org/wiki/Code_segment

程序空间(Program memory)的更多相关文章

  1. STM8S——Flash program memory and data EEPROM

    1.简介 STM8S内部的FLASH程序存储器和数据EEPROM是由一组通用寄存器来控制的:所以我们可以通过这些通用寄存器来编程或擦除存储器的内容.设置写保护.或者配置特定的低功耗模式.我们也可以自己 ...

  2. detect data races The cost of race detection varies by program, but for a typical program, memory usage may increase by 5-10x and execution time by 2-20x.

    小结: 1. conflicting access 2.性能危害 优化 The cost of race detection varies by program, but for a typical ...

  3. 记一次java程序out of memory问题

    在一个比较大批量的pdf转String项目中遇到了:java.lang.OutOfMemoryError: Java heap space错误 第一反应肯定是程序没有写好,大量循环时没有把程序中没有用 ...

  4. 服务器上运行程序Out of memory 解决办法

    ****** 服务器上跑过程序经常能遇到out of memory 这个问题,下面是我经常在实验室碰到的解决方法. 1.使用命令nvidia-smi,看到GPU显存被占满: 2.尝试使用 ps aux ...

  5. 一次 Go 程序 out of memory 排查及反思

    前言 最近在搞数据导出模块,在测试大文件下载的过程中,报了 Out of memory (OOM) 错误,因为之前没有遇到过这类问题,导致此次排查问题花费了大半天,也走了不少弯路,特此复盘记录. 现象 ...

  6. Xcode 运行程序,左侧memory 不显示内存

    运行程序后,xcode 不显示当前使用的内存情况,问题是打开了僵尸--enable zoombie Objects,关闭即可 打开 product--->SCheme-->EditSChe ...

  7. Arduino PROGMEM 从程序空间读取float值的方法

    方法: 使用avr-libc提供的宏定义: #define pgm_read_float_near(address_short) __LPM_float((uint16_t)(address_shor ...

  8. java程序out of memory【转】

    相信大家都有感触,线上服务内存OOM的问题,是最难定位的问题,不过归根结底,最常见的原因: 本身资源不够 申请的太多 资源耗尽 58到家架构部,运维部,58速运技术部联合进行了一次线上服务内存OOM问 ...

  9. GPU程序缓存(GPU Program Caching)

    GPU程序缓存 翻译文章: GPU Program Caching 总览 / 为什么 因为有一个沙盒, 每一次加载页面, 我们都会转化, 编译和链接它的GPU着色器. 当然不是每一个页面都需要着色器, ...

随机推荐

  1. Couchbase server---Enyim.Caching.dll

    本文不打算抄袭官方或者引用他人对Couchbase的各种描述,仅仅是自己对它的一点理解(错误之处,敬请指出),并附上一个入门示例. ASP.NET Web项目(其他web开发平台也一样)应用规模小的时 ...

  2. win8.1上安装vc6

    win8.1上安装vc6 1.以管理员方式运行SETUP.EXE,然后一路下一步 2.这里需要一点点耐心,等10分钟左右就能过去,电脑会比较卡,有点像假死,还是没有死掉,等等就好了 3.这里选择vc6 ...

  3. LoadRunner - 当DiscuzNT遇上了Loadrunner(中) (转发)

    当DiscuzNT遇上了Loadrunner(中) 在上文中,介绍了如果录制脚本和设置脚本执行次数.如果经过调试脚本能够正常工作的话,就可以设置并发用户数并进行压力测试了. 首先我们通过脚本编辑界面上 ...

  4. Qt5 程序发布打包

    关于qt5在win7下发布 & 打包 ----------------------------------------------------------------------------- ...

  5. NewRelicAgent(CustomAnalyticEvent.cxx.o), building for iOS simulator, but linking in object file built for OSX, for architecture x8(botched)

    昨天遇到一个问题,在项目swift1.2适配swift2.0的过程中,修改完毕之后,运行报错如下: /Pods/NewRelicAgent/NewRelic_iOS_Agent_5.1.0/NewRe ...

  6. self.a 和 _a 的区别

    在OC中我们可以通过指令@property定义属性. OC对属性封装了许多方法,同时也会自动实现一些方法,相比实例变量,感觉更加面向对象些. 一般定义属性的方法如下,在Class Test中定义属性i ...

  7. jira的插件

    jira的插件开发流程实践   怎么开头呢,由于自己比较懒,博客一直不怎么弄,以后克己一点,多传点自己遇到的问题和经历上来,供自己以后记忆,也供需要的小伙伴少走点弯路吧 最近公司项目需要竞标一个运维项 ...

  8. C++中不可重载的5个运算符

    大多数运算符都是可以重载的,但是有5个运算符C++语言规定是不可以重载的. 1. .(点运算符),通常用于去对象的成员,但是->(箭头运算符),是可以重载的 2.::(域运算符),即类名+域运算 ...

  9. fwrite ,fprintf的作用与区别

    1.概念和作用 fwrite是C语言函数,指向文件写入一个数据块,写入的是 fprintf是C/C++中的一个格式化写-库函数,其作用是格式输出到一个流/文件中:原型是int fprintf( FIL ...

  10. java中的IO流之文件复制

    O(∩_∩)O哈哈~ 1.综述 一门成熟的语言肯定具备的几个模块:IO,通信,线程,UI...... Java作为一门成熟的程序语言,其IO流是比较复杂的.上个图大家感受下: 简单分析一下,IO分为两 ...