Essentially, using the new expression with built-in types is similar to using operator new:
In MSVC++ 6.0, both allocations above call this code:char *buffer1 = ::new char[12]; // allocate 12 bytes (returns char *) void *buffer2 = ::operator new(12); // allocate 12 bytes (returns void *) ::delete [] buffer1; // free memory allocated by ::new ::operator delete(buffer2); // free memory allocated by ::operator new
void * operator new( unsigned int cb )
{
void *res = _nh_malloc( cb, 1 );
return res;
}
and both deallocations call this code:
void __cdecl operator delete(void *p) _THROW0()
{
free(p); // free an allocated object
}
In MSVC++ 7.1, the first call to new calls this:
void *__cdecl operator new[](size_t count) _THROW1(std::bad_alloc)
{ // try to allocate count bytes for an array
return (operator new(count));
}
and the second calls this:
void *__cdecl operator new(size_t size) _THROW1(_STD bad_alloc)
{ // try to allocate size bytes
void *p;
while ((p = malloc(size)) == 0)
if (_callnewh(size) == 0)
_STD _Nomemory();
return (p);
}
In MSVC++ 7.1, the first call to delete calls this:
void operator delete[]( void * p )
{
RTCCALLBACK(_RTC_Free_hook, (p, 0))
operator delete(p);
}
and the second calls this:
void operator delete(void *pUserData)
{
_CrtMemBlockHeader * pHead;
RTCCALLBACK(_RTC_Free_hook, (pUserData, 0));
if (pUserData == NULL)
return;
_mlock(_HEAP_LOCK); /* block other threads */
__TRY
/* get a pointer to memory block header */
pHead = pHdr(pUserData);
/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
_free_dbg( pUserData, pHead->nBlockUse );
__FINALLY
_munlock(_HEAP_LOCK); /* release other threads */
__END_TRY_FINALLY
return;
}
This allows us to call operator::new[] and operator::delete[] in MSVC 7.1:
char *buffer1 = ::new char[12]; // allocate 12 bytes (returns char *) void *buffer2 = ::operator new[](12); // allocate 12 bytes (returns void *) ::delete [] buffer1; // free memory allocated by ::new ::operator delete[](buffer2); // free memory allocated by ::operator new