blob: 4314303ddcad2bd29217ba61f5fb2632b3ef5729 (
plain)
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
|
<html>
<head>
<title>Apache Qpid : BewareOfStringPromotion</title>
<link rel="stylesheet" href="styles/site.css" type="text/css" />
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
<tr>
<td valign="top" class="pagebody">
<div class="pageheader">
<span class="pagetitle">
Apache Qpid : BewareOfStringPromotion
</span>
</div>
<div class="pagesubheading">
This page last changed on Oct 19, 2006 by <font color="#0050B2">mmccorma</font>.
</div>
<p><tt>std::string</tt> is a useful tool for simplifying memory management of strings and avoiding unnecessary copies by reference counting. However there is one common gotcha where it <em>causes</em> unnecessary copies. Consider: </p>
<div class="code"><div class="codeContent">
<pre class="code-java">void f(<span class="code-keyword">const</span> std::string& s) { cout << s << endl };
void g() {
<span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; i < 1000; ++i) { f(<span class="code-quote">"hello"</span>); };
}</pre>
</div></div>
<p>This actually allocates, copies and deletes 1000 heap buffers with the string "hello"! The problem here is that "hello" is <em>not</em> an instance of <tt>std::string</tt>. It is a char[5] that must be converted to a temporary <tt>std::string</tt> using the appropriate constructor. However <tt>std::string</tt> always wants to manage its own memory, so the constructor allocates a new buffer and copies the string. Once f() returns and we go round the loop again the temporary is deleted along with its buffer.</p>
<p>Here's a better solution:</p>
<div class="code"><div class="codeContent">
<pre class="code-java">void f(<span class="code-keyword">const</span> std::string& s) { cout << s << endl };
namespace { <span class="code-keyword">const</span> std::string hello(<span class="code-quote">"hello"</span>); }
void g() {
<span class="code-keyword">for</span> (<span class="code-object">int</span> i = 0; i < 1000; ++i) { f(hello); };
}</pre>
</div></div>
<p>This time we have a constant <tt>std::string</tt> that is created once at start up and destroyed once at shut-down. The anonymous namespace makes the constant private to this .cpp file so we wont have name clashes. (Its similar to using the static keyword on a global declaration in C, but anonymous namespaces are the preferred way to do it in modern C++) </p>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
</tr>
<tr>
<td align="center"><font color="grey">Document generated by Confluence on Apr 22, 2008 02:47</font></td>
</tr>
</table>
</body>
</html>
|