#C++内存管理
##per-class allocator,2
#include<cstddef>
#include<iostream>
using namespace std;
class Screen {
public:
Screen();
Screen(int x) :i(x) {};
int get() { return i; }
void* operator new(size_t);
void operator delete(void*, size_t);
private:
Screen* next;
int i;
static Screen* freeStore;
static const int screenChunk;
};
Screen* Screen::freeStore = 0;
const int Screen::screenChunk = 24;
Screen::Screen()
{
i = 0;
// next = (Screen *)malloc(sizeof (Screen));
next = 0;
}
/*
Screen::Screen(int x)
{
i = x;
next = (Screen*)malloc(sizeof(Screen));
}
*/
void* Screen::operator new(size_t size)
{
Screen* p;
if (!freeStore)
{
//linked list 是空的,所以申请一大块
size_t chunk = screenChunk * size;
freeStore = p = reinterpret_cast<Screen*>(new char[chunk]);
//reinterpret_cast运算符是用来处理无关类型之间的转换;它会产生一个新
//的值,这个值会有与原始参数(expression)有完全相同的比特位。
//将一大块分割片片,当做linked list 串接起来
for (; p != &freeStore[screenChunk - 1]; ++p)
p->next = p + 1;
p->next = 0;
}
p = freeStore;
freeStore = freeStore->next;
return p;
}
void Screen::operator delete(void* p, size_t)
{
//将deleted object插回 free list前端
(static_cast<Screen*>(p))->next = freeStore;
freeStore = static_cast<Screen*>(p);
}
int main()
{
cout << sizeof(Screen) << endl;
size_t const N = 100;
Screen* p[N];
for (int i = 0; i < N; ++i)
{
p[i] = new Screen(i);
}
for (int i = 0; i < 10; ++i)
cout << p[i] << endl;
cout << p[1] - p[0] << endl;
cout << p[24] - p[0] << endl;
cout << "p[23]:" << p[23] << endl;
cout << "p[24]:" << p[24] << endl;
cout << p[24] - p[23] << endl;
for (int i = 0; i < 10; ++i)
delete p[i];
cout << "_______________________________\n";
Screen* pt1[N];
for (int i = 0; i < 10; i++)
pt1[i] = ::new Screen(i);;
for (int i = 0; i < 10; i++)
cout << pt1[i] << endl;
cout << pt1[1] - pt1[0] << endl;
cout << "_______________________________\n";
Screen* pt[N];
for (int i = 0; i < 10; i++)
pt[i] = (Screen*)malloc(sizeof(Screen));
for (int i = 0; i < 10; i++)
cout << pt[i] << endl;
cout << pt1[1] - pt1[0] << endl;
return 0;
}
#per-class allocator,2(嵌入式指针应用)
另一个博主对这段代码的详细说明!
#include<cstddef>
#include<iostream>
using namespace std;
static int times;
class Airplane{
private:
struct AirplaneRep
{
unsigned long miles;
char type;
};
private:
union {
AirplaneRep rep; //此栏针对使用中的objects
Airplane* next; //此栏针对free list 上的object
};
public:
unsigned long getMiles() { return rep.miles; }
char getType() { return rep.type; }
void set(unsigned long m, char t)
{
rep.miles = m;
rep.type = t;
}
public:
Airplane()
{
next = 0;
}
static void* operator new(size_t size);
static void operator delete(void *deadObject,size_t size);
void operator<<(Airplane p[]);
private:
static const int BLOCK_SIZE;
static Airplane* headOfFreeList; //跟踪自由链表的表头,表头指针声明为静态成员很重要,因为整个类只有一个自由链表,而不是每个airplane对象都有
};
Airplane* Airplane::headOfFreeList;
const int Airplane::BLOCK_SIZE = 8;
void Airplane::operator<<(Airplane p[])//重载,没有被指针正确调用
{
cout << p << p->getMiles() << " " << p->getType() << endl;
}
void* Airplane::operator new(size_t size)
{
//如果大小有误,转交给::operator new()
if (size != sizeof(Airplane))
return ::operator new(size);
Airplane* p = headOfFreeList;
if (p)//如果P有效,就把list头部下移一个元素
headOfFreeList = p->next;
else
{
//freeList 已空,申请(分配)一大块
Airplane* newBlock = static_cast<Airplane*>(::operator new(BLOCK_SIZE * sizeof(Airplane)));
//将小块串成一个freeList
//但跳过#0,因为它将传回作为本次成果
for (int i = 1; i < BLOCK_SIZE - 1; ++i)
newBlock[i].next = &newBlock[i + 1];
newBlock[BLOCK_SIZE - 1].next = 0; //结束list
p = newBlock;
headOfFreeList = &newBlock[1];
}
//显示每次headOfFreeList指针变量的变化,headOfFreeList指针将指针数组的地址用链表链接
if(headOfFreeList)
cout << "times: " << times++ << " headOfFreeList address: " << headOfFreeList << " next: " << headOfFreeList->next << endl;
return p;
}
void Airplane::operator delete(void* deadObject, size_t size)
{
if (deadObject == 0)
return;
if (size != sizeof(Airplane))
{
::operator delete(deadObject);
return;
}
Airplane* carcass =
static_cast<Airplane*>(deadObject);
carcass->next = headOfFreeList;
headOfFreeList = carcass;
cout << "times: " << times-- << " headOfFreeList address: " << headOfFreeList << " next: " << headOfFreeList->next << endl;
}
int main()
{
cout << sizeof(Airplane) << endl;
size_t const N = 10;
Airplane* p[N],*pt[N];
cout << "/*******显示headOfFreeList指针变量的变化及调用次数*********/\n";
for (int i = 0; i < N; ++i)
p[i] = new Airplane;
cout << "/*******显示headOfFreeList指针变量的变化及调用次数*********/\n";
//随机测试object正常否
p[1]->set(1000, 'A');
p[5]->set(2000, 'B');
p[9]->set(500000, 'C');
//输出前十个pointer
//用以比较期间隔
cout << "---------------operator new分配的内存间隔,不含cookie ------------------------\n";
for (int i =0; i < 10; ++i)
cout << "指针数组 "<<i<<":"<<p[i]<<" miles: "<<p[i]->getMiles()<<" Type: "<<p[i]->getType()<<endl;
cout <<"指针数组间隔"<< p[1] - p[0] << endl;
cout << "---------------指针指向空间内存分配情况 ------------------------\n";
cout<<"p[1][1]: " << &p[1][1] << endl;
cout << "p[1][2]: " << &p[1][2] << endl;
cout << "p[1][3]: " << &p[1][3] << endl;
cout << "p[3][3]: " << &p[3][3] << endl;
cout << "p[3][4]: " << &p[3][4] << endl;
cout << "/*******显示headOfFreeList指针变量的变化及调用次数*********/\n";
for (int i = 0; i < 10; ++i)
delete p[i];
cout << "/*******显示headOfFreeList指针变量的变化及调用次数*********/\n";
cout << "---------------malloc分配的内存间隔 ,含cookie------------------------\n";
for (int i = 0; i < N; ++i)
pt[i] = (Airplane * )malloc(sizeof(Airplane)* 8);
for (int i = 0; i < 10; ++i)
cout << "指针数组 " << i << ":" << pt[i] << endl;
cout << "指针数组间隔" << pt[1] - pt[0] << endl;
cout << "指针数组间隔" << pt[2] - pt[1] << endl;
cout << "指针数组间隔" << pt[3] - pt[2] << endl;
for (int i = 9; i < 10; ++i)
free(pt[i]);
cout << pt[5] << endl;
}