Memory Layout of C Programs

from apue 7.6. Memory Layout of a C Program

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

1. Text segment
2. Initialized data segment
    2.1
initialized read-only area
    2.2 initialized read-write
area
3. Uninitialized data segment
4. Heap
5. Stack

Read Memory
Layout of C Programs
 for more details. It‘s compiled from apue 7.6 with
more details and examples.

做個實驗吧

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include<iostream>

using
namespace std;

int main(){

    int
a1,a2,a3,a4,a5;

    int
*b1=new
int;    //在C語言中 用的是malloc()

    int
*b2=new
int;

    int
*b3=new
int;

    int
*b4=new
int;

    cout << "address of a1 is "
<< &a1 << endl;

    cout << "address of a2 is "
<< &a2 << endl;

    cout << "address of a3 is "
<< &a3 << endl;

    cout << "address of a4 is "
<< &a4 << endl;

    cout << "address of a5 is "
<< &a5 << endl;

    cout << "address of b1 is "
<< &b1 << endl;

    cout << "  value of b1 is "
<< b1 << endl;

    cout << "address of b2 is "
<< &b2 << endl;

    cout << "  value of b2 is "
<< b2 << endl;

    cout << "address of b3 is "
<< &b3 << endl;

    cout << "  value of b3 is "
<< b3 << endl;

    cout << "address of b4 is "
<< &b4 << endl;

    cout << "  value of b4 is "
<< b4 << endl;

    delete
b1,b2,b3,b4;

}

address of a1 is 0x7fff4e62c554
address of a2 is
0x7fff4e62c550
address of a3 is
0x7fff4e62c54c
address of a4 is
0x7fff4e62c548
address of a5 is
0x7fff4e62c544
address of b1 is
0x7fff4e62c538
value of b1 is 0x417a010
address
of b2 is 0x7fff4e62c530
value of b2 is
0x417a030
address of b3 is 0x7fff4e62c528
value
of b3 is 0x417a050
address of b4 is
0x7fff4e62c520
value of b4 is
0x417a070
可以發現,a1到a5的記憶體位址是由大而小,也就是由高而低。而b1到b4的所指的位址(在heap)是由小而大,也就是由低而高,b1到b4本身的位址(在stack)則是由高而低。

Other references:

<<Advanced Programming in the UNIX Environment>> 7.6. Memory
Layout of a C Program

Memory
layout of C process (pdf)
download

Data
segment

[0x03]. Notes on Assembly - Memory from a process‘ point of
view

Structure of a C-Program in Memory | How Heap,Stack,Data and
Code segments are stored in memory?

Memory Layout of C Programs,布布扣,bubuko.com

时间: 2024-10-05 02:55:12

Memory Layout of C Programs的相关文章

Memory Layout for Multiple and Virtual Inheritance

2016/6/7 phc ­­ Memory Layout for Multiple and Virtual Inheritance ­­ Edsko de Vrieshttp://www.phpcompiler.org/articles/virtualinheritance.html 1/10Home | Download phc | Documentation | Developers and Contributors | Mailing List |Memory Layout for Mu

Memory Layout (Virtual address space of a C process)

Memory Layout (Virtual address space of a C process) 分类: C语言基础2012-12-06 23:16 2174人阅读 评论(0) 收藏 举报 found a good example to demostrate the memory layout and its stack info of a user-mode process, only that this example is for Linux. But it is still wo

Memory Layout for Multiple and Virtual Inheritance (一) (部分翻译)

Memory Layout for Multiple and Virtual Inheritance  Sources: http://www.phpcompiler.org/articles/virtualinheritance.html 本文主要介绍了gcc编译器中multiple 和 virtual inheritance中的对象内存布局.虽然C++程序员不需要关心编译器的内部细节,但多重继承(特别是虚拟继承)在C++代码实现中有不同的结果(特别是用pointers to pointers

C语言内存模型 (C memory layout)

 一. 内存模型                                                                         1. .text 代码区(code section).由编译器链接器生成的可执行指令,程序执行时由加载器(loader)从可执行文件拷贝到内存中.为了安全考虑,防止别的区域更改代码区数据(即可执行指令),代码区具有只读属性.另一个方面,代码区通常具有可共享性(sharable),即在内存中只有一份代码区,如编译器,假如同时有多个编译任务

C++ 多继承和虚继承的内存布局(Memory Layout for Multiple and Virtual Inheritance)

警告. 本文有点技术难度,需要读者了解C++和一些汇编语言知识. 在本文中,我们解释由gcc编译器实现多继承和虚继承的对象的布局.虽然在理想的C++程序中不需要知道这些编译器内部细节,但不幸的是多重继承(特别是虚拟继承)的实现方式有各种各样的不太明确的结论(尤其是,关于向下转型指针,使用指向指针的指针,还有虚拟基类的构造方法的调用命令). 如果你了解多重继承是如何实现的,你就能预见到这些结论并运用到你的代码中.而且,如果你关心性能,理解虚拟继承的开销也是非常有用的.最后,这很有趣. :-) 多重

Memory layout

Text Segment       Text Segment,通常也被称为代码段. 为了防止 heap 或是 stack 的溢出,text 段常被安排在 heap 或是 stack 之后. Text 段通常是sharable 的, 所以对于使用频次比较高的程序,在内存中一般只有一份拷贝.另外,Text 段也常常只是 read-only 的,其目的是防止其中存放的指令被意外的修改. Initialized Data Segment Initialized Data Segment 通常被称作数据

说说iOS与内存管理(上)

http://www.cocoachina.com/ios/20150625/12234.html 说起内存管理,看似老生常谈,而真正掌握内存管理的核心其实并不简单.ARC/MRR以及“谁分配谁就负责释放”这种基本原则是很重要的,但不是本文要讨论的重点.之前本人还没在小站发过相关的文章,本篇文章中,我本人是想结合实际开发和调试中遇到的一些细节问题,来谈谈iOS的内存管理内在机制和调试方法. 上一篇文章已经是4月份的了,时间飞快又过去了好久,小站5月份没有文章更新,罪过罪过.最近小站的站长我又转换

深入理解BSS(Block Started by Symbol)

理解ELF的BSS section, 可以概括为: Uninitialized global/static data "Block Started by Symbol" "Better Save Space" Has section header but occupies no space CSAPP一书对bss的描述如下: .bss: 未被初始化的全局的C变量.这一节在o文件中不占实际的空间,只是一个place holder.o文件格式之所以区分初始化的变量和未被

Understanding glibc malloc【待译】

今天尝试用Valgrind调试程序时,发现堆和栈的一些问题没有理解透彻,于是Google了下"Memory Layout C",接着就通过Memory Layout of C Programs以及Anatomy of a Program in Memory复习了以前的一些知识点并且学习了一些新的点, 在阅读过程中查看mallopt的manual时了解到 M_MXFAST 参数时,顺便搜了下"Fastbins linux",结果就看到了这篇待翻译的文章,大概看了下后觉