18.4 嵌入式指针概念及范例、内存池改进版
创始人
2024-02-17 13:58:15
0

一:嵌入式指针(embedded pointer)

1、嵌入式指针概念

一般应用在内存池相关的代码中,成功使用嵌入式指针有个前提条件:(类A对象的sizeof必须不小于4字节)

嵌入式指针工作原理:借用A对象所占用的内存空间中前4个字节,这4个字节用来链住这些空闲的内存块;但是,一旦某一块被分配出去,那么这个块的前4个字节就不再需要,此时这4个字节可以被正常使用。



2、嵌入式指针演示代码

sizeof()超过4字节的类就可以安全的使用嵌入式指针(×64指针是8位)。

#include using namespace std;class TestEP
{
public:int m_i;int m_j;int m_k;int m_l;public:struct obj  //结构{//成员,是个指针struct obj* next;  //这个next就是个嵌入式指针//自己是一个obj结构对象,那么把自己这个对象的next指针指向另外一个obj结构对象,//最终,把多个自己这种类型的对象通过链串起来};
};void func()
{TestEP mytest;cout << sizeof(mytest) << endl;  //8TestEP::obj* ptemp;  //定义一个指针ptemp = (TestEP::obj*)&mytest;  //把对象mytest首地址给了这个指针ptemp,这个指针ptemp指向对象mytest首地址cout << sizeof(ptemp->next) << endl;  //4cout << sizeof(TestEP::obj) << endl;  //4ptemp->next = nullptr;
}struct test
{
public:test* pt;
};int main()
{func();
}
TestEP mytest;
cout << sizeof(mytest) << endl;  //8
TestEP::obj* ptemp;  //定义一个指针
ptemp = (TestEP::obj*)&mytest;  //把对象mytest首地址给了这个指针ptemp,这个指针ptemp指向对象mytest首地址

&mytest 0x000000facff3f768 {m_i=-858993460 m_j=-858993460 m_k=-858993460 …}
&ptemp 0x000000facff3f798 {0x000000facff3f768 {next=0xcccccccccccccccc {next=??? }}}
&ptemp->next 0x000000facff3f768 {0xcccccccccccccccc {next=??? }}

ptemp->next = nullptr;

&ptemp 0x000000facff3f798 {0x000000facff3f768 {next=0x0000000000000000 }}
&(ptemp->next) 0x000000facff3f768 {0x0000000000000000 }

二:内存池代码的改进

单独的为内存池技术来写一个类

#include using namespace std;//专门的内存池类
class MyAllocator  //必须保证应用本类的sizeof()不少于4字节,否则会崩溃或者报错
{
public://分配内存接口void* allocate(size_t size){obj* tmplink;if (m_freePosition == nullptr){//为空,要申请一大块内存size_t realsize = m_sTrunkCount * size;  //申请m_sTrunkCount这么多倍的内存m_freePosition = (obj*)malloc(realsize);tmplink = m_freePosition;//把分配出来的这一大块内存(5小块),彼此链起来,供后续使用for (size_t i = 0; i < m_sTrunkCount - 1; i++){tmplink->next = (obj*)((char*)tmplink + size);tmplink = tmplink->next;}tmplink->next = nullptr;}tmplink = m_freePosition;m_freePosition = m_freePosition->next;return tmplink;}//释放内存接口void deallocate(void* phead){((obj*)phead)->next = m_freePosition;m_freePosition = (obj*)phead;}private://写在类内的结构,这样只让其在类内使用struct obj{struct obj* next;  //这个next就是个嵌入式指针};int m_sTrunkCount = 5;  //一次分配5倍的该类内存作为内存池的大小obj* m_freePosition = nullptr;
};//------------------------------------------
#define DECLARE_POOL_ALLOC()\
public:\static MyAllocator myalloc;\static void* operator new(size_t size)\{\return myalloc.allocate(size);\}\static void operator delete(void* phead)\{\return myalloc.deallocate(phead);\}\
//------------------------------------------
#define IMPLEMENT_POOL_ALLOC(classname)\
MyAllocator classname::myalloc;
//------------------------------------------class A
{
public:int m_i;int m_j;  //为了保证sizeof(A)凑够4字节,偃师市public:DECLARE_POOL_ALLOC();
};IMPLEMENT_POOL_ALLOC(A);void func()
{A* mypa[100];for (size_t i = 0; i < 15; i++){mypa[i] = new A();mypa[i]->m_i = 10;mypa[i]->m_j = 20;printf("%p\n", mypa[i]);}for (size_t i = 0; i < 15; i++){delete mypa[i];}
}int main()
{func();
}

相关内容

热门资讯

原创 戴... 最近,关于前国脚戴琳的欠薪丑闻无疑是引发了球迷的持续关注,从10月25日,媒体人李平康率先爆料,晒出...
思想政治工作条例最新修订内容,... 思想政治工作条例最新修订内容,思想政治工作条例全文下载 思想政治工作条例最新修订,全文下载与深度解读...
CBA潜力赛为何打成“老将赛”... 计时钟归零,双方教练握手致意,观众开始退场,CBA联赛的正赛宣告结束。然而球场并未就此沉寂,替补席上...
“手术钻头断裂遗留患者体内”,... 12月21日,湖南祁阳市卫生健康局发布情况通报称,近日,有媒体报道祁阳市中医医院发生骨科手术钻头断裂...
代驾纠纷 代驾时撞伤行人、车辆发生故障…… 这些都和车主无关,应由代驾赔偿? 观点: 使用代驾服务并非将所有...
公司股东与妻子分居期间出轨女下... 近日据报道,宁夏永宁县人民法院一审查明公司股东李某乙在与妻子李某甲分居期间,与公司女员工马某某存在不...
动物学家、律师和创作者,Thi... 12月21日,以“一起·了不起”为主题的2025 ThinkPad黑FUN礼在京举办。活动现场,律师...
徐奇渊:扩内需与对外政策紧密相... 近日,中国海关总署发布了一组数据令人关注:2025年前11个月,我国货物贸易顺差达到1.08万亿美元...
46岁上海独居女子不幸离世,官... 居住在上海虹口区46岁的蒋女士因突发脑溢血于今年10月入院,远亲吴先生与其公司共同垫付了医药费,但她...