Skip to content

C/C++基础语法 35 浅拷贝和深拷贝 优化 #157

@Wongony

Description

@Wongony
	~Student() {
        cout << "~Student " << &name << endl;
        delete name;
        name = NULL;
        }

析构函数中输出 name 指向的地址

	~Student() {
        cout << "~Student " << static_cast<void*>(name) << endl;
        delete name;
        name = NULL;
        }

//浅拷贝执行结果:
//Student
//copy Student
//~Student 0x7fffed0c3ec0
//~Student 0x7fffed0c3ec0
//*** Error in `/tmp/815453382/a.out': double free or corruption (fasttop): 0x0000000001c82c20 ***

//深拷贝执行结果:
//Student
//copy Student
//~Student 0x7fffebca9fb0
//~Student 0x7fffebca9fc0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions