// 面向对象 8-6内存管理.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//
//
//#include
//
//int main()
//{
// std::cout << “Hello World!\n”;
//}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
//面向对象
//new和delete
//#include
//using namespace std;
//void test()
//{
// //申请和释放的方式保持一致
//
// //使用malloc进行空间的申请 要进行空间大小的说明
// int* mptr = (int*)malloc(sizeof(int));
//
// free(mptr);
//
//
// //类型指针 指针变量=new 类型
// //类型指针 指针变量=new 类型(初始值)
// //类型指针 指针变量=new类型(元素个数)
//
// //使用new进行空间的申请 不需要说明申请空间的大小
// int* ptr = new int;
// //申请空间+初始化 初始值是10
// int* ptr2 = new int(10);
//
// //释放是需要的是delete
// delete ptr;
// //连续空间 申请的是10个数组空间 是随机值
// int* arr = new int[10];
//
// //释放的是连续的空间 delete[]+数组名(类似指针)
// delete[] arr; //记住释放连续的空间是需要的是[];
//}
//
//
//#include
//using namespace std;
////自定义类型的使用
//class A
//{
//public:
// //无参的构造函数
// /A()
// {
// cout << “A()” << endl;
//
// }/
//
//
//// //带参的构造函数 单参数
//// A(int a)
//// :_a(a)
////{
//// cout << “A(int a)” << endl;
////
////}
// //带参的多个参数
// A(int a,int b, int c)
// :_a(a)
// {}
//
// ~A()
// {
// cout << “资源清理” << endl;
// cout << “~A()” << endl;
//
// }
//private:
// int _a = 10;
//};
//void test()
//{
// A* mpa = (A*)malloc(sizeof(A));
// free(mpa);
//
// cout << “------------” << endl;
// //自定义类型:new 申请空间+调用构造函数进行空间内容的初始化
// A* pal = new A;
// //delete 首先是调用析构完成资源清理,再进行对象空间的释放。
// delete pal;
//
//
// //自定义类型;连续空间
// //new[]:申请空间+调用N次构造函数
// A* arrA = new A[100];
// //delete[]:调用N次析构(完成资源的清理)+对象空间的释放
// delete[] arrA;
//
// //类型指针 指针变量=new 类型(参数列表)----->调用带参构造
// A* pa2 = new A(10);
// A* pa3 = new A(1, 2, 3);
//
// //不能使用带参的构造进行多个对象的空间申请和初始化
// //A* arrA2 = new A(10)[3];
//
//}
//int main()
//{
// test();
// return 0;
//}
////new 和 delete的运算符重载的问题
//#include
//using namespace std;
////void* operator new(size_t n)
//void test()
//{
// //operator new 不是new的运算符重载函数
// //void* ptr = operator new(sizeof(int));
// //void* ptr2 = new sizeof(int);
//
// //void operator delete(void* ptr)
// //operator delete不是delete的运算符重载函数
// //operator new和operator delete是全局函数
// //他们的使用方式类似于malloc/free
// //operator new:malloc +异常
// /int ptr = (int*)operator new(sizeof(int));
// int* ptr2 = (int*)malloc(sizeof(int));
///
// int ptr = (int*)operator new(0x7fffffff);
// int* ptr2 = (int*)malloc(0x7fffffff);
//
// operator delete(ptr);
// free(ptr2);
//
//}
//int main()
//{
// test();
// return 0;
//}
//#include
//using namespace std;
//class A
//{
//public:
// A(int a = 10)
// :_a(a)
// {}
//private:
// int _a;
//};
////内置类型
//void test()
//{
////内置类型
// //new;operator new ---->malloc
// int* ptr = new int;
// //delete :operator delete—>free
// delete ptr;
// //new[];operator new[]—>operator new—>malloc
// int* ptr2 = new int[];
// //delete[];operator delete[]–>operator delete—>free
// delete[] ptr2;
//
// //自定义类型
// //new:operator new---->malloc ---->构造
// A* pa = new A;
// //delete;析构–>operator delete---->free
// delete pa;
// //new[];operator new[]—>operator new—>malloc---->N次构造
// A* pa2 = new A[10];
// //delete[];N次析构—》operator delete[]–>operator delete—>free
// delete[] pa2;
//}
//
//int main()
//{
// test();
// return 0;
//
//}
#include
using namespace std;
struct ListNode
{
ListNode* _next;
ListNode* _prev;
int _data;
void* operator new(size_t n)
{
void* p = nullptr;
p = allocator().allocate(1);
cout << “memory pool allocate” << endl;
return p;
}
void operator delete(void* p)
{
allocator().deallocate((ListNode*)p, 1);
cout << “memory pool deallocate” << endl;
}
};
class List
{
public:
List()
{
_head = new ListNode;
_head->_next = _head;
_head->_prev = _head;
}
~List()
{
ListNode* cur = _head->_next;
while (cur != _head)
{
ListNode* next = cur->_next;
delete cur;
cur = next;
}
delete _head;
_head = nullptr;
}
private:
ListNode* _head;
};
int main()
{
List l;
return 0;
}
#include
using namespace std;
class A
{
public:
A(int a)
:_a(a)
{
cout << “A(int)” << endl;
}
private:
int _a;
};
void test()
{
A* pa = (A*)malloc(sizeof(A));
//初始化已有的空间
//new定位表达式:new(指针) 类型(参数列表(可选))---->显示调用构造函数
new (pa)A(10);
pa->~A();
free(pa);
}
int main()
{
test();
return 0;
}