假设我们有一个没有重载operator=()函数的类X.
class X {
int n;
X() {n = 0;}
X(int _n) {n = _n;}
};
int main() {
X a; // (1) an object gets constructed here
// more code...
a = X(7); // (2) another object gets constructed here (?)
// some more code...
a = X(12); // (3) yet another object constructed here (?)
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是否在(2)构造了一个新对象?如果是,那么在(1)构造的旧对象会发生什么?是自动销毁还是解除分配(这是它)?它被覆盖了吗?
又在(3)的代码中发生了什么?
最重要的是,是否有可能通过编写如上所述的代码来导致内存泄漏?
您需要了解的是,编译器生成了很多“隐式”代码,作为新手,您不知道这些代码。我们将使用您的代码class X作为直接示例:
class X {
int n;
public: //You didn't include this, but this won't work at all unless your constructors are public
X() {n = 0;}
X(int _n) {n = _n;}
};
Run Code Online (Sandbox Code Playgroud)
在代码变成目标代码之前,但在您的编译器获得您的类定义之后,它会将您的类转换为(大致)如下所示的内容:
class X {
int n;
public:
X() {n = 0;} //Default-Constructor
X(int _n) {n = _n;} //Other Constructor
//GENERATED BY COMPILER
X(X const& x) {n = x.n;} //Copy-Constructor
X(X && x) {n = x.n;} //Move-Constructor
X & operator=(X const& x) {n = x.n; return *this;} //Copy-Assignment
X & operator=(X && x) {n = x.n; return *this;} //Move-Assignment
~X() noexcept {} //Destructor
};
Run Code Online (Sandbox Code Playgroud)
何时自动创建这些成员的规则并不是很明显(这里有一个很好的入门参考),但是现在,您可以相信在这种情况下,这正是会发生的事情。
因此,在您的main函数中,让我们回顾一下会发生什么,并通过注释关注具体细节:
int main() {
X a; //Default-Constructor called
a = X(7);//Other Constructor called, then Move-Assignment operator called,
//then Destructor called on temporary created by `X(7)`
a = X(12); //Same as previous line
return 0;
//Destructor called on `a`
}
Run Code Online (Sandbox Code Playgroud)
我们将添加更多行以显示这些调用的大多数(如果不是全部)各种排列:
int main() {
X a; //Default-Constructor
X b = a; //Copy-Constructor (uses copy-elision to avoid calling Default + copy-assign)
X c(5); //Other Constructor
X d{7}; //Also Other Constructor
X e(); //Declares a function! Probably not what you intended!
X f{}; //Default-Constructor
X g = X(8); //Other Constructor (uses copy-elision to avoid calling Other + move-assign + Destructor)
X h = std::move(b); //Move-Constructor (uses copy-elision to avoid calling Default + move-assign)
b = c; //Copy-assignment
b = std::move(d); //Move-assignment
d = X{15}; //Other Constructor, then Move-Assignment, then Destructor on `X{15}`.
//e = f; //Will not compile because `e` is a function declaration!
return 0;
//Destructor on `h`
//Destructor on `g`
//Destructor on `f`
//Destructor will NOT be called on `e` because `e` was a function declaration,
//not an object, and thus has nothing to clean up!
//Destructor on `d`
//Destructor on `c`
//Destructor on `b`
//Destructor on `a`
}
Run Code Online (Sandbox Code Playgroud)
这应该涵盖基础知识。
最重要的是,编写上述代码是否有可能导致内存泄漏?
正如所写,没有。然而,假设你的班级做了这样的事情:
class X {
int * ptr;
public:
X() {
ptr = new int{0};
}
};
Run Code Online (Sandbox Code Playgroud)
现在,您的代码会泄漏,因为每次X创建an时,您都会有一个永远不会被删除的指针。
要解决这个问题,您需要确保 A) 析构函数正确清理了指针,以及 B) 您的复制/移动构造函数/运算符是正确的。
class X {
int * ptr;
public:
X() {
ptr = new int{0};
}
X(int val) {
ptr = new int{val};
}
X(X const& x) : X() {
*ptr = *(x.ptr);
}
X(X && x) : X() {
std::swap(ptr, x.ptr);
}
X & operator=(X const& x) {
*ptr = *(x.ptr);
return *this;
}
X & operator=(X && x) {
std::swap(ptr, x.ptr);
return *this;
}
~X() noexcept {
delete ptr;
}
};
Run Code Online (Sandbox Code Playgroud)
如果在您的main函数或我的函数中按原样使用,此代码不会泄漏内存。但是,当然,如果您执行以下操作,它并不能阻止泄漏:
int main() {
X * ptr = new X{};
return 0;
//Whelp.
}
Run Code Online (Sandbox Code Playgroud)
一般来说,如果您根本不需要使用指针,建议您改用类似的东西std::unique_ptr,因为它免费提供大部分内容。
int main() {
std::unique_ptr<X> ptr{new X{}};
return 0;
//Destructor called on *ptr
//`delete` called on ptr
}
Run Code Online (Sandbox Code Playgroud)
在您的原始类中这是一个好主意,但需要注意的是,除非您明确更改它,否则您的类将不再可复制(尽管它仍然是可移动的):
class X {
std::unique_ptr<int> ptr;
public:
X() {
ptr.reset(new int{0});
}
X(int val) {
ptr.reset(new int{val});
}
//X(X && x); //auto generated by compiler
//X & operator=(X && x); //auto generated by compiler
//~X() noexcept; //auto generated by compiler
//X(X const& x); //Deleted by compiler
//X & operator=(X const& x); //Deleted by compiler
};
Run Code Online (Sandbox Code Playgroud)
我们可以看到我之前版本的变化main:
int main() {
X a; //Default-Constructor
//X b = a; //Was Copy-Constructor, no longer compiles
X c(5); //Other Constructor
X d{7}; //Also Other Constructor
X f{}; //Default-Constructor
X g = X(8); //Other Constructor (uses copy-elision to avoid calling Other + move-assign + Destructor)
X h = std::move(c); //Move-Constructor (uses copy-elision to avoid calling Default + move-assign)
//b = c; //Was Copy-assignment, no longer compiles
c = std::move(d); //Move-assignment
d = X{15}; //Other Constructor, then Move-Assignment, then Destructor on `X{15}`.
return 0;
//Destructor on `h`
//Destructor on `g`
//Destructor on `f`
//Destructor on `d`
//Destructor on `c`
//Destructor on `a`
}
Run Code Online (Sandbox Code Playgroud)
如果您想使用std::unique_ptr,但也希望生成的类是可复制的,则需要使用我讨论的技术自己实现复制构造函数。
这应该是关于它的!如果我错过了什么,请告诉我。
在第(2)点,发生了三件事:
X(int _n)构造函数构造临时对象.a.同样的事情发生在第(3)点.
在函数结束时,将a调用默认的析构函数on .
| 归档时间: |
|
| 查看次数: |
217 次 |
| 最近记录: |