下面这段代码会输出什么?

  1. const char* p = string("hello temprary string").c_str();
  2. cout << p;

下面这段代码运行之后会怎样?

  1. #include <vector>
  2. class Foo
  3. {
  4. public:
  5. Foo()
  6. {
  7. _p = new char[32];
  8. }
  9. ~Foo()
  10. {
  11. delete _p;
  12. }
  13. private:
  14. char* _p;
  15. };
  16. int main()
  17. {
  18. std::vector<Foo> cont;
  19. cont.push_back(Foo());
  20. return 0;
  21. }

第一个会输出奇怪的字符串,第二个会崩溃。

Why?这是临时对象提前销毁造成的。

临时对象会在什么情况下被创建,msdn上介绍有以下几种情况:

  • To initialize a const reference with an initializer of a type different from that of the underlying type of the reference being initialized.

  • To store the return value of a function that returns a user-defined type. These temporaries are created only if your program does not copy the return value to an object. For example:

     
     
    UDT Func1();    //  Declare a function that returns a user-defined
    // type. ... Func1(); // Call Func1, but discard return value.
    // A temporary object is created to store the return
    // value.

    Because the return value is not copied to another object, a temporary object is created. A more common case where temporaries are created is during the evaluation of an expression where overloaded operator functions must be called. These overloaded operator functions return a user-defined type that often is not copied to another object.

    Consider the expression ComplexResult = Complex1 + Complex2 + Complex3. The expression Complex1 + Complex2 is evaluated, and the result is stored in a temporary object. Next, the expression temporary + Complex3 is evaluated, and the result is copied to ComplexResult(assuming the assignment operator is not overloaded).

  • To store the result of a cast to a user-defined type. When an object of a given type is explicitly converted to a user-defined type, that new object is constructed as a temporary object.

临时对象何时被销毁呢,还是看msdn的介绍:

Temporary objects have a lifetime that is defined by their point of creation and the point at which they are destroyed. Any expression that creates more than one temporary object eventually destroys them in the reverse order in which they were created. The points at which destruction occurs are shown in the following table.

Destruction Points for Temporary Objects

Reason Temporary Created

Destruction Point

Result of expression evaluation

All temporaries created as a result of expression evaluation are destroyed at the end of the expression statement (that is, at the semicolon), or at the end of the controlling expressions for for, if, while, do, and switch statements.

Initializingconstreferences

If an initializer is not an l-value of the same type as the reference being initialized, a temporary of the underlying object type is created and initialized with the initialization expression. This temporary object is destroyed immediately after the reference object to which it is bound is destroyed.

注意第一条:由于表达式的计算产生的临时对象会在表达式计算完成(遇到;号)之后被销毁。

所以:

第一个问题中,p在第一行代码之后指向了一被销毁的内存。

第二个问题中,Foo在push_back的那行代码之后被销毁,_p被delete。而vector析构的时候会析构每个元素,_p又被delete,崩溃!

编译器默认的operator=只是赋值每一个字段。要解决第二个问题,可以在operator=中对_p申请另外的内存再拷贝。

关于临时对象的析构造成的问题还可以举出很多例子。掌握了它析构的时机才能很好的避免类似错误。

http://blog.csdn.net/passion_wu128/article/details/38959465

C++临时对象销毁时间的更多相关文章

  1. c# -- 对象销毁和垃圾回收

    有些对象需要显示地销毁代码来释放资源,比如打开的文件资源,锁,操作系统句柄和非托管对象.在.NET中,这就是所谓的对象销毁,它通过IDisposal接口来实现.不再使用的对象所占用的内存管理,必须在某 ...

  2. Perl面向对象(3):解构——对象销毁

    本系列: Perl面向对象(1):从代码复用开始 Perl面向对象(2):对象 Perl面向对象(3):解构--对象销毁 第3篇依赖于第2篇,第2篇依赖于1篇. perl中使用引用计数的方式管理内存, ...

  3. C++ 对象构造顺序、构析函数、临时对象。

    对象的构造顺序: 1.对于局部对象,构造顺序是根据程序执行流进行构造,从上到下. #include <stdio.h> class Test { int mi; public: Test( ...

  4. C++中的临时对象

    1,临时对象神秘在于不知不觉就请入程序当中,并且给程序带来了一定的问题: 2,下面的程序输出什么?为什么? #include <stdio.h> class Test { int mi; ...

  5. 【编程篇】C++11系列之——临时对象分析

    /*C++中返回一个对象时的实现及传说中的右值——临时对象*/ 如下代码: /**********************************************/ class CStuden ...

  6. SQL Server 内置函数、临时对象、流程控制

    SQL Server 内置函数 日期时间函数 --返回当前系统日期时间 select getdate() as [datetime],sysdatetime() as [datetime2] getd ...

  7. C++ 临时对象

    1.什么是临时对象? swap方法中,常常定义一个temp对象,这个temp对象不是临时对象,而是局部对象.这里所说的临时对象是不可见的,在原代码中是看不到的. 2.为什么会产生临时对象? a.客户期 ...

  8. 【M19】了解临时对象的来源

    1.首先,确认什么是临时对象.在swap方法中,建立一个对象temp,程序员往往把temp称为临时对象.实际上,temp是个局部对象.C++中所谓的临时对象是不可见的,产生一个non-heap对象,并 ...

  9. [转] C++中临时对象及返回值优化

    http://www.cnblogs.com/xkfz007/articles/2506022.html 什么是临时对象? C++真正的临时对象是不可见的匿名对象,不会出现在你的源码中,但是程序在运行 ...

随机推荐

  1. phonegap退出android程序

    最近用android做了一个程序,在点“后退”的时候,会不停地后退,感觉不好. 查了些资料有这么些: 一.toast_plugin插件 <script type="text/javas ...

  2. javascript 小计

    ①if文 if(){} else if(){} else if 中间有空格 ②

  3. JAVA编程思想——类型信息(反射)

    一.反射与RTTI RTTI:这个类型必须在编译的时候已知或者存在,如果不知道对象的确切类型,RTTI可以告诉你. 反射(个人认为就是能够利用Class获取或者调用.class这个文件中的数据):当我 ...

  4. GDAL库学习笔记(1):无缝拼接Google卫星图

    开工之前要先了解一下瓦片地图,瓦片地图金字塔模型是一种多分辨率层次模型,从瓦片金字塔的底层到顶层,分辨率越来越低,但表示的地理范围不变.实现原理就是,首先确定地图服务平台所要提供的缩放级别的数量N,把 ...

  5. 【配置】电信华为HG8245 无线路由器配置 有贴图

          引子:家里的电信无线路由器连接之后无法直接上上网,只能再次通过PPPoe方式拨号上网.经过网上查询和一番折腾之后,整理攻略如下. 1. 用超级用户登录192.168.1.1(默认密码) 用 ...

  6. 環氧樹脂對COB的影響

    COB的封膠一般使用單液 Epoxy(環氧樹脂),也可以使用雙液(Epoxy+硬化劑),雙液的成品品質可靠度雖然比較高,但是保存及維護非常麻煩,所以一般的 COB 都還是採用單液的製程. 其次是如何控 ...

  7. Centos 6.8下安装LBP2900打印机驱动

    今天第一次在Linux下面安装LBP2900的驱动程序,在安装的过程中出现了不少的问题,不过问题最终还是解决了. 1.下载LBP2900的Linux驱动程序: Linux_LBP2900_CAPT_P ...

  8. Web 开发中 9 个有用的提示和技巧

    在本文中,我们给出 9 个有用的 HTML.CSS 和 JavaScript 的技巧和提示,可能在做 Web 开发中经常会需要用到的,其中有几个是关于 HTML5 和 CSS3 的,如果你是一个前端开 ...

  9. Linux下如何进行FTP安装与设置

    1. 先用rpm -qa| grep vsftpd命令检查是否已经安装,如果ftp没有安装,使用yum  -y  install vsftpd 安装,(ubuntu 下使用apt-get instal ...

  10. 0ctf-pwn_warmup-re_mips4

    Warmup(2) 程序很小,读写操作直接通过int 80h系统调用完成.栈溢出漏洞很明显,能溢出20字节.提示由于沙盒的保护只能来读取/home/warmup/flag文件.那么思路就很清楚了,打开 ...