【本文链接】

http://www.cnblogs.com/hellogiser/p/operator-new.html

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/9/20
*/

#include "stdafx.h"
#include "iostream"
#include <new> // for operator new
using namespace std;

/*
throwing (1)
void* operator new (std::size_t size);

nothrow (2)
void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;

placement (3)
void* operator new (std::size_t size, void* ptr) noexcept;
*/

struct MyClass
{
    ];
    MyClass()
    {
        std::cout << "constructed [" << this << "]\n";
    }
};

void test_new_3 ()
{

std::cout << "1: ";
    MyClass *p1 = new MyClass;
    // allocates memory by calling: operator new (sizeof(MyClass))
    // and then constructs an object at the newly allocated space

std::cout << "2: ";
    MyClass *p2 = new (std::nothrow) MyClass;
    // allocates memory by calling: operator new (sizeof(MyClass),std::nothrow)
    // and then constructs an object at the newly allocated space

std::cout << "3: ";
    new (p2) MyClass;
    // does not allocate memory -- calls: operator new (sizeof(MyClass),p2)
    // but constructs an object at p2

// Notice though that calling this function directly does not construct an object:
    std::cout << "4: ";
    MyClass *p3 = (MyClass *) ::operator new (sizeof(MyClass));
    // allocates memory by calling: operator new (sizeof(MyClass))
    // but does not call MyClass's constructor

delete p1;
    delete p2;
    delete p3;
}

int main()
{
    test_new_3();
    ;
}
/*
1: constructed [0031E690]
2: constructed [0031E958]
3: constructed [0031E958]
4:
*/

【参考】

http://www.cplusplus.com/reference/new/operator%20new/

operator new3种情况详解的更多相关文章

  1. JS生成某个范围的随机数【四种情况详解】

    JS没有现成的函数,能够直接生成指定范围的随机数. 但是它有个函数:Math.random()  这个函数可以生成 [0,1) 的一个随机数. 利用它,我们就可以生成指定范围内的随机数. 而涉及范围的 ...

  2. [ 转载 ] Java开发中的23种设计模式详解(转)

    Java开发中的23种设计模式详解(转)   设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类 ...

  3. redis 五种数据结构详解(string,list,set,zset,hash)

    redis 五种数据结构详解(string,list,set,zset,hash) Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存 ...

  4. 利用C#实现AOP常见的几种方法详解

    利用C#实现AOP常见的几种方法详解 AOP面向切面编程(Aspect Oriented Programming) 是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 下面这篇文章主要 ...

  5. 多表连接的三种方式详解 hash join、merge join、 nested loop

    在多表联合查询的时候,如果我们查看它的执行计划,就会发现里面有多表之间的连接方式.多表之间的连接有三种方式:Nested Loops,Hash Join 和 Sort Merge Join.具体适用哪 ...

  6. 【Redis】redis 五种数据结构详解(string,list,set,zset,hash)

    redis 五种数据结构详解(string,list,set,zset,hash) Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存 ...

  7. Java构造和解析Json数据的两种方法详解二

    在www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别.下面接着介绍用org.json构造和解析Jso ...

  8. [转]hibernate三种状态详解

    本文来自 http://blog.sina.com.cn/u/2924525911 hibernate 三种状态详解 (2013-04-15 21:24:23) 转载▼   分类: hibernate ...

  9. android emulator启动的两种方法详解

    android emulator启动的两种方法详解    转https://blog.csdn.net/TTS_Kevin/article/details/7452237 对于android学习者,模 ...

随机推荐

  1. Xunit

    Attributes Note: This table was written back when xUnit.net 1.0 has shipped, and needs to be updated ...

  2. SEO 相关知识

    传统SEO 观念 换独立服务器 加硬件 减少http请求,压缩网页体积 css 放页面顶部,js 放页面底部 一个中等规模网站的架构 技术角度 切换不同的 User-agent Chrome 的 Ne ...

  3. CentOS/Redhat VNC 服务

    # yum install vnc-server vnc* (CentOS 5.x)# yum install tigervnc-server tigervnc (CentOS 6.x) [root@ ...

  4. linux中防CC攻击两种实现方法(转)

    CC攻击就是说攻击者利用服务器或代理服务器指向被攻击的主机,然后模仿DDOS,和伪装方法网站,这种CC主要是用来攻击页面的,导致系统性能用完而主机挂掉了,下面我们来看linux中防CC攻击方法. 什么 ...

  5. ISO 基础之 (十二) 文件管理

    一 文件管理 沙盒:让每个APP应用在手机上有一个独立的文件夹,相互之间不能访问. 沙盒目录:NSHomeDirectory() library: 库文件 tmp: 临时文件 1.NSData 也是一 ...

  6. js div浮动层拖拽效果代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. tomcat Host及Context 配置

    参考资料: 一.Host配置 对一个Tomcat,可以配置多台虚拟主机.简单地说,就是让一台服务器可以对应多个主机名.这在Tomcat中称之为Host.要求每个Host的Name必须唯一. 配置方法: ...

  8. zabbix: failed to accept an incoming connection

    错误描述 查日志发现: failed to accept an incoming connection: connection from "192.168.186.132" rej ...

  9. web.config 加密/解密

    (Aspnet_regiis.exe) 这样的一个工具,可以对站点的.config文件的节进行加密 方法: #> 加密:aspnet_regiis -pef "加密的web.confi ...

  10. CSS-3 Animation 的使用

    在开始介绍Animation之前我们有必要先来了解一个特殊的东西,那就是"Keyframes",我们把他叫做"关键帧",玩过flash的朋友可能对这个东西并不会 ...