summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/temporaries.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/temporaries.html
new repository
Diffstat (limited to 'devdocs/gcc~13/temporaries.html')
-rw-r--r--devdocs/gcc~13/temporaries.html20
1 files changed, 20 insertions, 0 deletions
diff --git a/devdocs/gcc~13/temporaries.html b/devdocs/gcc~13/temporaries.html
new file mode 100644
index 00000000..f933a394
--- /dev/null
+++ b/devdocs/gcc~13/temporaries.html
@@ -0,0 +1,20 @@
+<div class="subsection-level-extent" id="Temporaries"> <div class="nav-panel"> <p> Next: <a href="copy-assignment" accesskey="n" rel="next">Implicit Copy-Assignment for Virtual Bases</a>, Previous: <a href="name-lookup" accesskey="p" rel="prev">Name Lookup, Templates, and Accessing Members of Base Classes</a>, Up: <a href="c_002b_002b-misunderstandings" accesskey="u" rel="up">Common Misunderstandings with GNU C++</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="subsection" id="Temporaries-May-Vanish-Before-You-Expect"><span>14.7.3 Temporaries May Vanish Before You Expect<a class="copiable-link" href="#Temporaries-May-Vanish-Before-You-Expect"> ¶</a></span></h1> <p>It is dangerous to use pointers or references to <em class="emph">portions</em> of a temporary object. The compiler may very well delete the object before you expect it to, leaving a pointer to garbage. The most common place where this problem crops up is in classes like string classes, especially ones that define a conversion function to type <code class="code">char *</code> or <code class="code">const char *</code>—which is one reason why the standard <code class="code">string</code> class requires you to call the <code class="code">c_str</code> member function. However, any class that returns a pointer to some internal structure is potentially subject to this problem. </p> <p>For example, a program may use a function <code class="code">strfunc</code> that returns <code class="code">string</code> objects, and another function <code class="code">charfunc</code> that operates on pointers to <code class="code">char</code>: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">string strfunc ();
+void charfunc (const char *);
+
+void
+f ()
+{
+ const char *p = strfunc().c_str();
+ …
+ charfunc (p);
+ …
+ charfunc (p);
+}</pre>
+</div> <p>In this situation, it may seem reasonable to save a pointer to the C string returned by the <code class="code">c_str</code> member function and use that rather than call <code class="code">c_str</code> repeatedly. However, the temporary string created by the call to <code class="code">strfunc</code> is destroyed after <code class="code">p</code> is initialized, at which point <code class="code">p</code> is left pointing to freed memory. </p> <p>Code like this may run successfully under some other compilers, particularly obsolete cfront-based compilers that delete temporaries along with normal local variables. However, the GNU C++ behavior is standard-conforming, so if your program depends on late destruction of temporaries it is not portable. </p> <p>The safe way to write such code is to give the temporary a name, which forces it to remain until the end of the scope of the name. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">const string&amp; tmp = strfunc ();
+charfunc (tmp.c_str ());</pre>
+</div> </div> <div class="nav-panel"> <p> Next: <a href="copy-assignment">Implicit Copy-Assignment for Virtual Bases</a>, Previous: <a href="name-lookup">Name Lookup, Templates, and Accessing Members of Base Classes</a>, Up: <a href="c_002b_002b-misunderstandings">Common Misunderstandings with GNU C++</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
+ <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Temporaries.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Temporaries.html</a>
+ </p>
+</div>