1<html lang="en"> 2<head> 3<title>Temporaries - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings" title="C++ Misunderstandings"> 9<link rel="prev" href="Name-lookup.html#Name-lookup" title="Name lookup"> 10<link rel="next" href="Copy-Assignment.html#Copy-Assignment" title="Copy Assignment"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Temporaries"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Copy-Assignment.html#Copy-Assignment">Copy Assignment</a>, 53Previous: <a rel="previous" accesskey="p" href="Name-lookup.html#Name-lookup">Name lookup</a>, 54Up: <a rel="up" accesskey="u" href="C_002b_002b-Misunderstandings.html#C_002b_002b-Misunderstandings">C++ Misunderstandings</a> 55<hr> 56</div> 57 58<h4 class="subsection">11.8.3 Temporaries May Vanish Before You Expect</h4> 59 60<p><a name="index-temporaries_002c-lifetime-of-3336"></a><a name="index-portions-of-temporary-objects_002c-pointers-to-3337"></a>It is dangerous to use pointers or references to <em>portions</em> of a 61temporary object. The compiler may very well delete the object before 62you expect it to, leaving a pointer to garbage. The most common place 63where this problem crops up is in classes like string classes, 64especially ones that define a conversion function to type <code>char *</code> 65or <code>const char *</code>—which is one reason why the standard 66<code>string</code> class requires you to call the <code>c_str</code> member 67function. However, any class that returns a pointer to some internal 68structure is potentially subject to this problem. 69 70 <p>For example, a program may use a function <code>strfunc</code> that returns 71<code>string</code> objects, and another function <code>charfunc</code> that 72operates on pointers to <code>char</code>: 73 74<pre class="smallexample"> string strfunc (); 75 void charfunc (const char *); 76 77 void 78 f () 79 { 80 const char *p = strfunc().c_str(); 81 ... 82 charfunc (p); 83 ... 84 charfunc (p); 85 } 86</pre> 87 <p class="noindent">In this situation, it may seem reasonable to save a pointer to the C 88string returned by the <code>c_str</code> member function and use that rather 89than call <code>c_str</code> repeatedly. However, the temporary string 90created by the call to <code>strfunc</code> is destroyed after <code>p</code> is 91initialized, at which point <code>p</code> is left pointing to freed memory. 92 93 <p>Code like this may run successfully under some other compilers, 94particularly obsolete cfront-based compilers that delete temporaries 95along with normal local variables. However, the GNU C++ behavior is 96standard-conforming, so if your program depends on late destruction of 97temporaries it is not portable. 98 99 <p>The safe way to write such code is to give the temporary a name, which 100forces it to remain until the end of the scope of the name. For 101example: 102 103<pre class="smallexample"> const string& tmp = strfunc (); 104 charfunc (tmp.c_str ()); 105</pre> 106 </body></html> 107 108