码迷,mamicode.com
首页 > 其他好文 > 详细

Memory Layout of C Programs

时间:2014-06-02 19:05:28      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   a   tar   

bubuko.com,布布扣

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

Memory Layout of C Programs

标签:c   class   blog   code   a   tar   

原文地址:http://www.cnblogs.com/bittorrent/p/3764367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!