C++程序代码的内存结构分析

C++程序内部的结构,同C程序的内部结构,也是分段的,一般分为代码段、堆、栈、数据段等。可以通过下面的代码来证明:

CClassAddress.hpp 文件:

#ifndef CClassAddress_hpp
#define CClassAddress_hpp

class CClassAddress
{
public:
    CClassAddress();
    int getNum1();
    void setNum1(int a);

private:
    int mPrvNum1;
public:
    char mPubChar1;
};

#endif /* CClassAddress_hpp */

 

CClassAddress.cpp文件:

#include "CClassAddress.hpp"
#include <iostream>

using namespace std;

CClassAddress::CClassAddress()
{
    cout << __FILE__ << ":" << __LINE__ <<":"<<__FUNCTION__<<"\tlocation is\t"<<(void*)&(__FUNCTION__)<<endl;
    mPrvNum1=0;
    mPubChar1=0;
}

int CClassAddress::getNum1()
{
    cout << __FILE__ << ":" << __LINE__ <<":"<<__FUNCTION__<<"\tlocation is\t"<<(void*)&(__FUNCTION__)<<endl;
    cout<<"mPrvNum1 location is\t"<<(void*)&(mPrvNum1)<<endl;
    return mPrvNum1;
}

void CClassAddress::setNum1(int a)
{
    cout << __FILE__ << ":" << __LINE__ <<":"<<__FUNCTION__<<"\tlocation is "<<(void*)&(__FUNCTION__)<<endl;
    this->mPrvNum1 = a;
}

 

main文件:

#include "CClassAddress.hpp"
#include <iostream>

int main(int argc, const char * argv[]) {
    std::cout<<__FILE__ << ":" << __LINE__<<":"<<__FUNCTION__ << "\t"<<(void*)&__FUNCTION__<<endl;

    CClassAddress classAdd;
    cout << __FILE__ << ":" << __LINE__ << ":" <<"local classAdd: location is\t"<< (void*)&classAdd << endl;
    classAdd.setNum1(10);
    int a = classAdd.getNum1();
    cout << __FILE__ << ":" << __LINE__ << ":" <<"local a: location is\t"<< (void*)&a << endl;
    cout << __FILE__ << ":" << __LINE__ << ":" <<"public classAdd.mPubChar1: location is\t"<< (void*)&classAdd.mPubChar1 << endl;

    static int sIntA=10;
    cout << __FILE__ << ":" << __LINE__ << ":" <<"static int sIntA: location is\t"<< (void*)&sIntA << endl;

    cout<<"================="<<endl;
    CClassAddress *p_classAdd = new CClassAddress();
    cout << __FILE__ << ":" << __LINE__ << ":" <<"local *p_classAdd: location is\t"<< (void*)&p_classAdd << endl;
    cout << __FILE__ << ":" << __LINE__ << ":" <<"local &(*p_classAdd): location is\t"<< &(*p_classAdd) << endl;
    p_classAdd->setNum1(11);
    p_classAdd->getNum1();
    cout << __FILE__ << ":" << __LINE__ << ":" <<"public p_classAdd->mPubChar1: location is\t"<< (void*)&(p_classAdd->mPubChar1) << endl;
    delete p_classAdd;
    return 0;
}

 

程序的执行结果:
/Users/huGuohua/xcode/cpp_study/cpp_study/main.cpp:167:main    0x1000235a2
/Users/huGuohua/xcode/cpp_study/cpp_study/CClassAddress.cpp:16:CClassAddress    location is    0x1000236dd
/Users/huGuohua/xcode/cpp_study/cpp_study/main.cpp:170:local classAdd: location is    0x7ffeefbff438
/Users/huGuohua/xcode/cpp_study/cpp_study/CClassAddress.cpp:30:setNum1    location is 0x100023717
/Users/huGuohua/xcode/cpp_study/cpp_study/CClassAddress.cpp:23:getNum1    location is    0x1000236f9
mPrvNum1 location is    0x7ffeefbff438
/Users/huGuohua/xcode/cpp_study/cpp_study/main.cpp:173:local a: location is    0x7ffeefbff434
/Users/huGuohua/xcode/cpp_study/cpp_study/main.cpp:174:public classAdd.mPubChar1: location is    0x7ffeefbff43c
/Users/huGuohua/xcode/cpp_study/cpp_study/main.cpp:177:static int sIntA: location is    0x100024350
=================
/Users/huGuohua/xcode/cpp_study/cpp_study/CClassAddress.cpp:16:CClassAddress    location is    0x1000236dd
/Users/huGuohua/xcode/cpp_study/cpp_study/main.cpp:181:local *p_classAdd: location is    0x7ffeefbff428
/Users/huGuohua/xcode/cpp_study/cpp_study/main.cpp:182:local &(*p_classAdd): location is    0x1039110a0
/Users/huGuohua/xcode/cpp_study/cpp_study/CClassAddress.cpp:30:setNum1    location is 0x100023717
/Users/huGuohua/xcode/cpp_study/cpp_study/CClassAddress.cpp:23:getNum1    location is    0x1000236f9
mPrvNum1 location is    0x1039110a0
/Users/huGuohua/xcode/cpp_study/cpp_study/main.cpp:185:public p_classAdd->mPubChar1: location is    0x1039110a4
Program ended with exit code: 0

根据以上的结果,可以发现如下规律:
不管是main函数还是类内部的函数,地址在一起,放在代码段的;
不管是普通函数中的变量还是类中的变量,地址在一起,放在栈段的;
static的的变量,地址放在全局变量的数据段;
如果一个对象是用new创建的,他的数据放在堆中;

这就是内存中的进程的几个段:
代码段存放可执行代码、字符串常量、常量数据;
数据段存放已初始化全局变量、静态变量;
栈存放局部变量、函数参数;
堆是用来动态内存分配的;

由此可见:
C和C++的区别,只是从开发人员的角度来看,他的代码是不同,在内存中的结构是完全一样的。如果把类名和域运算符和成员函数看成一个整体,那么C++的代码,就是C结构的面向过程的了。

 

上一篇:Xcode编译时证书错误问题


下一篇:Xcode项目引入Cocoapods管理(基础篇)