几种最常见的这么做的理由:
1 static const int signature = 0XDEADBEEF; 2 typedef using char Byte; 3 void operator new(std::size_t size) throw(std::bad_alloc) 4 { 5 using namespace std; 6 size_t realSize = size + 2 * sizeof(int); 7 void * pMem = malloc(realSize); 8 if(!pMem) throw std::bad_alloc(); 9 *(static_cast<int*>(pMem)) = signature; 10 *(reinterpret_cast<int*>(static_cast<Byte*>(pMem) + realSize - sizeof(int))) = signature; 11 return static_cast<Byte *(pMem) + sizeof(int); //返回指向第一个签名之后的空间 12 }
原文:http://www.cnblogs.com/-wang-cheng/p/4889844.html