new and delete 表达式

本文解析了C++中new和delete操作符的工作原理,包括它们如何分配和释放内存、调用构造与析构函数的过程,同时介绍了C++标准库中与内存管理相关的函数实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是摘自 优快云上一篇文章

当写

B *p = new D;

这里,BDclass类型,并且有构造和析构函数,编译器实际产生的代码大约是这样的:

B *p = operator new(sizeof(D));

D::D(p);

    过程是:

l         new操作接受D对象的大小(字节为单位)作为参数。

l         new操作返回一块大小足以容纳一个D对象的内存的地址。

l         D的缺省构造函数被调用。这个构造函数传入的this指针就是刚刚返回的内存地址。

l         最终结果:*p是个完整构造了的对象,静态类型是B,动态类型是D

 

    相似的,语句

delete p;

差不多被编译为

D::~D(p);

operator delete(p);

    D的析构函数被调用,被传入的this指针是p;然后delete操作释放被分配的内存。

    new操作和delete操作其实是函数。如果你没有提供自己的版本,编译器会使用标准C++运行库头文件<new>中申明的版本:

void *operator new(std::size_t);

void operator delete(void *);

 

下面我们去VC6.0库文件看看

new.h

/***
*new.h - declarations and definitions for C++ memory allocation functions
*
*       Copyright (c) 1990-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
*       Contains the declarations for C++ memory allocation functions.
*
*       [Public]
*
****/

#if _MSC_VER > 1000
#pragma once
#endif  /* _MSC_VER > 1000 */

 

#ifndef _INC_NEW
#define _INC_NEW

#ifdef __cplusplus

#ifndef _MSC_EXTENSIONS
#include <new>
#endif  /* _MSC_EXTENSIONS */

#if !defined (_WIN32) && !defined (_MAC)
#error ERROR: Only Mac or Win32 targets supported!
#endif  /* !defined (_WIN32) && !defined (_MAC) */

#ifndef _CRTBLD
/* This version of the header files is NOT for user programs.
 * It is intended for use when building the C runtimes ONLY.
 * The version intended for public use will not have this message.
 */
#error ERROR: Use of C runtime library internal header file.
#endif  /* _CRTBLD */

#ifndef _INTERNAL_IFSTRIP_
#include <cruntime.h>
#endif  /* _INTERNAL_IFSTRIP_ */


/* Define _CRTIMP */

#ifndef _CRTIMP
#ifdef CRTDLL
#define _CRTIMP __declspec(dllexport)
#else  /* CRTDLL */
#ifdef _DLL
#define _CRTIMP __declspec(dllimport)
#else  /* _DLL */
#define _CRTIMP
#endif  /* _DLL */
#endif  /* CRTDLL */
#endif  /* _CRTIMP */


/* Define __cdecl for non-Microsoft compilers */

#if (!defined (_MSC_VER) && !defined (__cdecl))
#define __cdecl
#endif  /* (!defined (_MSC_VER) && !defined (__cdecl)) */

/* Define _CRTAPI1 (for compatibility with the NT SDK) */

#ifndef _CRTAPI1
#if _MSC_VER >= 800 && _M_IX86 >= 300
#define _CRTAPI1 __cdecl
#else  /* _MSC_VER >= 800 && _M_IX86 >= 300 */
#define _CRTAPI1
#endif  /* _MSC_VER >= 800 && _M_IX86 >= 300 */
#endif  /* _CRTAPI1 */


/* types and structures */

#ifndef _SIZE_T_DEFINED
typedef unsigned int size_t;
#define _SIZE_T_DEFINED
#endif  /* _SIZE_T_DEFINED */

#ifdef _MSC_EXTENSIONS
typedef void (__cdecl * new_handler) ();
_CRTIMP new_handler __cdecl set_new_handler(new_handler);
#endif  /* _MSC_EXTENSIONS */

#ifndef __PLACEMENT_NEW_INLINE
#define __PLACEMENT_NEW_INLINE
inline void *__cdecl operator new(size_t, void *_P)
        {return (_P); }
#if _MSC_VER >= 1200
inline void __cdecl operator delete(void *, void *)
        {return; }
#endif  /* _MSC_VER >= 1200 */
#endif  /* __PLACEMENT_NEW_INLINE */


/*
 * new mode flag -- when set, makes malloc() behave like new()
 */

_CRTIMP int __cdecl _query_new_mode( void );
_CRTIMP int __cdecl _set_new_mode( int );

#ifndef _PNH_DEFINED
typedef int (__cdecl * _PNH)( size_t );
#define _PNH_DEFINED
#endif  /* _PNH_DEFINED */

_CRTIMP _PNH __cdecl _query_new_handler( void );
_CRTIMP _PNH __cdecl _set_new_handler( _PNH );

/*
 * Microsoft extension:
 *
 * _NO_ANSI_NEW_HANDLER de-activates the ANSI new_handler. Use this special value
 * to support old style (_set_new_handler) behavior.
 */

#ifndef _NO_ANSI_NH_DEFINED
#define _NO_ANSI_NEW_HANDLER  ((new_handler)-1)
#define _NO_ANSI_NH_DEFINED
#endif  /* _NO_ANSI_NH_DEFINED */

#endif  /* __cplusplus */

#endif  /* _INC_NEW */
上面这个是C标准头,看看C++标准头

// new standard header

#if     _MSC_VER > 1000 /*IFSTRIP=IGN*/
#pragma once
#endif

#ifndef _NEW_
#define _NEW_
#include <exception>

#ifdef  _MSC_VER
#pragma pack(push,8)
#endif  /* _MSC_VER */
_STD_BEGIN
  // CLASS bad_alloc
class _CRTIMP2 bad_alloc : public exception {
public:
 bad_alloc(const char *_S = "bad allocation") _THROW0()
  : exception(_S) {}
 virtual ~bad_alloc() _THROW0()
  {}
protected:
 virtual void _Doraise() const
  {_RAISE(*this); }
 };
  // SUPPORT TYPES
struct nothrow_t {};
extern _CRTIMP2 const nothrow_t nothrow;
_STD_END

typedef void (__cdecl *new_handler)();

  // new AND delete DECLARATIONS
void __cdecl operator delete(void *) _THROW0();
void *__cdecl operator new(size_t) _THROW1(std::bad_alloc);
void *__cdecl operator new(size_t, const std::nothrow_t&)
 _THROW0();

#ifndef __PLACEMENT_NEW_INLINE
#define __PLACEMENT_NEW_INLINE
inline void *__cdecl operator new(size_t, void *_P)
 {return (_P); }
#if     _MSC_VER >= 1200 /*IFSTRIP=IGN*/
inline void __cdecl operator delete(void *, void *)
 {return; }
#endif
#endif

#ifdef _CRTIMP
_CRTIMP
#endif
 new_handler __cdecl set_new_handler(new_handler) _THROW0();

#ifdef  _MSC_VER
#pragma pack(pop)
#endif  /* _MSC_VER */

#endif /* _NEW_ */

/*
 * Copyright (c) 1994 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
 */

在去看看 new.cpp

/***
*new.cxx - defines C++ new routine
*
*       Copyright (c) 1990-1998, Microsoft Corporation.  All rights reserved.
*
*Purpose:
*       Defines C++ new routine.
*
*******************************************************************************/


#include <cruntime.h>
#include <malloc.h>
#include <new.h>
#include <stdlib.h>
#ifdef WINHEAP
#include <winheap.h>
#else  /* WINHEAP */
#include <heap.h>
#endif  /* WINHEAP */

void * operator new( unsigned int cb )
{
    void *res = _nh_malloc( cb, 1 );

    return res;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值