summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/temporaries.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2025-08-14 22:58:58 -0500
committerCraig Jennings <c@cjennings.net>2025-08-14 22:58:58 -0500
commit82ba818ff456bcd6d56a06226e3f27e98fbb55c3 (patch)
tree158cfc17b2f644a10f063cb546752cfaae12c97f /devdocs/gcc~13/temporaries.html
parent9278ddd4ea1a8b1a4c1edaa8894516e3f48d245b (diff)
downloaddotemacs-82ba818ff456bcd6d56a06226e3f27e98fbb55c3.tar.gz
dotemacs-82ba818ff456bcd6d56a06226e3f27e98fbb55c3.zip
removing all downloaded devdocs files
Diffstat (limited to 'devdocs/gcc~13/temporaries.html')
-rw-r--r--devdocs/gcc~13/temporaries.html20
1 files changed, 0 insertions, 20 deletions
diff --git a/devdocs/gcc~13/temporaries.html b/devdocs/gcc~13/temporaries.html
deleted file mode 100644
index f933a394..00000000
--- a/devdocs/gcc~13/temporaries.html
+++ /dev/null
@@ -1,20 +0,0 @@
-<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>