forked from peter-can-write/cpp-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferent-string-implementations.html
More file actions
24 lines (24 loc) · 4.43 KB
/
different-string-implementations.html
File metadata and controls
24 lines (24 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><meta name="exporter-version" content="Evernote Mac 6.3 (452849)"/><meta name="altitude" content="519.0106811523438"/><meta name="author" content="petergoldsborough@hotmail.com"/><meta name="created" content="2015-08-06 13:08:07 +0000"/><meta name="latitude" content="46.62883328429765"/><meta name="longitude" content="13.8245059924025"/><meta name="source" content="desktop.mac"/><meta name="updated" content="2015-08-06 14:00:05 +0000"/><title>Different string implementations</title></head><body>
<div>String implementations between different libraries may differ in:</div>
<div><br/></div>
<ul>
<li>Whether or not they are reference-counted. Many implementations use reference counting to avoid unnecessary allocations for copied strings. This can be good if strings are often copied, but can be bad if they are not or in multi-threaded systems, as concurrency support has high overhead.</li>
<li>String objects may range in size form one to at least seven times the size of char* pointers, because they store more data, e.g. the size and capacity or even because they have a static fixed-size array to store small strings.</li>
<li>Creation of new strings may require zero (if put into a fixed-size array), one (if allocating only the character buffer) or two dynamic allocations (if allocating an implementation struct and then also the character buffer).</li>
<li>String objects may share information to different extents, i.e. some share their character buffers (via ref-counting), some share nothing because they do not use ref-counting or they may share all data including size and other members if they share pointers to implementation.</li>
<li>Some strings have a minimum allocation policy, i.e. even if the user requests to store only a small string of size 5, it will allocate memory for, e.g., 32 objects, for efficiency, to reduce the number of allocations.</li>
</ul>
<div><br/></div>
<div>E.g. on my system:</div>
<div><br/></div>
<div>#include <string></div>
<div>#inclue <iostream></div>
<div><br/></div>
<div><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">int</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">main(</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">int</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">argc,</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #e448ab">char</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">* argv[])<br/>
{<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #02a1f3">std</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">::</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #02a1f3">string</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">s;<br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #02a1f3">std</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">::</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #703daa">cout</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures"><< s.</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #3d1d81">size</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">() <<</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #43d34b">", "</span> <span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures"><< s.</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #3d1d81">capacity</span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures">();<br/>
<br/></span><span style="font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #848784">// Output: 0, 22</span></div>
<div><span style="font-size: 11px;"><span style="font-family: Menlo;">}</span></span></div>
</body></html>