During my current project, i faced one simple, but complex situation.

ie. My TestApp code use memory allocation functions such as malloc, realloc, free etc during the earlier stages. At a later stage I changed those into C++ functions such as new and delete.

for eg:

BYTE* pbKey = NULL;
if (!(pbeKey = (BYTE *)malloc(dwKeyLen)))
 {
 // Error
 TRACE(_T("Memory Allocation error 0x%x\n"), GetLastError());
 return false;
 }

could be replaced by

BYTE *pbKey = NULL;
 if(!(pbKey = new BYTE[dwKeyLen]))
{
// Error
 TRACE(_T("Memory Allocation error 0x%x\n"), GetLastError());
 return false;
}

so simple neh? 🙂

But what about realloc() ? *-)

realloc can be implemented in simple way

BYTE *pbKey = NULL;
if(!(pbKey = new BYTE[dwKeyLen]))
{
// Error
 TRACE(_T("Memory Allocation error 0x%x\n"), GetLastError());
 return false;
}
// Create a buffer for encrypted data
BYTE* pbReallocData = NULL;
if (!(pbReallocData = new BYTE[dwNewLen]))
{
  // Error
  TRACE(_T("malloc error 0x%x\n"), GetLastError());
  return false;
}
::ZeroMemory(pbReallocData,dwNewLen);
memcpy(pbReallocData,pbData,dwKeyLen);
delete[] pbData;
...
delete[] pbReallocData;

😛