Android NDK篇-C++ 函数执行流程分析以及浅拷贝和深拷贝

1.函数的执行过程分析
#include <iostream>
#include <string.h>

using namespace std;

class Douluo{
public:
    char* name;
    int level;
public:
    Douluo(){
        cout<<"null param construct"<<endl;
    }

    Douluo(char* name):Douluo(name,90){
        this->name=name;
        cout<<"one param construct"<<endl;
    }

    Douluo(char* name,int level){
        this->name=name;
        this->level=level;
        cout<<"two param construct"<<endl;
    }

    ~Douluo(){
        cout<<"destructor......"<<name<<endl;
    }

    // 默认有一个拷贝构造函数 隐士的看不见
     Douluo(const Douluo & douluo) {
        cout<<"copy constructor......"<<endl;
        this->name="mahongjun";
        this->level=douluo.level;
     }
};

Douluo getDouluo(char* name){
    Douluo douluo(name);
    cout << "getDouluo:" << &douluo << endl;
    return douluo;
}


void showDouluo(Douluo douluo){
    cout << "showDouluo:" <<douluo.name <<"  level="<<douluo.level<< endl;
}


场景1

int main() {
    Douluo tangsan;
    Douluo mahongjun;
    cout << &tangsan << endl;
    cout << &mahongjun << endl;

}

日志输出:
null param construct
null param construct
00D7F938
00D7F928
destructor......
destructor......
  • Douluo tangsan:表示在栈区开辟的空间,main函数执行完之后会自动进行弹栈操作,这样的情况不需要手动释放内存。

  • Douluo tangsan:默认会执行空参数构造函数。

场景2

int main(){
Douluo tangsan;
Douluo mahongjun=tangsan;
cout << &tangsan << endl;
cout << &mahongjun << endl;
}
日志输出:
null param construct
copy constructor......
008FF750
008FF740
destructor......
destructor......
  • 由于有赋值操作,所以会执行一次拷贝构造函数,默认情况下是不可见的,重写之后便可以清晰的看到执行流程。

场景3

int main(){
	Douluo mahongjun=getDouluo("tangsan");
	cout << "main :" << &mahongjun << endl;    
}


日志输出:
two param construct  //执行两个参数的构造函数
one param construct  //执行一个参数的构造函数
getDouluo:00AFF778   //旧的地址
copy constructor...... //执行拷贝构造函数
destructor......tangsan //执行析构函数
main :00AFF7B4         
destructor......mahongjun  //main 函数弹栈 执行析构函数

通过日志可知:

  • 先执行两个参数的构造函数,然后再执行一个参数的构造函数
  • 通过一个方法来赋值的情况,也会执行一次拷贝构造函数,赋值给另外一个对象之后会自动执行析构函数

场景4

int main(){
	Douluo mahongjun("tangsan",100);
	showDouluo(mahongjun); // 弹栈后 新地址name释放一遍
}

日志输出:
two param construct
copy constructor......  //执行拷贝构造函数
showDouluo:mahongjun  level=100
destructor......mahongjun
destructor......tangsan

通过日志可知:

即便是将一个对象当做一个参数传递进去,也是会执行拷贝构造函数,由于多生成了一个对象,所以main函数弹栈的时候,会释放两个对象。

场景5
~Douluo(){
    cout<<"destructor......"<<name<<endl;
    free(this->name);
    this->name = NULL;
}

如果在析构函数增加指针释放操作,立马会崩溃,原因就是使用的是浅拷贝,两个对象的变量执行的是同一个地址,第一释放之后已经就是null了,再次释放就会崩溃。

解决方法:

  Douluo(char* name,int level){
      //浅拷贝
//        this->name=name;
       //深拷贝
        this->name= static_cast<char *>(malloc(sizeof(char *) * 10));
        strcpy(this->name,name);
        this->level=level;
        cout<<"two param construct"<<endl;
    }

// 默认有一个拷贝构造函数 隐士的看不见
 Douluo(const Douluo & douluo) {
    cout<<"copy constructor......"<<endl;
    //开辟空间
    this->name= static_cast<char *>(malloc(sizeof(char *) * 10));
    //拷贝内容
    strcpy(this->name,"mahongjun");

    this->level=douluo.level;
 }

在两个参数的构造函数使用深拷贝,拷贝构造函数也使用深拷贝,这样就不会发生崩溃了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值