CSGuide icon indicating copy to clipboard operation
CSGuide copied to clipboard

对于 https://csguide.cn/cpp/object_oriented/deep_copy_and_shallow_copy.html 中的代码有些疑问

Open EdGrass opened this issue 10 months ago • 2 comments

对于 https://csguide.cn/cpp/object_oriented/deep_copy_and_shallow_copy.html 中的代码有些疑问:作者给出的代码即使我删去深拷贝的部分,也就如下代码,仍然没有出现修改一方会影响另一方的现象

#include <iostream>
#include <cstring>

class MyClass {
public:
    MyClass(const char* str) {
        data = new char[strlen(str) + 1];
        strcpy(data, str);
    }

    // 深拷贝的拷贝构造函数
    // MyClass(const MyClass& other) {
    //     data = new char[strlen(other.data) + 1];
    //     strcpy(data, other.data);
    // }

    // 深拷贝的赋值运算符重载
    // MyClass& operator=(const MyClass& other) {
    //     if (this == &other) {
    //         return *this;
    //     }
        
    //     delete[] data;
    //     data = new char[strlen(other.data) + 1];
    //     strcpy(data, other.data);
        
    //     return *this;
    // }
    
   void SetString(const char* str) {
     if (data != NULL) {
       delete[] data;
     }
     data = new char[strlen(str) + 1];
     strcpy(data, str);
   }
   
    ~MyClass() {
        delete[] data;
    }

    void print() {
        std::cout << data << std::endl;
    }

private:
    char* data;
};

int main() {
    MyClass obj1("Hello, World!");
    MyClass obj2 = obj1; // 深拷贝

    obj1.print(); // 输出:Hello, World!
    obj2.print(); // 输出:Hello, World!

    // 修改obj2中的数据,不会影响obj1,反之亦然
    obj2.SetString("Test");
    obj1.print(); // 输出:Hello, World!
    obj2.print(); // 输出:Test
    return 0;
}

EdGrass avatar Feb 27 '25 11:02 EdGrass

似乎在不同的电脑上,这份代码的运行结果与报错都不相同:

  • 在我的电脑上:为注释的输出,无报错
  • 在同学电脑上:后两次为 Test,有报错,因为delete两次

似乎在我的电脑上会进行深拷贝,这是为什么

EdGrass avatar Feb 27 '25 11:02 EdGrass

class MyClass {
public:
    MyClass(char* str) {
        data = new char[strlen(str) + 1];
        strcpy(data, str);
    }
  
    ~MyClass() {
        delete[] data;
    }

    void print() {
        std::cout << data << std::endl;
    }

    char* data;
};

int main() {
    char str[] = "Hello, World!"; 
    MyClass obj1(str);
    {
        MyClass obj2 = obj1; // 调用默认拷贝构造函数
		printf("%p\n",obj1.data);	// 000002416c240e10
		printf("%p\n",obj2.data);	// 000002416c240e10
        obj2.print(); // Hello, World!
    }
	printf("%p\n",obj1.data);	// 000002416c240e10
    obj1.print();  // Hello, World!
    return 0;
}

仍然是浅拷贝,只是访问已释放或重复释放内存的未定义行为(UB)由C++标准将具体处理权下放至编译器和平台的内存管理器实现,不同系统对内存回收、复用及错误检测的策略差异导致其表现不可预测。

EdGrass avatar Feb 27 '25 13:02 EdGrass