class A{
public:
int _a;
A(int a) :_a(a){};
};
class B{
A* _ap;
public:
B(A& a){
_ap = &a;
}
~B(){
//这里并不执行什么,但是_ap仍然被delete了。
};
};
void main(){
A* a = new A(1);
B b(*a);
delete &b;
cout << a->_a;
}
是不是不管你析构函数写的什么,都一定会delete掉其所有的成员变量?
Absolutely impossible.
In addition,
b
is not new. The result of removing itdelete
is undefined.The above two can be said to be fatal errors! ! !
Because the former will cause memory leaks
the latter will cause your program to behave in a magical way at a magic moment method of crashing, and after crashing, it is basically difficult for you to discover the secret hidden in it.
There are several problems in the subject’s program:
No. If you don't write delete, the destructor will not release the memory pointed to by the corresponding pointer.
In addition, the title class
B
also has design problems. If a class member is a pointer, then you need to consider:of
B
_ap
point to an object on the stack or an object in dynamic memory?If it points to an object on the stack, there is no need to consider memory allocation and release issues;
If it points to an object in dynamic memory, you need to consider memory allocation and release issues.
If it points to dynamic memory, who should manage (allocate, release) the pointed memory? Is it class
B
or a user of classB
(such as amain
function)?If it is managed by class
B
(which is also the usual practice), then the corresponding constructor and destructor will be responsible for the allocation and release of dynamic memory. This is what is often called Resource Acquisition Is Initialization (RAII) in C++.is managed by users of class
B
(such asmain
function), then users (such asmain
function) must carefully consider resource allocation and release issues during implementation, otherwise it will be very difficult Error prone.Finally, if you don’t have special needs, it is not recommended to use raw pointers. Instead, use smart pointers
shared_ptr
,weak_ptr
andunique_ptr
in STL. Smart pointers can automatically manage the dynamics they point to. Memory, so there is no need to consider when to release the memory, whether the same memory will be released multiple times, whether a wild pointer will be accidentally created, etc. For usage, please refer to C++ Primer 5e Chapter 12 Dynamic Memory.Only dynamic memory needs to be released manually, and the rest of the variables are automatically released when the class variable is destroyed. You don’t have dynamic memory allocation here, so you don’t need to write anything
Two things:
1. If the member variable is a pointer. Then the memory that the program needs to allocate is the memory occupied by the storage pointer (4 bytes for 32-bit programs) and the memory pointed to by the pointer. The memory occupied by the pointer is managed by the compiler, and the memory pointed to by the pointer is managed by you.
That is, the memory occupied by this pointer (4 bytes) will be automatically released in the destructor. However, the default destructor of the part of the memory pointed to by the pointer will not help you release it, because the compiler does not know whether your pointer points to memory allocated by yourself, nor does it know whether you will use this memory in the future. .
2. When the program/process is executed, all the memory occupied by the program will be released by the operating system. Even if there is no
delete
shown.