C++为什么要用 new 和 delete 。
大概是为了解决某些含有资源,需要确保通过析构,释放资源的类的对象留存。你可以认为是为了保障 RAII 的一种设置。
C则不必为此操心,虽然为此会更操心,但是操心也没卵用,没有class封装,只能整天提心内存泄漏,更没有 RAII 如此优雅的机制。
对于内置类型,是 OK 的,对只含有内置类型的类对象,也是 OK 的。
因为其内存和类实体是合一的,实体即内存。
以下代码是不安全的,只供学习!
#include <iostream>
#include <vector>
struct canFree
{
int intArry[3] = {0, 1, 2};
};
int main()
{
canFree *testCanFree = new canFree();
int *rawDataCf = testCanFree->intArry;
std::cout << "testCanFree before free " << std::endl;
for (int i = 0; i != 3; ++i)
{
std::cout << *(rawDataCf + i) << std::endl;
}
free(testCanFree);
std::cout << "testCanFree after free " << std::endl;
for (int i = 0; i != 3; ++i)
{
std::cout << *(rawDataCf + i) << std::endl;
}
return 0;
}
对于非内置类型,包括标准容器,则必然是 LeakMem。
因为类实体和内存是分离的,实体只包含一个符号,符号指向内存,把符号抹除,内存还在。
#include <iostream>
#include <vector>
struct canNotFree
{
std::vector<std::string> strVec = {"free", "and", "memleak"};
};
int main()
{
canNotFree *testCanNotFree = new canNotFree();
std::string *rawDataCnf = testCanNotFree->strVec.data();
std::cout << "testCanNotFree before free " << std::endl;
for (int i = 0; i != 3; ++i)
{
std::cout << *(rawDataCnf + i) << std::endl;
}
free(testCanNotFree);
std::cout << "testCanNotFree after free " << std::endl;
for (int i = 0; i != 3; ++i)
{
std::cout << *(rawDataCnf + i) << std::endl;
}
return 0;
}
相信如果运行过上面的代码,就比较容易理解了。
深入理解,可能还是需要自己实现一个符号和资源分离的类,比如 vector 容器,还要理解符号和实体之间的关系。
我写下月亮两个字,你能知道这是天上的阴晴圆缺,我把月亮两个字从笔记上擦掉,你晚上抬头,依旧悲欢离合,没有任何区别。