careercup-C和C++ 13.4

13.4 深拷贝和浅拷贝有什么区别,如何使用?

解答

浅拷贝并不复制数据,只复制指向数据的指针,因此是多个指针指向同一份数据。 深拷贝会复制原始数据,每个指针指向一份独立的数据。通过下面的代码, 可以清楚地看出它们的区别:

struct Test{
    char *ptr;
};
void shallow_copy(Test &src, Test &dest){
    dest.ptr = src.ptr; //指向同一块内存空间,如果一个指针将该内存空间删除,另一个指针将出现错误
}
void deep_copy(Test &src, Test &dest){
    dest.ptr = (char*)malloc(strlen(src.ptr) + 1); //重新分配内存空间
    memcpy(dest.ptr, src.ptr);
}

浅拷贝在构造和删除对象时容易产生问题,因此使用时要十分小心。如无必要, 尽量不用浅拷贝。当我们要传递复杂结构的信息,而又不想产生另一份数据时, 可以使用浅拷贝,比如引用传参。浅拷贝特别需要注意的就是析构时的问题, 当多个指针指向同一份内存时,删除这些指针将导致多次释放同一内存而出错。

实际情况下是很少使用浅拷贝的,而智能指针是浅拷贝概念的增强。 比如智能指针可以维护一个引用计数来表明指向某块内存的指针数量, 只有当引用计数减至0时,才真正释放内存。

大部分时候,我们用的是深拷贝,特别是当拷贝的结构不大的时候。

时间: 2024-07-30 09:59:31

careercup-C和C++ 13.4的相关文章

python 各模块

01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支持模块 12 _ _builtin_ _ 模块 121 使用元组或字典中的参数调用函数 1211 Example 1-1 使用 apply 函数 1212 Example 1-2 使用 apply 函数传递关键字参数 1213 Example 1-3 使用 apply 函数调用基类的构造函数 122

转:Python标准库(非常经典的各种模块介绍)

Python Standard Library 翻译: Python 江湖群 10/06/07 20:10:08 编译 0.1. 关于本书 0.2. 代码约定 0.3. 关于例子 0.4. 如何联系我们 核心模块 1.1. 介绍 1.2. _ _builtin_ _ 模块 1.3. exceptions 模块 1.4. os 模块 1.5. os.path 模块 1.6. stat 模块 1.7. string 模块 1.8. re 模块 1.9. math 模块 1.10. cmath 模块

[CareerCup] 13.10 Allocate a 2D Array 分配一个二维数组

13.10 Write a function in C called my2DAlloc which allocates a two-dimensional array. Minimize the number of calls to malloc and make sure that the memory is accessible by the notation arr[i][j]. 这道题让我们写个C语言函数my2DAlloc用来给一个二维数组分配内存,并且让我们尽可能的少调用malloc

[CareerCup] 13.6 Virtual Destructor 虚析构函数

13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面这段代码: class Foo { public: void f(); }; class Bar: public Foo { public: void f(); }; Foo *p = new Bar(); p->f(); 调用p->f()会调用基类中的f(),这是因为f()不是虚函数.为了调用派

[CareerCup] 13.5 Volatile Keyword 关键字volatile

13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义,volatile有易变的易挥发的意思,在C/C++里,表示告知编译器某个变量可能会由程序外部改变,比如操作系统,硬件或者其他的线程.由于变量会由无法预期的改变,所有编译器每次都需要从内存中读取变量值.我们可以如下定义一个整型变量为volatile: int volatile x; volatile int x

[CareerCup] 13.9 Aligned Malloc and Free Function 写一对申请和释放内存函数

13.9 Write an aligned malloc and free function that supports allocating memory such that the memory address returned is divisible by a specific power of two. EXAMPLE align_malloc (1000,128) will return a memory address that is a multiple of 128 and t

[CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the number of inputs is small, which data structure options can be used instead of a hash table? 这道题让我们比较哈希表和STL中的map数据结构,在遇到这道题之前,我一直以为map就是c++中的哈希表呢,原来是不同的啊-

[CareerCup] 13.7 Node Pointer 节点指针

13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete copy of the passed in data structure. The Node data structure contains two pointers to other Nodes. 在这道题让我们通过一个节点指针来复制整个数据结构,节点类Node中包含两个节点指针,我们需要用哈希表来

[CareerCup] 13.8 Smart Pointer 智能指针

13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates, that simulates a pointer while also providing automatic garbage collection. It automatically counts the number of references to a SmartPointer<T*>

[CareerCup] 18.4 Count Number of Two 统计数字2的个数

18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字