Changeset b766c9b7 for doc/theses
- Timestamp:
- Nov 11, 2024, 8:30:57 AM (4 days ago)
- Branches:
- master
- Children:
- 2ae845e9, 7e943e1
- Parents:
- c01a2fd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/string.tex
rc01a2fd rb766c9b7 25 25 \end{tabular} 26 26 \end{cquote} 27 The key commonality is that operations work on groups of characters for assigning. copying, scanning, and updating ,27 The key commonality is that operations work on groups of characters for assigning. copying, scanning, and updating. 28 28 Because a C string is null terminated and requires explicit storage management \see{\VRef{s:String}}, most of its group operations are error prone and expensive. 29 29 Most high-level string libraries use a separate length field and specialized storage management to support group operations. 30 \CC strings retain s null termination to allow them to interface with Clibrary functions requiring C strings.30 \CC strings retain null termination to interface with library functions requiring C strings. 31 31 \begin{cfa} 32 32 int open( const char * pathname, int flags ); 33 string fname{ " abc" );33 string fname{ "test.cc" ); 34 34 open( fname.@c_str()@ ); 35 35 \end{cfa} 36 The function @c_str@ does not create a new null-terminated C string of the \CC string, as that requires passing ownership of the C string to the caller for eventual deletion. 36 The function @c_str@ does not create a new null-terminated C string from the \CC string, as that requires passing ownership of the C string to the caller for eventual deletion.\footnote{ 37 C functions like \lstinline{strdup} do return allocated storage that must be freed by the caller.} 37 38 Instead, each \CC string is null terminator just in case it might be needed for this purpose. 38 Providing this backwards compatibility with C has a ubiquitous performance and storage cost even when this capability is not used.39 Providing this backwards compatibility with C has a ubiquitous performance and storage cost. 39 40 40 41 … … 42 43 43 44 This section discusses issues related to storage management of strings. 44 Specifically, it is common for strings to logically overlap completely or within a substring.45 Specifically, it is common for strings to logically overlap completely or partially. 45 46 \begin{cfa} 46 47 string s1 = "abcdef"; 47 48 string s2 = s1; $\C{// complete overlap, s2 == "abcdef"}$ 48 string s3 = s1.substr( 0, 3 ); $\C{// substringoverlap, s3 == "abc"}$49 string s3 = s1.substr( 0, 3 ); $\C{// partial overlap, s3 == "abc"}$ 49 50 \end{cfa} 50 51 This raises the question of how strings behave when an overlapping component is changed, … … 52 53 s3[1] = 'w'; $\C{// what happens to s1 and s2?}$ 53 54 \end{cfa} 54 \ie the notion of mutable andimmutable strings.55 For example, Java has immutable strings so stringsare copied when any overlapping string changes.56 Note, the notion of string mutability is not specified by @const@, \eg:55 This question is the notion of mutable or immutable strings. 56 For example, Java has immutable strings that are copied when any overlapping string changes. 57 Note, the notion of underlying string mutability is not specified by @const@, \eg: 57 58 \begin{cfa} 58 59 const string s1 = "abc"; 59 60 \end{cfa} 60 Here, @s1@ always points at @"abc"@, and @"abc"@ is an immutable constant, so all aspects of @s1@ are immutable. 61 Because a string is an implicit pointer, there is no mechanism to write: 62 \begin{cfa} 63 const string @* const@ s1; 64 \end{cfa} 65 Hence, the underlying string is mutable. 61 Here, @const@ applies to the @s1@ pointer to @"abc"@, and @"abc"@ is an immutable constant that is \emph{copied} into the string's storage. 62 Hence, @s1@ is not pointing at an immutable constant, meaning its underlying string is always mutable, unless some other designation is specified, such as Java's global rule. 66 63 67 64 68 65 \subsection{Logical overlap} 69 66 70 Rather than have strings be either mutable or immutable, it is possible to mark them on assignment.67 \CFA provides a dynamic mechanism to indicate mutable or immutable as an assignment attribute: @`shareEdits@. 71 68 72 69 \input{sharing-demo.tex}
Note: See TracChangeset
for help on using the changeset viewer.