龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

简明分析C/C++内存分配的解决方案(6)

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
SMemoryChunk *CMemoryPool::SkipChunks(SMemoryChunk *ptrStartChunk, unsigned int uiChunksToSkip) { SMemoryChunk *ptrCurrentChunk = ptrStartChunk; for(unsigned int i = 0; i { if(ptrCurrentChunk) { ptrCu

SMemoryChunk *CMemoryPool::SkipChunks(SMemoryChunk *ptrStartChunk, unsigned int uiChunksToSkip)

{

  SMemoryChunk *ptrCurrentChunk = ptrStartChunk;

  for(unsigned int i = 0; i < uiChunksToSkip; i++)

  {

    if(ptrCurrentChunk)

    {

      ptrCurrentChunk = ptrCurrentChunk->Next;

    }

    else

    {

      assert(false && "Error : Chunk == NULL was not expected.");

      break ;

    }

  }

  return ptrCurrentChunk;

}

  测试方法:

// 111.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "CMemoryPool.h"

CMemoryPool* g_pMemPool = NULL;

class testMemoryPool

{

public:

  testMemoryPool(){

  }

  void *operator new(std::size_t ObjectSize)

  {

    return g_pMemPool->GetMemory(ObjectSize) ;

  }

private:

  char a[25];

  bool b;

  long c;

};//sizeof(32);

int _tmain(int argc, _TCHAR* argv[])

{

  g_pMemPool = new CMemoryPool();

  testMemoryPool* test = new testMemoryPool();

  return 0;

}

精彩图集

赞助商链接