delete问题

zw1987122 2010-04-23 04:00:20
class ScrPtr {
friend class ScreenPtr;
Screen *sp;
size_t use;
ScrPtr(Screen *p): sp(p), use(1) { }
~ScrPtr() { delete sp; }
};
这是C++ Primer书上的,为什么sp没new就可以delete
...全文
107 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
only_delusion 2010-04-23
  • 打赏
  • 举报
回复
应该是前边有new 你的这个是有别的类嵌套进来了....你应该看全点...... 一般的只有NEW才配有delete
没有new delete没有意义的.........
赵4老师 2010-04-23
  • 打赏
  • 举报
回复
c:\Microsoft SDK\src\crt\delete.cpp
/***
*delete.cpp - defines C++ scalar delete routine, non-debug version
*
* Copyright (c) 1990-2001, Microsoft Corporation. All rights reserved.
*
*Purpose:
* Defines C++ delete routine.
*
*******************************************************************************/

#ifndef _DEBUG

#include <cruntime.h>
#include <malloc.h>
#include <new.h>
#include <windows.h>
#include <rtcsup.h>

void operator delete( void * p )
{
RTCCALLBACK(_RTC_Free_hook, (p, 0));

free( p );
}

#endif /* _DEBUG */

c:\Microsoft SDK\src\crt\free.c
/***
*free.c - free an entry in the heap
*
* Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
*
*Purpose:
* Defines the following functions:
* free() - free a memory block in the heap
*
*******************************************************************************/

#ifdef WINHEAP

#include <cruntime.h>
#include <malloc.h>
#include <winheap.h>
#include <windows.h>
#include <internal.h>
#include <mtdll.h>
#include <dbgint.h>
#include <rtcsup.h>

/***
*void free(pblock) - free a block in the heap
*
*Purpose:
* Free a memory block in the heap.
*
* Special ANSI Requirements:
*
* (1) free(NULL) is benign.
*
*Entry:
* void *pblock - pointer to a memory block in the heap
*
*Return:
* <void>
*
*******************************************************************************/

void __cdecl _free_base (void * pBlock)
{


if (pBlock == NULL)
return;

RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));

#ifndef _WIN64
if ( __active_heap == __V6_HEAP )
{
PHEADER pHeader;

#ifdef _MT
_mlock( _HEAP_LOCK );
__try {
#endif /* _MT */

if ((pHeader = __sbh_find_block(pBlock)) != NULL)
__sbh_free_block(pHeader, pBlock);

#ifdef _MT
}
__finally {
_munlock( _HEAP_LOCK );
}
#endif /* _MT */

if (pHeader == NULL)
HeapFree(_crtheap, 0, pBlock);
}
#ifdef CRTDLL
else if ( __active_heap == __V5_HEAP )
{
__old_sbh_region_t *preg;
__old_sbh_page_t * ppage;
__old_page_map_t * pmap;
#ifdef _MT
_mlock(_HEAP_LOCK );
__try {
#endif /* _MT */

if ( (pmap = __old_sbh_find_block(pBlock, &preg, &ppage)) != NULL )
__old_sbh_free_block(preg, ppage, pmap);

#ifdef _MT
}
__finally {
_munlock(_HEAP_LOCK );
}
#endif /* _MT */

if (pmap == NULL)
HeapFree(_crtheap, 0, pBlock);
}
#endif /* CRTDLL */
else // __active_heap == __SYSTEM_HEAP
#endif /* _WIN64 */
{
HeapFree(_crtheap, 0, pBlock);
}
}


#else /* WINHEAP */


#include <cruntime.h>
#include <heap.h>
#include <malloc.h>
#include <mtdll.h>
#include <stdlib.h>
#include <dbgint.h>

/***
*void free(pblock) - free a block in the heap
*
*Purpose:
* Free a memory block in the heap.
*
* Special Notes For Multi-thread: The non-multi-thread version is renamed
* to _free_lk(). The multi-thread free() simply locks the heap, calls
* _free_lk(), then unlocks the heap and returns.
*
*Entry:
* void *pblock - pointer to a memory block in the heap
*
*Return:
* <void>
*
*******************************************************************************/

#ifdef _MT

void __cdecl _free_base (
void *pblock
)
{
/* lock the heap
*/
_mlock(_HEAP_LOCK);

/* free the block
*/
_free_base_lk(pblock);

/* unlock the heap
*/
_munlock(_HEAP_LOCK);
}


/***
*void _free_lk(pblock) - non-locking form of free
*
*Purpose:
* Same as free() except that no locking is performed
*
*Entry:
* See free
*
*Return:
*
*******************************************************************************/

void __cdecl _free_base_lk (

#else /* _MT */

void __cdecl _free_base (

#endif /* _MT */

REG1 void *pblock
)
{
REG2 _PBLKDESC pdesc;


/*
* If the pointer is NULL, just return [ANSI].
*/

if (pblock == NULL)
return;

/*
* Point to block header and get the pointer back to the heap desc.
*/

pblock = (char *)pblock - _HDRSIZE;
pdesc = *(_PBLKDESC*)pblock;

/*
* Validate the back pointer.
*/

if (_ADDRESS(pdesc) != pblock)
_heap_abort();

/*
* Pointer is ok. Mark block free.
*/

_SET_FREE(pdesc);

/*
* Check for special conditions under which the rover is reset.
*/
if ( (_heap_resetsize != 0xffffffff) &&
(_heap_desc.proverdesc->pblock > pdesc->pblock) &&
(_BLKSIZE(pdesc) >= _heap_resetsize) )
{
_heap_desc.proverdesc = pdesc;
}
else if ( _heap_desc.proverdesc == pdesc->pnextdesc )
{
_heap_desc.proverdesc = pdesc;
}
}

#endif /* WINHEAP */

hahalxp 2010-04-23
  • 打赏
  • 举报
回复
空指针也可以delete的
删除空指针是安全的(因为它什么也没做)。所以,在写构造函数,赋值操作符,或其他成员函数时,类的每个指针成员要么指向有效的内存,要么就指向空,那在你的析构函数里你就可以只用简单地delete掉他们,而不用担心他们是不是被new过。
十八道胡同 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 zw1987122 的回复:]
class ScrPtr {
friend class ScreenPtr;
Screen *sp;
size_t use;
ScrPtr(Screen *p): sp(p), use(1) { }
~ScrPtr() { delete sp; }
};
这是C++ Primer书上的,为什么sp没new就可以delete
[/Quote]
这个只是声明,具体的实现里面会new
pengzhixi 2010-04-23
  • 打赏
  • 举报
回复
class ScreenPtr;//你贴下这个类的定义
fuyifan 2010-04-23
  • 打赏
  • 举报
回复
sp不是该类new的,此外即使sp=NULL也是可以delete的

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧