code project 上的内存管理的示例代码

/********************************************************************
created: 2014/03/17 18:53
filename: main.cpp
author: Justme0 (http://blog.csdn.net/justme0) purpose: code project 内存管理
*********************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <new>
using namespace std; // Overload the new operator
void *operator new ( size_t size , bool isAllocFailure )
{
if ( isAllocFailure )
return NULL;
else
{
try
{
void *Memory = ::operator new ( size );
return Memory;
}
catch ( std::bad_alloc )
{
return NULL;
}
}
return NULL;
} // Overload placement delete operator so that exceptions from
// constructors can be handled
void operator delete ( void * Memory , bool isAllocFailure )
{
::delete Memory;
} void SimpleFunction ( int MemCounter , bool AllFail )
{ int LocalCounter = ;
char *Mem1 = new ( ( MemCounter == LocalCounter ++ ) || AllFail ) char [ ];
if ( Mem1 != NULL )
strcpy ( Mem1 , "First Memory" ); char *Mem2 = new ( ( MemCounter == LocalCounter ++ ) || AllFail ) char [ ];
if ( Mem2 != NULL )
strcpy ( Mem2 , "Second Memory" ); int NumTimesLoop = ;
int LoopCounter = ; // Create a Variable that will be allocate Inner Memory
char **InnerMemory = new ( ( MemCounter == LocalCounter ++ ) || AllFail ) char * [ NumTimesLoop ]; // Loop through and allocate the memory required
for ( LoopCounter = ; LoopCounter < NumTimesLoop ; LoopCounter ++ )
{
InnerMemory [ LoopCounter ] = new ( ( MemCounter == LocalCounter ++ ) || AllFail ) char [ ];
} } int main()
{
for ( int counter = ; counter < ; counter ++ )
SimpleFunction ( counter , false );
SimpleFunction ( - , true ); return ;
}
上一篇:[IOT] 自制蓝牙工牌办公室定位系统 (一)—— 阿里物联网平台概览及打通端到云(硬核·干货)


下一篇:vb.net写的odbc连接dsn数据源和ole链接oracle的小例子