您的位置:首页 > 服装鞋帽 > 内衣 > C++ new operator

C++ new operator

luyued 发布于 2011-01-12 05:50   浏览 N 次  



void *__CRTDECL operator new(size_t size)
........
while ((p = malloc(size)) == 0) //可见C++中也是调用malloc分配空间


extern "C" _CRTIMP void * __cdecl malloc ( size_t nSize)
{

//extern "C" c语言中声明的malloc函数
void *res = _nh_malloc_dbg(nSize, _newmode, _NORMAL_BLOCK, NULL, 0);


extern "C" void * __cdecl _nh_malloc_dbg

调用 pvBlk = _heap_alloc_dbg(nSize, nBlockUse, szFileName, nLine);


extern "C" void * __cdecl _heap_alloc_dbg(

size_t nSize,
int nBlockUse,
const char * szFileName,
int nLine
)
{//extern "C" c语言中声明的malloc函数
long lRequest;
size_t blockSize;
int fIgnore = FALSE;
_CrtMemBlockHeader * pHead; //空间块的头部
void *retval=NULL;

.....

//计算空间的大小
blockSize = sizeof(_CrtMemBlockHeader) + nSize + nNoMansLandSize;

pHead = (_CrtMemBlockHeader *)_heap_alloc_base(blockSize);

然后调用HeapAlloc函数分配空间


.....

if (_pFirstBlock)
_pFirstBlock->pBlockHeaderPrev = pHead;
else
_pLastBlock = pHead;
//插入一个链表中,可以方便管理,插在链表头
pHead->pBlockHeaderNext = _pFirstBlock;
pHead->pBlockHeaderPrev = NULL;
pHead->szFileName = (char *)szFileName;
pHead->nLine = nLine;
pHead->nDataSize = nSize; //需要分配空间大小
pHead->nBlockUse = nBlockUse;
pHead->lRequest = lRequest;


_pFirstBlock = pHead;

memset((void *)pHead->gap, _bNoMansLandFill, nNoMansLandSize);
memset((void *)(pbData(pHead) + nSize), _bNoMansLandFill, nNoMansLandSize);


memset((void *)pbData(pHead), _bCleanLandFill, nSize);




MSN空间完美搬家到新浪博客!

上一篇:去汽車旅館跟我上床? 下一篇:通灵王
图文资讯
广告赞助商