最近一些工作里需要处理些内存的东东,需要把delete,free,delete[]分清楚。
跟了下几个汇编
delete汇编化是:
- call destructor.
- call delete(void*)->free+一些判断
free就是free
delete[]是
- 调相应数量的destructor,
- delete(void*)
so:
- 所以如果是POD的话,错调了delete(应该调用delete[])并不会造成内存泄漏,一般的内存泄露是来源与destructor少掉了时候。
- 对于POD的话,不用在意delete[]和delete调用的正确与否,甚至直接free也可以
下面是c++的delete和free
- 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;
- }
- extern "C" _CRTIMP void __cdecl _free_dbg(
- void * pUserData,
- int nBlockUse
- )
- {
- /* lock the heap
- */
- _mlock(_HEAP_LOCK);
- __try {
- /* allocate the block
- */
- _free_dbg_nolock(pUserData, nBlockUse);
- }
- __finally {
- /* unlock the heap
- */
- _munlock(_HEAP_LOCK);
- }
- }