【本文链接】

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. 用php生成数据字典

    <?php header("Content-type: text/html; charset=utf-8"); $dbserver = "localhost&quo ...

  2. 【BZOJ-1406】密码箱 约数 + 乱搞 + set?

    1406: [AHOI2007]密码箱 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1143  Solved: 677[Submit][Status][ ...

  3. RequestMethod.DELETE相关,如何用jquery实现RequestMethod.DELETE请求

    Spring MVC添加支持Http的delete.put请求!(HiddenHttpMethodFilter) Spring3.0之后->Spring MVC过滤器-HiddenHttpMet ...

  4. 蓝桥T291(BFS + 输出路径)

    http://lx.lanqiao.org/problem.page?gpid=T291 学霸的迷宫   时间限制:1.0s   内存限制:256.0MB      问题描述 学霸抢走了大家的作业,班 ...

  5. leach-matlab

    http://www.oschina.net/code/snippet_860036_21044 clear;%清除变量 xm=100;%设置区域为100*100 ym=100; sink.x=0.5 ...

  6. Knockout Grid - Loading Remote Data

    http://wijmo.com/grid-with-knockout-viewmodel-loading-remote-data/ We were hearing quite a few peopl ...

  7. [Angularjs]视图和路由(二)

    写在前面 上篇文章主要介绍了视图和路由的基本概念,并在文章最后举了一个简单的使用案例.这篇文章将继续学习路由的配置,及相关参数的说明. 系列文章 [Angularjs]ng-select和ng-opt ...

  8. vc++ 6.0下Glut的配置 及 Glut 框架介绍

    2014-04-08  16:18:30 一.配置Glut 学习来源: http://blog.sina.com.cn/s/blog_5f0cf7bd0100c9oa.html 亲测可行. Glut的 ...

  9. AI顶级会议以及期刊

    AI顶级会议以及期刊 Upcoming Top Conferences NIPS 2009 UAI 2009 ICML 2009 COLT 2009 AISTATS 2009 CVPR 2009 IC ...

  10. Matalab IFS分形算法

    IFS 算法代码 function IFS_draw(M,p) N=; :length(p); eval(['a',num2str(k),'=reshape(M(',num2str(k),',:),2 ...