Plex,内存块的申请

struct KK_Plex     // warning variable length structure
{
 KK_Plex* pNext;
/*
#if (_AFX_PACKING >= 8)
 DWORD dwReserved[1];    // align on 8 byte boundary
#endif
*/

 void* data() { return this+1; }
 
 static KK_Plex* Create(KK_Plex*& head, unsigned int nMax, unsigned int cbElement);
 // like 'calloc' but no zero fill
 // may throw memory exceptions
 
 void FreeDataChain();       // free this one and links
};

/************************************************************************/
/*   Class: KK_Plex                                                     */
/************************************************************************/
KK_Plex* KK_Plex::Create(KK_Plex*& pHead, unsigned int nMax, unsigned int cbElement)
{
 KK_Assert(nMax > 0 && cbElement > 0);
 KK_Plex* p = (KK_Plex*) new unsigned char[sizeof(KK_Plex) + nMax * cbElement];
 // may throw exception
 p->pNext = pHead;
 pHead = p;  // change head (adds in reverse order for simplicity)
 return p;
}

void KK_Plex::FreeDataChain()     // free this one and links
{
 KK_Plex* p = this;
 while (p != ((void *)0))
 {
  unsigned char* bytes = (unsigned char*) p;
  KK_Plex* pNext = p->pNext;
  delete[] bytes;
  p = pNext;
 }
}
/************************************************************************/
/*   End Class: KK_Plex                                                 */
/************************************************************************/

习这,知道了指引用KK_Plex*& pHead,内存的申放,可以返回数据void* data() { return this+1; },因在申请时多申KK_Plex这么大的空。数据区正好存在后,所以可以这么操作,非常方便。

----------------------------

code色的句可以看出

data()返回的是我可以存放数据的内存的首地址;(注:用void*可以转为任意的指针类型,使之具有通用性)

CPlex* Create(CPlex*& head, UINT nMax, UINT cbElement)

分配新的内存,并用新的CPlex表的Head,改参数head值为表的Head这样从多个CPlex象得到的都是唯一的:表的Head

的声明:

//MFC AFXPLEX_.H

struct CPlex       // warning variable length structure
{
CPlex* pNext;
#if (_AFX_PACKING >= 8)
DWORD dwReserved[1];      // align on 8 byte boundary
#endif
// BYTE data[maxNum*elementSize];

void* data() { return this+1; }

static CPlex* PASCAL Create(CPlex*& head, UINT nMax, UINT cbElement);
     // like 'calloc' but no zero fill
     // may throw memory exceptions

void FreeDataChain();         // free this one and links
};

实现

//MFC PLEX.CPP

CPlex* PASCAL CPlex::Create(CPlex*& pHead, UINT nMax, UINT cbElement)
{
ASSERT(nMax > 0 && cbElement > 0);
CPlex* p = (CPlex*) new BYTE[sizeof(CPlex) + nMax * cbElement];
     // may throw exception
p->pNext = pHead;
pHead = p;    // change head (adds in reverse order for simplicity)
return p;
}

void CPlex::FreeDataChain()       // free this one and links
{
CPlex* p = this;
while (p != NULL)
{
    BYTE* bytes = (BYTE*) p;
    CPlex* pNext = p->pNext;
    delete[] bytes;
    p = pNext;
}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值