source: doc/theses/mike_brooks_MMath/background.tex @ 174a11a

Last change on this file since 174a11a was 7d95bce9, checked in by Michael Brooks <mlbrooks@…>, 10 days ago

Thesis, background, array: clarify C laxed inner dimension checking

  • Property mode set to 100644
File size: 68.6 KB
RevLine 
[27f1055]1\chapter{Background}
2
[0554c1a]3Since this work builds on C, it is necessary to explain the C mechanisms and their shortcomings for array, linked list, and string.
[27f1055]4
[40ab446]5
[43d9679]6\section{Ill-typed expressions}
7
8C reports many ill-typed expressions as warnings.
9For example, these attempts to assign @y@ to @x@ and vice-versa are obviously ill-typed.
10\lstinput{12-15}{bkgd-c-tyerr.c}
11with warnings:
12\begin{cfa}
13warning: assignment to 'float *' from incompatible pointer type 'void (*)(void)'
14warning: assignment to 'void (*)(void)' from incompatible pointer type 'float *'
15\end{cfa}
16Similarly,
17\lstinput{17-19}{bkgd-c-tyerr.c}
18with warning:
19\begin{cfa}
20warning: passing argument 1 of 'f' from incompatible pointer type
21note: expected 'void (*)(void)' but argument is of type 'float *'
22\end{cfa}
23with a segmentation fault at runtime.
24Clearly, @gcc@ understands these ill-typed case, and yet allows the program to compile, which seems inappropriate.
[63cf80e]25Compiling with flag @-Werror@, which turns warnings into errors, is often too pervasive, because some warnings are just warnings, \eg an unused variable.
[43d9679]26In the following discussion, ``ill-typed'' means giving a nonzero @gcc@ exit condition with a message that discusses typing.
27Note, \CFA's type-system rejects all these ill-typed cases as type mismatch errors.
28
29% That @f@'s attempt to call @g@ fails is not due to 3.14 being a particularly unlucky choice of value to put in the variable @pi@.
30% Rather, it is because obtaining a program that includes this essential fragment, yet exhibits a behaviour other than "doomed to crash," is a matter for an obfuscated coding competition.
31
32% A "tractable syntactic method for proving the absence of certain program behaviours by classifying phrases according to the kinds of values they compute"*1 rejected the program.
33% The behaviour (whose absence is unprovable) is neither minor nor unlikely.
34% The rejection shows that the program is ill-typed.
35%
36% Yet, the rejection presents as a GCC warning.
37% *1  TAPL-pg1 definition of a type system
38
39
[63cf80e]40\section{Reading declarations}
[266732e]41
[63cf80e]42A significant area of confusion for reading C declarations results from:
[40ab446]43\begin{itemize}
[63cf80e]44\item
45C is unique in having dimension be higher priority than pointer in declarations.\footnote{
46For consistency, subscript has higher priority than dereference, yielding \lstinline{(*arp)[3]} rather than \lstinline{*arp[3]}.}
47\item
48Embedding a declared variable in a declaration, mimics the way the variable is used in executable statements.
[40ab446]49\end{itemize}
[dd37afa]50\begin{cquote}
51\begin{tabular}{@{}ll@{}}
[0554c1a]52\multicolumn{1}{@{}c}{\textbf{Array}} & \multicolumn{1}{c@{}}{\textbf{Function Pointer}} \\
[266732e]53\begin{cfa}
[dd37afa]54int @(*@ar@)[@5@]@; // definition
55  ... @(*@ar@)[@3@]@ += 1; // usage
[266732e]56\end{cfa}
[dd37afa]57&
[d3a49864]58\begin{cfa}
[dd37afa]59int @(*@f@())[@5@]@ { ... }; // definition
60  ... @(*@f@())[@3@]@ += 1; // usage
[d3a49864]61\end{cfa}
[dd37afa]62\end{tabular}
63\end{cquote}
[63cf80e]64The parenthesis are necessary to achieve a pointer to a @T@, and the type is wrapped around the name in successive layers (like an \Index{onion}) to match usage in an expression.
[d3a49864]65While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice, even though Dennis Richie believed otherwise:
66\begin{quote}
67In spite of its difficulties, I believe that the C's approach to declarations remains plausible, and am comfortable with it; it is a useful unifying principle.~\cite[p.~12]{Ritchie93}
68\end{quote}
[dd37afa]69After all, reading a C array type is easy: just read it from the inside out, and know when to look left and when to look right!
[d3a49864]70
[0554c1a]71\CFA provides its own type, variable and routine declarations, using a simpler syntax.
[d3a49864]72The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type.
[c721105]73The qualifiers have the same syntax and semantics in \CFA as in C.
[0554c1a]74Then, a \CFA declaration is read left to right, where a function return type is enclosed in brackets @[@\,@]@.
[d3a49864]75\begin{cquote}
[dd37afa]76\begin{tabular}{@{}l@{\hspace{3em}}ll@{}}
[0554c1a]77\multicolumn{1}{c@{\hspace{3em}}}{\textbf{C}}   & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{read left to right}}   \\
[dd37afa]78\begin{cfa}
79int @*@ x1 @[5]@;
80int @(*@x2@)[5]@;
81int @(*@f( int p )@)[5]@;
82\end{cfa}
83&
84\begin{cfa}
85@[5] *@ int x1;
86@* [5]@ int x2;
87@[ * [5] int ]@ f( int p );
[d3a49864]88\end{cfa}
89&
[dd37afa]90\begin{cfa}
91// array of 5 pointers to int
92// pointer to array of 5 int
93// function returning pointer to array of 5 ints
[d3a49864]94\end{cfa}
[dd37afa]95\\
96& &
97\LstCommentStyle{//\ \ \ and taking an int argument}
[d3a49864]98\end{tabular}
99\end{cquote}
[c721105]100As declaration size increases, it becomes corresponding difficult to read and understand the C declaration form, whereas reading and understanding a \CFA declaration has linear complexity as the declaration size increases.
[63cf80e]101Note, writing declarations left to right is common in other programming languages, where the function return-type is often placed after the parameter declarations, \eg \CC \lstinline[language=C++]{auto f( int ) -> int}.
102Unfortunately, \CFA cannot interchange the priorities of subscript and dereference in expressions without breaking C compatibility.
[d3a49864]103
[c721105]104\VRef[Table]{bkgd:ar:usr:avp} introduces the many layers of the C and \CFA array story, where the \CFA story is discussion in \VRef[Chapter]{c:Array}.
[63cf80e]105The \CFA-thesis column shows the new array declaration form, which is my contribution to safety and ergonomics.
[dd37afa]106The table shows there are multiple yet equivalent forms for the array types under discussion, and subsequent discussion shows interactions with orthogonal (but easily confused) language features.
107Each row of the table shows alternate syntactic forms.
108The simplest occurrences of types distinguished in the preceding discussion are marked with $\triangleright$.
[63cf80e]109Removing the declared variable @x@, gives the type used for variable, structure field, cast, or error messages.
110Unfortunately, parameter declarations have more syntactic forms and rules.
[40ab446]111
[dd37afa]112\begin{table}
[266732e]113\centering
[0554c1a]114\caption{Syntactic Reference for Array vs Pointer. Includes interaction with \lstinline{const}ness.}
[dd37afa]115\label{bkgd:ar:usr:avp}
116\begin{tabular}{ll|l|l|l}
117        & Description & \multicolumn{1}{c|}{C} & \multicolumn{1}{c|}{\CFA}  & \multicolumn{1}{c}{\CFA-thesis} \\
118        \hline
[0554c1a]119$\triangleright$ & value & @T x;@ & @T x;@ & \\
[d3a49864]120        \hline
[dd37afa]121        & immutable value & @const T x;@ & @const T x;@ & \\
122        & & @T const x;@ & @T const x;@ & \\
[d3a49864]123        \hline \hline
[0554c1a]124$\triangleright$ & pointer to value & @T * x;@ & @* T x;@ & \\
[d3a49864]125        \hline
[dd37afa]126        & immutable ptr. to val. & @T * const x;@ & @const * T x;@ & \\
[d3a49864]127        \hline
[dd37afa]128        & ptr. to immutable val. & @const T * x;@ & @* const T x;@ & \\
129        & & @T const * x;@ & @* T const x;@ & \\
[d3a49864]130        \hline \hline
[0554c1a]131$\triangleright$ & array of value & @T x[10];@ & @[10] T x@ & @array(T, 10) x@ \\
[d3a49864]132        \hline
[dd37afa]133        & ar.\ of immutable val. & @const T x[10];@ & @[10] const T x@ & @const array(T, 10) x@ \\
[c721105]134        & & @T const x[10];@ & @[10] T const x@ & @array(T, 10) const x@ \\
[d3a49864]135        \hline
[dd37afa]136        & ar.\ of ptr.\ to value & @T * x[10];@ & @[10] * T x@ & @array(T *, 10) x@ \\
137        & & & & @array(* T, 10) x@ \\
[d3a49864]138        \hline
[dd37afa]139        & ar.\ of imm. ptr.\ to val. & @T * const x[10];@ & @[10] const * T x@ & @array(* const T, 10) x@ \\
140        & & & & @array(const * T, 10) x@ \\
[d3a49864]141        \hline
[dd37afa]142        & ar.\ of ptr.\ to imm. val. & @const T * x[10];@ & @[10] * const T x@ & @array(const T *, 10) x@ \\
143        & & @T const * x[10];@ & @[10] * T const x@ & @array(* const T, 10) x@ \\
[d3a49864]144        \hline \hline
[0554c1a]145$\triangleright$ & ptr.\ to ar.\ of value & @T (*x)[10];@ & @* [10] T x@ & @* array(T, 10) x@ \\
[d3a49864]146        \hline
[dd37afa]147        & imm. ptr.\ to ar.\ of val. & @T (* const x)[10];@ & @const * [10] T x@ & @const * array(T, 10) x@ \\
[d3a49864]148        \hline
[dd37afa]149        & ptr.\ to ar.\ of imm. val. & @const T (*x)[10];@ & @* [10] const T x@ & @* const array(T, 10) x@ \\
150        & & @T const (*x)[10];@ & @* [10] T const x@ & @* array(T, 10) const x@ \\
[d3a49864]151        \hline
[dd37afa]152        & ptr.\ to ar.\ of ptr.\ to val. & @T *(*x)[10];@ & @* [10] * T x@ & @* array(T *, 10) x@ \\
153        & & & & @* array(* T, 10) x@ \\
[d3a49864]154        \hline
[40ab446]155\end{tabular}
[dd37afa]156\end{table}
[2d82999]157
[63cf80e]158
159\section{Array}
160\label{s:Array}
161
162At the start, the C language designers made a significant design mistake with respect to arrays.
163\begin{quote}
164In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays really should be treated simultaneously.
165Any operation which can be achieved by array subscripting can also be done with pointers.~\cite[p.~93]{C:old}
166\end{quote}
167Accessing any storage requires pointer arithmetic, even if it is just base-displacement addressing in an instruction.
168The conjoining of pointers and arrays could also be applied to structures, where a pointer references a structure field like an array element.
169Finally, while subscripting involves pointer arithmetic (as does a field reference @x.y.z@), the computation is complex for multi-dimensional arrays and requires array descriptors to know stride lengths along dimensions.
[ad9f593]170Many C errors result from manually performing pointer arithmetic instead of using language subscripting, letting the compiler perform any arithmetic.
171
172Some C textbooks erroneously suggest manual pointer arithmetic is faster than subscripting.
[63cf80e]173A sound and efficient C program does not require explicit pointer arithmetic.
[ad9f593]174TODO: provide an example, explain the belief, and give modern refutation
[63cf80e]175
[ad9f593]176C semantics wants a programmer to \emph{believe} an array variable is a ``pointer to its first element.''
[63cf80e]177This desire becomes apparent by a detailed inspection of an array declaration.
178\lstinput{34-34}{bkgd-carray-arrty.c}
179The inspection begins by using @sizeof@ to provide program semantics for the intuition of an expression's type.
[c3e41cda]180An architecture with 64-bit pointer size is used, to remove irrelevant details.
[63cf80e]181\lstinput{35-36}{bkgd-carray-arrty.c}
182Now consider the @sizeof@ expressions derived from @ar@, modified by adding pointer-to and first-element (and including unnecessary parentheses to avoid any confusion about precedence).
183\lstinput{37-40}{bkgd-carray-arrty.c}
184Given that arrays are contiguous and the size of @float@ is 4, then the size of @ar@ with 10 floats being 40 bytes is common reasoning for C programmers.
[ad9f593]185Equally, C programmers know the size of a pointer to the first array element is 8.
[63cf80e]186% Now, set aside for a moment the claim that this first assertion is giving information about a type.
187Clearly, an array and a pointer to its first element are different.
188
[ad9f593]189In fact, the idea that there is such a thing as a pointer to an array may be surprising.
190It it is not the same thing as a pointer to the first element.
[63cf80e]191\lstinput{42-45}{bkgd-carray-arrty.c}
192The first assignment generates:
193\begin{cfa}
194warning: assignment to `float (*)[10]' from incompatible pointer type `float *'
195\end{cfa}
196and the second assignment generates the opposite.
197
198The inspection now refutes any suggestion that @sizeof@ is informing about allocation rather than type information.
199Note, @sizeof@ has two forms, one operating on an expression and the other on a type.
200Using the type form yields the same results as the prior expression form.
201\lstinput{46-49}{bkgd-carray-arrty.c}
[ad9f593]202The results are also the same when there is no allocation at all.
203This time, starting from a pointer-to-array type:
[63cf80e]204\lstinput{51-57}{bkgd-carray-arrty.c}
[ad9f593]205Hence, in all cases, @sizeof@ is reporting on type information.
[63cf80e]206
207Therefore, thinking of an array as a pointer to its first element is too simplistic an analogue and it is not backed up by the type system.
208This misguided analogue works for a single-dimension array but there is no advantage other than possibly teaching beginner programmers about basic runtime array-access.
209
210Continuing, there is a short form for declaring array variables using length information provided implicitly by an initializer.
211\lstinput{59-62}{bkgd-carray-arrty.c}
212The compiler counts the number of initializer elements and uses this value as the first dimension.
213Unfortunately, the implicit element counting does not extend to dimensions beyond the first.
214\lstinput{64-67}{bkgd-carray-arrty.c}
215
216My observation is recognizing:
[c3e41cda]217\begin{itemize}[leftmargin=*,itemsep=0pt]
[63cf80e]218        \item There is value in using a type that knows its size.
219        \item The type pointer to the (first) element does not.
220        \item C \emph{has} a type that knows the whole picture: array, \eg @T[10]@.
221        \item This type has all the usual derived forms, which also know the whole picture.
222        A noteworthy example is pointer to array, \eg @T (*)[10]@.
[2d82999]223\end{itemize}
224
225
[40ab446]226\subsection{Arrays decay and pointers diffract}
227
[63cf80e]228The last section established the difference among these four types:
[266732e]229\lstinput{3-6}{bkgd-carray-decay.c}
230But the expression used for obtaining the pointer to the first element is pedantic.
231The root of all C programmer experience with arrays is the shortcut
232\lstinput{8-8}{bkgd-carray-decay.c}
233which reproduces @pa0@, in type and value:
234\lstinput{9-9}{bkgd-carray-decay.c}
[63cf80e]235The validity of this initialization is unsettling, in the context of the facts established in \VRef{s:Array}.
[b5bfb16]236Notably, it initializes name @pa0x@ from expression @ar@, when they are not of the same type:
[266732e]237\lstinput{10-10}{bkgd-carray-decay.c}
[40ab446]238
[0554c1a]239So, C provides an implicit conversion from @float[10]@ to @float *@.
[40ab446]240\begin{quote}
[0554c1a]241Except when it is the operand of the @sizeof@ operator, or the unary @&@ operator, or is a string literal used to
242initialize an array an expression that has type ``array of \emph{type}'' is converted to an expression with type
243``pointer to \emph{type}'' that points to the initial element of the array object~\cite[\S~6.3.2.1.3]{C11}
[40ab446]244\end{quote}
[c721105]245This phenomenon is the famous \newterm{pointer decay}, which is a decay of an array-typed expression into a pointer-typed one.
[63cf80e]246It is worthy to note that the list of exceptional cases does not feature the occurrence of @ar@ in @ar[i]@.
[0554c1a]247Thus, subscripting happens on pointers not arrays.
[40ab446]248
[0554c1a]249Subscripting proceeds first with pointer decay, if needed.  Next, \cite[\S~6.5.2.1.2]{C11} explains that @ar[i]@ is treated as if it were @(*((a)+(i)))@.
250\cite[\S~6.5.6.8]{C11} explains that the addition, of a pointer with an integer type,  is defined only when the pointer refers to an element that is in an array, with a meaning of ``@i@ elements away from,'' which is valid if @ar@ is big enough and @i@ is small enough.
251Finally, \cite[\S~6.5.3.2.4]{C11} explains that the @*@ operator's result is the referenced element.
252Taken together, these rules illustrate that @ar[i]@ and @i[a]@ mean the same thing!
[40ab446]253
[63cf80e]254Subscripting a pointer when the target is standard-inappropriate is still practically well-defined.
[c721105]255While the standard affords a C compiler freedom about the meaning of an out-of-bound access, or of subscripting a pointer that does not refer to an array element at all,
256the fact that C is famously both generally high-performance, and specifically not bound-checked, leads to an expectation that the runtime handling is uniform across legal and illegal accesses.
[b5bfb16]257Moreover, consider the common pattern of subscripting on a @malloc@ result:
[266732e]258\begin{cfa}
259float * fs = malloc( 10 * sizeof(float) );
260fs[5] = 3.14;
261\end{cfa}
[0554c1a]262The @malloc@ behaviour is specified as returning a pointer to ``space for an object whose size is'' as requested (\cite[\S~7.22.3.4.2]{C11}).
263But \emph{nothing} more is said about this pointer value, specifically that its referent might \emph{be} an array allowing subscripting.
[40ab446]264
[0554c1a]265Under this assumption, a pointer being subscripted (or added to, then dereferenced) by any value (positive, zero, or negative), gives a view of the program's entire address space, centred around the @p@ address, divided into adjacent @sizeof(*p)@ chunks, each potentially (re)interpreted as @typeof(*p)@.
[c721105]266I call this phenomenon \emph{array diffraction}, which is a diffraction of a single-element pointer into the assumption that its target is in the middle of an array whose size is unlimited in both directions.
[40ab446]267No pointer is exempt from array diffraction.
268No array shows its elements without pointer decay.
269
270A further pointer--array confusion, closely related to decay, occurs in parameter declarations.
[0554c1a]271\cite[\S~6.7.6.3.7]{C11} explains that when an array type is written for a parameter,
272the parameter's type becomes a type that can be summarized as the array-decayed type.
[63cf80e]273The respective handling of the following two parameter spellings shows that the array and pointer versions are identical.
[266732e]274\lstinput{12-16}{bkgd-carray-decay.c}
[b5bfb16]275As the @sizeof(x)@ meaning changed, compared with when run on a similarly-spelled local variable declaration,
[c4024b46]276@gcc@ also gives this code the warning for the first assertion:
[0554c1a]277\begin{cfa}
278warning: 'sizeof' on array function parameter 'x' will return size of 'float *'
279\end{cfa}
280The caller of such a function is left with the reality that a pointer parameter is a pointer, no matter how it is spelled:
[266732e]281\lstinput{18-21}{bkgd-carray-decay.c}
[c721105]282This fragment gives the warning for the first argument, in the second call.
283\begin{cfa}
284warning: 'f' accessing 40 bytes in a region of size 4
285\end{cfa}
[40ab446]286
287The shortened parameter syntax @T x[]@ is a further way to spell ``pointer.''
288Note the opposite meaning of this spelling now, compared with its use in local variable declarations.
289This point of confusion is illustrated in:
[266732e]290\lstinput{23-30}{bkgd-carray-decay.c}
[c721105]291Note, \CC gives a warning for the initialization of @cp@.
292\begin{cfa}
293warning: ISO C++ forbids converting a string constant to 'char*'
294\end{cfa}
295and C gives a warning at the call of @edit@, if @const@ is added to the declaration of @cp@.
296\begin{cfa}
297warning: passing argument 1 of 'edit' discards 'const' qualifier from pointer target type
298\end{cfa}
[63cf80e]299The basic two meanings, with a syntactic difference helping to distinguish, are illustrated in the declarations of @ca@ \vs @cp@, whose subsequent @edit@ calls behave differently.
300The syntax-caused confusion is in the comparison of the first and last lines, both of which use a literal to initialize an object declared with spelling @T x[]@.
301But these initialized declarations get opposite meanings, depending on whether the object is a local variable or a parameter!
[40ab446]302
[b5bfb16]303In summary, when a function is written with an array-typed parameter,
[c3e41cda]304\begin{itemize}[leftmargin=*]
[63cf80e]305        \item an appearance of passing an array by value is always an incorrect understanding,
306        \item a dimension value, if any is present, is ignored,
307        \item pointer decay is forced at the call site and the callee sees the parameter having the decayed type.
[40ab446]308\end{itemize}
309
310Pointer decay does not affect pointer-to-array types, because these are already pointers, not arrays.
311As a result, a function with a pointer-to-array parameter sees the parameter exactly as the caller does:
[63cf80e]312\par\noindent
313\begin{tabular}{@{\hspace*{-0.75\parindentlnth}}l@{}l@{}}
314\lstinput{32-36}{bkgd-carray-decay.c}
315&
316\lstinput{38-42}{bkgd-carray-decay.c}
317\end{tabular}
318\par\noindent
[0554c1a]319\VRef[Table]{bkgd:ar:usr:decay-parm} gives the reference for the decay phenomenon seen in parameter declarations.
[40ab446]320
[0554c1a]321\begin{table}
322\caption{Syntactic Reference for Decay during Parameter-Passing.
323Includes interaction with \lstinline{const}ness, where ``immutable'' refers to a restriction on the callee's ability.}
324\label{bkgd:ar:usr:decay-parm}
[266732e]325\centering
[40ab446]326\begin{tabular}{llllll}
[0554c1a]327        & Description & Type & Parameter Declaration & \CFA  \\
328        \hline
329        & & & @T * x,@ & @* T x,@ \\
330$\triangleright$ & pointer to value & @T *@ & @T x[10],@ & @[10] T x,@ \\
331        & & & @T x[],@ & @[] T x,@ \\
332        \hline
333        & & & @T * const x,@ & @const * T x@ \\
334        & immutable ptr.\ to val. & @T * const@ & @T x[const 10],@ & @[const 10] T x,@ \\
335        & & & @T x[const],@ & @[const] T x,@\\
336        \hline
337        & & & @const T * x,@ & @ * const T x,@ \\
338        & &     & @T const * x,@ & @ * T const x,@ \\
339        & ptr.\ to immutable val. & @const T *@ & @const T x[10],@ & @[10] const T x,@ \\
340        & & @T const *@ & @T const x[10],@ &  @[10] T const x,@ \\
341        & & & @const T x[],@ & @[] const T x,@ \\
342        & & & @T const x[],@ & @[] T const x,@ \\
343        \hline \hline
344        & & & @T (*x)[10],@ & @* [10] T x,@ \\
345$\triangleright$ & ptr.\ to ar.\ of val. & @T(*)[10]@ & @T x[3][10],@ & @[3][10] T x,@ \\
346        & & & @T x[][10],@ & @[][10] T x,@ \\
347        \hline
348        & & & @T ** x,@ & @** T x,@ \\
349        & ptr.\ to ptr.\ to val. & @T **@ & @T * x[10],@ & @[10] * T x,@ \\
350        & & & @T * x[],@ & @[] * T x,@ \\
351        \hline
352        & ptr.\ to ptr.\ to imm.\ val. & @const char **@ & @const char * argv[],@ & @[] * const char argv,@ \\
[c721105]353        & & & \emph{others elided} & \emph{others elided} \\
[0554c1a]354        \hline
[40ab446]355\end{tabular}
[0554c1a]356\end{table}
[40ab446]357
358
[63cf80e]359\subsection{Variable Length Arrays}
[c721105]360
[63cf80e]361As of C99, the C standard supports a \newterm{variable length array} (VLA)~\cite[\S~6.7.5.2.5]{C99}, providing a dynamic-fixed array feature \see{\VRef{s:ArrayIntro}}.
362Note, the \CC standard does not support VLAs, but @g++@ provides them.
363A VLA is used when the desired number of array elements is \emph{unknown} at compile time.
[266732e]364\begin{cfa}
[63cf80e]365size_t  cols;
366scanf( "%d", &cols );
367double ar[cols];
[266732e]368\end{cfa}
[63cf80e]369The array dimension is read from outside the program and used to create an array of size @cols@ on the stack.
370The VLA is implemented by the @alloca@ routine, which bumps the stack pointer.
371Unfortunately, there is significant misinformation about VLAs, \eg the stack size is limited (small), or VLAs cause stack failures or are inefficient.
[0554c1a]372VLAs exist as far back as Algol W~\cite[\S~5.2]{AlgolW} and are a sound and efficient data type.
[63cf80e]373For types with a dynamic-fixed stack, \eg coroutines or user-level threads, large VLAs can overflow the stack without appropriately sizing the stack, so heap allocation is used when the array size is unbounded.
[0554c1a]374
[40ab446]375
[63cf80e]376\subsection{Multidimensional Arrays}
377\label{toc:mdimpl}
[40ab446]378
[63cf80e]379% TODO: introduce multidimensional array feature and approaches
[40ab446]380
[63cf80e]381When working with arrays, \eg linear algebra, array dimensions are referred to as ``rows'' and ``columns'' for a matrix, adding ``planes'' for a cube.
382(There is little terminology for higher dimensional arrays.)
383For example, an acrostic poem\footnote{A type of poetry where the first, last or other letters in a line spell out a particular word or phrase in a vertical column.}
384can be treated as a grid of characters, where the rows are the text and the columns are the embedded keyword(s).
385Within a poem, there is the concept of a \newterm{slice}, \eg a row is a slice for the poem text, a column is a slice for a keyword.
386In general, the dimensioning and subscripting for multidimensional arrays has two syntactic forms: @m[r,c]@ or @m[r][c]@.
[40ab446]387
[63cf80e]388Commonly, an array, matrix, or cube, is visualized (especially in mathematics) as a contiguous row, rectangle, or block.
389This conceptualization is reenforced by subscript ordering, \eg $m_{r,c}$ for a matrix and $c_{p,r,c}$ for a cube.
390Few programming languages differ from the mathematical subscript ordering.
391However, computer memory is flat, and hence, array forms are structured in memory as appropriate for the runtime system.
392The closest representation to the conceptual visualization is for an array object to be contiguous, and the language structures this memory using pointer arithmetic to access the values using various subscripts.
393This approach still has degrees of layout freedom, such as row or column major order, \ie juxtaposed rows or columns in memory, even when the subscript order remains fixed.
394For example, programming languages like MATLAB, Fortran, Julia and R store matrices in column-major order since they are commonly used for processing column-vectors in tabular data sets but retain row-major subscripting to match with mathematical notation.
395In general, storage layout is hidden by subscripting, and only appears when passing arrays among different programming languages or accessing specific hardware.
[40ab446]396
[63cf80e]397\VRef[Figure]{f:FixedVariable} shows two C90 approaches for manipulating a contiguous matrix.
398Note, C90 does not support VLAs.
399The fixed-dimension approach (left) uses the type system;
400however, it requires all dimensions except the first to be specified at compile time, \eg @m[][6]@, allowing all subscripting stride calculations to be generated with constants.
401Hence, every matrix passed to @fp1@ must have exactly 6 columns but the row size can vary.
402The variable-dimension approach (right) ignores (violates) the type system, \ie argument and parameters types do not match, and subscripting is performed manually using pointer arithmetic in the macro @sub@.
[40ab446]403
[63cf80e]404\begin{figure}
405\begin{tabular}{@{}l@{\hspace{40pt}}l@{}}
406\multicolumn{1}{c}{\textbf{Fixed Dimension}} & \multicolumn{1}{c}{\textbf{Variable Dimension}} \\
407\begin{cfa}
[40ab446]408
[63cf80e]409void fp1( int rows, int m[][@6@] ) {
410        ...  printf( "%d ", @m[r][c]@ );  ...
411}
412int fm1[4][@6@], fm2[6][@6@]; // no VLA
413// initialize matrixes
414fp1( 4, fm1 ); // implicit 6 columns
415fp1( 6, fm2 );
416\end{cfa}
417&
418\begin{cfa}
419#define sub( m, r, c ) *(m + r * sizeof( m[0] ) + c)
420void fp2( int rows, int cols, int *m ) {
421        ...  printf( "%d ", @sub( m, r, c )@ );  ...
422}
423int vm1[@4@][@4@], vm2[@6@][@8@]; // no VLA
424// initialize matrixes
425fp2( 4, 4, vm1 );
426fp2( 6, 8, vm2 );
427\end{cfa}
428\end{tabular}
429\caption{C90 Fixed \vs Variable Contiguous Matrix Styles}
430\label{f:FixedVariable}
431\end{figure}
[40ab446]432
[63cf80e]433Many languages allow multidimensional arrays-of-arrays, \eg in Pascal or \CC.
434\begin{cquote}
435\begin{tabular}{@{}ll@{}}
436\begin{pascal}
437var m : array[0..4, 0..4] of Integer;  (* matrix *)
438type AT = array[0..4] of Integer;  (* array type *)
439type MT = array[0..4] of AT;  (* array of array type *)
440var aa : MT;  (* array of array variable *)
441m@[1][2]@ := 1;   aa@[1][2]@ := 1 (* same subscripting *)
442\end{pascal}
443&
444\begin{c++}
445int m[5][5];
[40ab446]446
[63cf80e]447typedef vector< vector<int> > MT;
448MT vm( 5, vector<int>( 5 ) );
449m@[1][2]@ = 1;  aa@[1][2]@ = 1;
450\end{c++}
451\end{tabular}
452\end{cquote}
453The language decides if the matrix and array-of-array are laid out the same or differently.
454For example, an array-of-array may be an array of row pointers to arrays of columns, so the rows may not be contiguous in memory nor even the same length (triangular matrix).
455Regardless, there is usually a uniform subscripting syntax masking the memory layout, even though a language could differentiated between the two forms using subscript syntax, \eg @m[1,2]@ \vs @aa[1][2]@.
456Nevertheless, controlling memory layout can make a difference in what operations are allowed and in performance (caching/NUMA effects).
[40ab446]457
[63cf80e]458C also provides non-contiguous arrays-of-arrays.
459\begin{cfa}
460int m[5][5];                                                    $\C{// contiguous}$
461int * aa[5];                                                    $\C{// non-contiguous}$
462\end{cfa}
463both with different memory layout using the same subscripting, and both with different degrees of issues.
464The focus of this work is on the contiguous multidimensional arrays in C.
465The reason is that programmers are often forced to use the more complex array-of-array form when a contiguous array would be simpler, faster, and safer.
466Nevertheless, the C array-of-array form is still important for special circumstances.
467
468\VRef[Figure]{f:ContiguousNon-contiguous} shows a powerful extension made in C99 for manipulating contiguous \vs non-contiguous arrays.\footnote{C90 also supported non-contiguous arrays.}
469For contiguous-array (including VLA) arguments, C99 conjoins one or more of the parameters as a downstream dimension(s), \eg @cols@, implicitly using this parameter to compute the row stride of @m@.
470There is now sufficient information to support subscript checking along the columns to prevent buffer-overflow problems, but subscript checking is not provided.
471If the declaration of @fc@ is changed to:
472\begin{cfa}
473void fc( int rows, int cols, int m[@rows@][@cols@] ) ...
474\end{cfa}
475it is possible for C to perform bound checking across all subscripting.
476While this contiguous-array capability is a step forward, it is still the programmer's responsibility to manually manage the number of dimensions and their sizes, both at the function definition and call sites.
477That is, the array does not automatically carry its structure and sizes for use in computing subscripts.
478While the non-contiguous style in @faa@ looks very similar to @fc@, the compiler only understands the unknown-sized array of row pointers, and it relies on the programmer to traverse the columns in a row correctly with a correctly bounded loop index.
479Specifically, there is no requirement that the rows are the same length, like a poem with different length lines.
[40ab446]480
[63cf80e]481\begin{figure}
482\begin{tabular}{@{}ll@{}}
483\multicolumn{1}{c}{\textbf{Contiguous}} & \multicolumn{1}{c}{\textbf{ Non-contiguous}} \\
484\begin{cfa}
485void fc( int rows, @int cols@, int m[ /* rows */ ][@cols@] ) {
486        for ( size_t  r = 0; r < rows; r += 1 ) {
487                for ( size_t  c = 0; c < cols; c += 1 )
488                        ...  @m[r][c]@  ...
489}
490int m@[5][5]@;
491for ( int r = 0; r < 5; r += 1 ) {
[40ab446]492
[63cf80e]493        for ( int c = 0; c < 5; c += 1 )
494                m[r][c] = r + c;
495}
496fc( 5, 5, m );
497\end{cfa}
498&
499\begin{cfa}
500void faa( int rows, int cols, int * m[ @/* cols */@ ] ) {
501        for ( size_t  r = 0; r < rows; r += 1 ) {
502                for ( size_t  c = 0; c < cols; c += 1 )
503                        ...  @m[r][c]@  ...
504}
505int @* aa[5]@;  // row pointers
506for ( int r = 0; r < 5; r += 1 ) {
507        @aa[r] = malloc( 5 * sizeof(int) );@ // create rows
508        for ( int c = 0; c < 5; c += 1 )
509                aa[r][c] = r + c;
510}
511faa( 5, 5, aa );
512\end{cfa}
513\end{tabular}
514\caption{C99 Contiguous \vs Non-contiguous Matrix Styles}
515\label{f:ContiguousNon-contiguous}
516\end{figure}
[40ab446]517
518
[63cf80e]519\subsection{Multi-dimensional arrays decay and pointers diffract}
[40ab446]520
[63cf80e]521As for single-dimension arrays, multi-dimensional arrays have similar issues.
522\lstinput{16-18}{bkgd-carray-mdim.c}
523The significant axis of deriving expressions from @ar@ is now ``itself,'' ``first element'' or ``first grand-element (meaning, first element of first element).''
524\PAB{Explain, explain, explain.}
525\lstinput{20-26}{bkgd-carray-mdim.c}
526\PAB{Explain, explain, explain.}
527\lstinput{28-36}{bkgd-carray-mdim.c}
528\PAB{Explain, explain, explain.}
529\lstinput{38-44}{bkgd-carray-mdim.c}
[40ab446]530
531
[63cf80e]532\subsection{Array Parameter Declaration}
[40ab446]533
[c3e41cda]534Passing an array as an argument to a function is necessary.
535Assume a parameter is an array when the function intends to subscript it.
536This section asserts that a more satisfactory/formal characterization does not exist in C, surveys the ways that C API authors communicate ``@p@ has zero or more dimensions'' and calls out the minority cases where the C type system is using or verifying such claims.
[ad9f593]537
[c3e41cda]538A C parameter declarations look different, from the caller's and callee's perspectives.
[ad9f593]539Both perspectives consist of the text read by a programmer and the semantics enforced by the type system.
[c3e41cda]540The caller's perspective is available from a function declaration, which allow definition-before-use and separate compilation, but can also be read from (the non-body part of) a function definition.
[ad9f593]541The callee's perspective is what is available inside the function.
542\begin{cfa}
[c3e41cda]543int foo( int, float, char );                            $\C{// declaration, names optional}$
544int bar( int i, float f, char c ) {             $\C{// definition, names mandatory}$
545        // caller's perspective of foo; callee's perspective of bar
546}
547// caller's perspectives of foo's and bar's
[ad9f593]548\end{cfa}
[c3e41cda]549In caller's perspective, the parameter names (by virtue of being optional) are really comments;
550in the callee's perspective, parameter names are semantically significant.
[ad9f593]551Array parameters introduce a further, subtle, semantic difference and considerable freedom to comment.
552
[c3e41cda]553At the semantic level, there is no such thing as an array parameter, except for one case (@T [static 5]@) discussed shortly.
[ad9f593]554Rather, there are only pointer parameters.
[c3e41cda]555This fact probably shares considerable responsibility for the common sense of ``an array is just a pointer,'' which has been refuted in non-parameter contexts.
[ad9f593]556This fact holds in both the caller's and callee's perspectives.
[c3e41cda]557However, a parameter's type can include ``array of.'', \eg the type ``pointer to array of 5 ints'' (@T (*)[5]@) is a pointer type.
558This type is fully meaningful in the sense that its description does not contain any information that the type system ignores, and the type appears the same in the caller's \vs callee's perspectives.
559In fact, the outermost type constructor (syntactically first dimension) is really the one that determines the flavour of parameter.
560
561Yet, C allows array syntax for the outermost type constructor, from which comes the freedom to comment.
562An array parameter declaration can specify the outermost dimension with a dimension value, @[10]@ (which is ignored), an empty dimension list, @[ ]@, or a pointer, @*@, as seen in \VRef[Figure]{f:ArParmEquivDecl}.
563The rationale for rejecting the first ``invalid'' row follows shortly, while the second ``invalid'' row is simple nonsense, included to complete the pattern; its syntax hints at what the final row actually achieves.
[ad9f593]564
565\begin{figure}
[63cf80e]566\begin{cquote}
567\begin{tabular}{@{}llll@{}}
568\begin{cfa}
[ad9f593]569float sum( float a[5] );
570float sum( float a[5][4] );
571float sum( float a[5][] );
572float sum( float a[5]* );
573float sum( float *a[5] );
[63cf80e]574\end{cfa}
575&
576\begin{cfa}
[ad9f593]577float sum( float a[] );
578float sum( float a[][4] );
579float sum( float a[][] );
580float sum( float a[]* );
581float sum( float *a[] );
[63cf80e]582\end{cfa}
583&
584\begin{cfa}
[ad9f593]585float sum( float *a );
586float sum( float (*a)[4] );
587float sum( float (*a)[] );
588float sum( float (*a)* );
589float sum( float **a );
[63cf80e]590\end{cfa}
591&
592\begin{cfa}
[ad9f593]593// ar of float
594// mat of float
595// invalid
596// invalid
597// ar of ptr to float
[63cf80e]598\end{cfa}
599\end{tabular}
600\end{cquote}
[c3e41cda]601\caption{Multiple ways to declare an array parameter.
602Across a valid row, every declaration is equivalent.
603Each column gives a declaration style, where the style for that column is read from the first row.
604The second row begins the style for multiple dimensions, with the rows thereafter providing context for the choice of which second-row \lstinline{[]} receives the column-style variation.}
[ad9f593]605\label{f:ArParmEquivDecl}
606\end{figure}
607
[c3e41cda]608In the leftmost style, the typechecker ignores the actual value in most practical cases.
609This value is allowed to be a dynamic expression, and then it has practical cases.
610\begin{cfa}
611void foo( int @n@ ) {
612        float _42( float @a[n]@ ) {    // nested function
613                a[0] = 42;
614        }
615        float b[n];
616        _42( b );
617}
618\end{cfa}
[ad9f593]619
620
621% To help contextualize the matrix part of this example, the syntaxes @float [5][]@, @float [][]@ and @float (*)[]@ are all rejected, for reasons discussed shortly.
622% So are @float[5]*@, @float[]*@ and @float (*)*@.  These latter ones are simply nonsense, though they hint at ``1d array of pointers'', whose equivalent syntax options are, @float *[5]@, @float *[]@, and @float **@.
623
[c3e41cda]624It is a matter of taste as to whether a programmer should use a form as far left as possible (getting the most out of possible subscripting and dimension sizes), sticking to the right (avoiding false comfort from suggesting the typechecker is checking more than it is), or compromising in the middle (reducing unchecked information, yet clearly stating, ``I will subscript).
[ad9f593]625
[c3e41cda]626Note that this equivalence of pointer and array declarations is special to parameters.
[ad9f593]627It does not apply to local variables, where true array declarations are possible.
628\begin{cfa}
629void f( float * a ) {
630        float * b = a; // ok
631        float c[] = a; // reject
632        float d[] = { 1.0, 2.0, 3.0 }; // ok
633        static_assert( sizeof(b) == sizeof(float*) );
634        static_assert( sizeof(d) != sizeof(float*) );
635}
636\end{cfa}
637This equivalence has the consequence that the type system does not help a caller get it right.
[63cf80e]638\begin{cfa}
[ad9f593]639float sum( float v[] );
640float arg = 3.14;
[c3e41cda]641sum( &arg );                                                            $\C{// accepted, v = \&arg}$
[63cf80e]642\end{cfa}
[40ab446]643
[ad9f593]644Given the syntactic dimension forms @[ ]@ or @[5]@, it raises the question of qualifying the implied array pointer rather than the array element type.
[63cf80e]645For example, the qualifiers after the @*@ apply to the array pointer.
646\begin{cfa}
647void foo( const volatile int * @const volatile@ );
648void foo( const volatile int [ ] @const volatile@ ); // does not parse
649\end{cfa}
[ad9f593]650C instead puts these pointer qualifiers syntactically into the first dimension.
[63cf80e]651\begin{cquote}
652@[@ \textit{type-qualifier-list}$_{opt}$ \textit{assignment-expression}$_{opt}$ @]@
653\end{cquote}
654\begin{cfa}
655void foo( int [@const  volatile@] );
656void foo( int [@const  volatile@ 5] );          $\C{// 5 is ignored}$
657\end{cfa}
[40ab446]658
[ad9f593]659To make the first dimension size meaningful, C adds this form.
[63cf80e]660\begin{cquote}
661@[@ @static@ \textit{type-qualifier-list}$_{opt}$ \textit{assignment-expression} @]@
662\end{cquote}
663\begin{cfa}
664void foo( int [static @3@] );
665int ar[@10@];
666foo( ar ); // check argument dimension 10 > 3
667\end{cfa}
668Here, the @static@ storage qualifier defines the minimum array size for its argument.
[ad9f593]669@gcc@ ignores this dimension qualifier, \ie it gives no warning if the argument array size is less than the parameter minimum.  However, @clang@ implements the check, in accordance with the standard.  TODO: be specific about versions
670
671Note that there are now two different meanings for modifiers in the same position.  In
672\begin{cfa}
673void foo( int x[static const volatile 3] );
674\end{cfa}
675the @static@ applies to the 3, while the @const volatile@ applies to the @x@.
[40ab446]676
[ad9f593]677With multidimensional arrays, on dimensions after the first, a size is required and, is not ignored.
678These sizes are required for the callee to be able to subscript.
679\begin{cfa}
680void f( float a[][10], float b[][100] ) {
681    static_assert( ((char*)&a([1])) - ((char*)&a([0])) == 10 * sizeof(float) );
682    static_assert( ((char*)&b([1])) - ((char*)&b([0])) == 100 * sizeof(float) );
683}
684\end{cfa}
685Here, the distance between the first and second elements of each array depends on the inner dimension size.
686
[7d95bce9]687This significance of an inner dimension's length is a fact of the callee's perspective.
688In the caller's perspective, the type sytem is quite lax.
689Here, there is (some, but) little checking that what is being passed, matches.
690% void f( float [][10] );
691% int n = 100;
692% float a[100], b[n];
693% f(&a); // reject
694% f(&b); // accept
[ad9f593]695\begin{cfa}
[c3e41cda]696void foo() {
697        void f( float [][10] );
698        int n = 100;
699        float a[100], b[3][12], c[n], d[n][n];
[7d95bce9]700        f( a );
701        f( b );    $\C{// reject: inner dimension 12 for 10}$
702        f( c );
703        f( @d@ );  $\C{// accept with inner dimension n for 10}$
704        f( &a );   $\C{// reject: inner dimension 100 for 10}$
705        f( &b );
706        f( @&c@ ); $\C{// accept with inner dimension n for 10}$
707        f( &d );
[c3e41cda]708}
[ad9f593]709\end{cfa}
[7d95bce9]710The cases without comments are rejections, but simply because the array ranks do not match; in the commented cases, the ranks match and the rules being discussed apply.
711The cases @f(b)@ and @f(&a)@ show where some length checking occurs.
712But this checking misses the cases @f(d)@ and @f(&c)@, allowing the calls with mismatched lengths, actually 100 for 10.
713The C checking rule avoids false alarms, at the expense of safety, by allowing any combinations that involve dynamic values.
714Ultimately, an inner dimension's size is a callee's \emph{assumption} because the type system uses declaration details in the callee's perspective that it does not enforce in the caller's perspective.
[ad9f593]715
[c3e41cda]716Finally, to handle higher-dimensional VLAs, C repurposed the @*@ \emph{within} the dimension in a declaration to mean that the callee has make an assumption about the size, but no (unchecked, possibly wrong) information about this assumption is included for the caller-programmer's benefit/over-confidence.
[63cf80e]717\begin{cquote}
718@[@ \textit{type-qualifier-list$_{opt}$} @* ]@
719\end{cquote}
720\begin{cfa}
[ad9f593]721void foo( float [][@*@] );                                              $\C{// declaration}$ 
722void foo( float a[][10] ) { ... }                               $\C{// definition}$
723\end{cfa}
724Repeating it with the full context of a VLA is useful:
725\begin{cfa}
726void foo( int, float [][@*@] );                                 $\C{// declaration}$ 
727void foo( int n, float a[][n] ) { ... }                 $\C{// definition}$
[63cf80e]728\end{cfa}
[ad9f593]729Omitting the dimension from the declaration is consistent with omitting parameter names, for the declaration case has no name @n@ in scope.
730The omission is also redacting all information not needed to generate correct caller-side code.
[63cf80e]731
732
733\subsection{Arrays could be values}
[40ab446]734
[63cf80e]735All arrays have a know runtime size at their point of declaration.
736Furthermore, C provides an explicit mechanism to pass an array's dimensions to a function.
737Nevertheless, an array cannot be copied, and hence, not passed by value to a function, even when there is sufficient information to do so.
[40ab446]738
[63cf80e]739However, if an array is a structure field (compile-time size), it can be copied and passed by value.
740For example, a C @jmp_buf@ is an array.
741\begin{cfa}
742typedef long int jmp_buf[8];
743\end{cfa}
744A instance of this array can be declared as a structure field.
745\begin{cfa}
746struct Jmp_Buf {
747        @jmp_buf@ jb;
748};
749\end{cfa}
750Now the array can be copied (and passed by value) because structures can be copied.
751\begin{cfa}
752Jmp_Buf jb1, jb2;
753jb1 = jb2;
754void foo( Jmp_Buf );
755foo( jb2 );
756\end{cfa}
[40ab446]757
[63cf80e]758This same argument applies to returning arrays from functions.
759There can be sufficient information to return an array by value but it is not supported.
760Again, array wrapping allows an array to be returned from a function and copied into variable.
[40ab446]761
762
[f5fbcad]763\section{Linked List}
[40ab446]764
[c4024b46]765Linked-lists are blocks of storage connected using one or more pointers.
[520fa9e]766The storage block (node) is logically divided into data (user payload) and links (list pointers), where the links are the only component used by the list structure.
[0775468]767Since the data is opaque, list structures are often polymorphic over the data, which is often homogeneous.
768
[520fa9e]769The links organize nodes into a particular format, \eg queue, tree, hash table, \etc, with operations specific to that format.
[0775468]770Because a node's existence is independent of the data structure that organizes it, all nodes are manipulated by address not value;
771hence, all data structure routines take and return pointers to nodes and not the nodes themselves.
772
773
[1e110bf]774\subsection{Design issues}
[0775468]775\label{toc:lst:issue}
776
[520fa9e]777This thesis focuses on a reduced design space for linked lists that target \emph{system programmers}.
778Within this restricted space, all design-issue discussions assume the following invariants;
779alternatives to the assumptions are discussed under Future Work (Section~\ref{toc:lst:futwork}).
[0775468]780\begin{itemize}
[c721105]781        \item A doubly-linked list is being designed.
782                Generally, the discussed issues apply similarly for singly-linked lists.
783                Circular \vs ordered linking is discussed under List identity (Section~\ref{toc:lst:issue:ident}).
784        \item Link fields are system-managed.
785                The user works with the system-provided API to query and modify list membership.
786                The system has freedom over how to represent these links.
[0775468]787        \item The user data must provide storage for the list link-fields.
[c721105]788                Hence, a list node is \emph{statically} defined as data and links \vs a node that is \emph{dynamically} constructed from data and links \see{\VRef{toc:lst:issue:attach}}.
[0775468]789\end{itemize}
790
791
[1e110bf]792\subsection{Preexisting linked-list libraries}
[63cf80e]793\label{s:PreexistingLinked-ListLibraries}
[0775468]794
795Two preexisting linked-list libraries are used throughout, to show examples of the concepts being defined,
796and further libraries are introduced as needed.
797\begin{enumerate}
[520fa9e]798        \item Linux Queue library~\cite{lst:linuxq} (LQ) of @<sys/queue.h>@.
[c721105]799        \item \CC Standard Template Library's (STL)\footnote{The term STL is contentious as some people prefer the term standard library.} @std::list@\cite{lst:stl}
[0775468]800\end{enumerate}
[c721105]801%A general comparison of libraries' abilities is given under Related Work (Section~\ref{toc:lst:relwork}).
[0775468]802For the discussion, assume the fictional type @req@ (request) is the user's payload in examples.
[520fa9e]803As well, the list library is helping the user manage (organize) requests, \eg a request can be work on the level of handling a network arrival-event or scheduling a thread.
[0775468]804
805
[1e110bf]806\subsection{Link attachment: intrusive vs.\ wrapped}
[0775468]807\label{toc:lst:issue:attach}
808
809Link attachment deals with the question:
[520fa9e]810Where are the libraries' inter-node link-fields stored, in relation to the user's payload data fields?
[c721105]811\VRef[Figure]{fig:lst-issues-attach} shows three basic styles.
812\VRef[Figure]{f:Intrusive} shows the \newterm{intrusive} style, placing the link fields inside the payload structure.
813\VRef[Figures]{f:WrappedRef} and \subref*{f:WrappedValue} show the two \newterm{wrapped} styles, which place the payload inside a generic library-provided structure that then defines the link fields.
814The wrapped style distinguishes between wrapping a reference and wrapping a value, \eg @list<req *>@ or @list<req>@.
[0775468]815(For this discussion, @list<req &>@ is similar to @list<req *>@.)
816This difference is one of user style, not framework capability.
[c721105]817Library LQ is intrusive; STL is wrapped with reference and value.
[0775468]818
819\begin{comment}
820\begin{figure}
[c721105]821        \begin{tabularx}{\textwidth}{Y|Y|Y}
[0775468]822                \lstinput[language=C]{20-39}{lst-issues-intrusive.run.c}
[c721105]823                &\lstinputlisting[language=C++]{20-39}{lst-issues-wrapped-byref.run.cpp}
824                &\lstinputlisting[language=C++]{20-39}{lst-issues-wrapped-emplaced.run.cpp}
825          \\ & &
826          \\
827                \includegraphics[page=1]{lst-issues-attach.pdf}
828                &
829                \includegraphics[page=2]{lst-issues-attach.pdf}
830                &
831                \includegraphics[page=3]{lst-issues-attach.pdf}
832          \\ & &
833          \\
834                (a) & (b) & (c)
835        \end{tabularx}
[0775468]836\caption{
[c721105]837                Three styles of link attachment: (a)~intrusive, (b)~wrapped reference, and (c)~wrapped value.
838                The diagrams show the memory layouts that result after the code runs, eliding the head object \lstinline{reqs};
839                head objects are discussed in Section~\ref{toc:lst:issue:ident}.
840                In (a), the field \lstinline{req.x} names a list direction;
841                these are discussed in Section~\ref{toc:lst:issue:simultaneity}.
842                In (b) and (c), the type \lstinline{node} represents a system-internal type,
843                which is \lstinline{std::_List_node} in the GNU implementation.
844                (TODO: cite? found in  /usr/include/c++/7/bits/stl\_list.h )
845        }
846         \label{fig:lst-issues-attach}
[0775468]847\end{figure}
848\end{comment}
849
850\begin{figure}
851\centering
852\newsavebox{\myboxA}                                    % used with subfigure
853\newsavebox{\myboxB}
854\newsavebox{\myboxC}
855
856\begin{lrbox}{\myboxA}
857\begin{tabular}{@{}l@{}}
858\lstinput[language=C]{20-35}{lst-issues-intrusive.run.c} \\
859\includegraphics[page=1]{lst-issues-attach.pdf}
860\end{tabular}
861\end{lrbox}
862
863\begin{lrbox}{\myboxB}
864\begin{tabular}{@{}l@{}}
865\lstinput[language=C++]{20-35}{lst-issues-wrapped-byref.run.cpp} \\
866\includegraphics[page=2]{lst-issues-attach.pdf}
867\end{tabular}
868\end{lrbox}
869
870\begin{lrbox}{\myboxC}
871\begin{tabular}{@{}l@{}}
872\lstinput[language=C++]{20-35}{lst-issues-wrapped-emplaced.run.cpp} \\
873\includegraphics[page=3]{lst-issues-attach.pdf}
874\end{tabular}
875\end{lrbox}
876
877\subfloat[Intrusive]{\label{f:Intrusive}\usebox\myboxA}
878\hspace{6pt}
879\vrule
880\hspace{6pt}
881\subfloat[Wrapped reference]{\label{f:WrappedRef}\usebox\myboxB}
882\hspace{6pt}
883\vrule
884\hspace{6pt}
885\subfloat[Wrapped value]{\label{f:WrappedValue}\usebox\myboxC}
886
887\caption{
[c721105]888                Three styles of link attachment:
889                % \protect\subref*{f:Intrusive}~intrusive, \protect\subref*{f:WrappedRef}~wrapped reference, and \protect\subref*{f:WrappedValue}~wrapped value.
890                The diagrams show the memory layouts that result after the code runs, eliding the head object \lstinline{reqs};
891                head objects are discussed in Section~\ref{toc:lst:issue:ident}.
892                In \protect\subref*{f:Intrusive}, the field \lstinline{req.d} names a list direction;
893                these are discussed in Section~\ref{toc:lst:issue:simultaneity}.
894                In \protect\subref*{f:WrappedRef} and \protect\subref*{f:WrappedValue}, the type \lstinline{node} represents a
[0775468]895                library-internal type, which is \lstinline{std::_List_node} in the GNU implementation
[c721105]896                \see{\lstinline{/usr/include/c++/X/bits/stl_list.h}, where \lstinline{X} is the \lstinline{g++} version number}.
897        }
898        \label{fig:lst-issues-attach}
[0775468]899\end{figure}
900
901Each diagrammed example is using the fewest dynamic allocations for its respective style:
[c721105]902in intrusive, here is no dynamic allocation, in wrapped reference only the linked fields are dynamically allocated, and in wrapped value the copied data and linked fields are dynamically allocated.
903The advantage of intrusive is the control in memory layout and storage placement.
904Both wrapped styles have independent storage layout and imply library-induced heap allocations, with lifetime that matches the item's membership in the list.
[0775468]905In all three cases, a @req@ object can enter and leave a list many times.
[c721105]906However, in intrusive a @req@ can only be on one list at a time, unless there are separate link-fields for each simultaneous list.
907In wrapped reference, a @req@ can appear multiple times on the same or different lists simultaneously, but since @req@ is shared via the pointer, care must be taken if updating data also occurs simultaneously, \eg concurrency.
908In wrapped value, the @req@ is copied, which increases storage usage, but allows independent simultaneous changes;
[0775468]909however, knowing which of the @req@ object is the ``true'' object becomes complex.
910\see*{\VRef{toc:lst:issue:simultaneity} for further discussion.}
911
912The implementation of @LIST_ENTRY@ uses a trick to find the links and the node containing the links.
913The macro @LIST_INSERT_HEAD(&reqs, &r2, d);@ takes the list header, a pointer to the node, and the offset of the link fields in the node.
914One of the fields generated by @LIST_ENTRY@ is a pointer to the node, which is set to the node address, \eg @r2@.
915Hence, the offset to the link fields provides an access to the entire node, \ie the node points at itself.
916For list traversal, @LIST_FOREACH(cur, &reqs_pri, by_pri)@, there is the node cursor, the list, and the offset of the link fields within the node.
917The traversal actually moves from link fields to link fields within a node and sets the node cursor from the pointer within the link fields back to the node.
918
[520fa9e]919A further aspect of layout control is allowing the user to explicitly specify link fields controlling placement and attributes within the @req@ object.
920LQ allows this ability through the @LIST_ENTRY@ macro\footnote{It is possible to have multiple named linked fields allowing a node to appear on multiple lists simultaneously.}, which can be placed anywhere in the node.
921An example of an attribute on the link fields is cache alignment, possibly in conjunction with other @req@ fields, improving locality and/or avoiding false sharing.
922Supplying the link fields by inheritance makes them implicit and relies on compiler placement, such as the start or end of @req@, and no explicit attributes.
[ca4f2b2]923Wrapped reference has no control over the link fields, but the separate data allows some control;
[0775468]924wrapped value has no control over data or links.
925
926Another subtle advantage of intrusive arrangement is that a reference to a user-level item (@req@) is sufficient to navigate or manage the item's membership.
[c721105]927In LQ, the intrusive @req@ pointer is the right argument type for operations @LIST_NEXT@ or @LIST_REMOVE@;
[0775468]928there is no distinguishing a @req@ from ``a @req@ in a list.''
[c721105]929The same is not true of STL, wrapped reference or value.
930There, the analogous operations, @iterator::operator++()@, @iterator::operator*()@, and @list::erase(iterator)@, work on a parameter of type @list<T>::iterator@;
[520fa9e]931there is no mapping from @req &@ to @list<req>::iterator@. %, for linear search.
[0775468]932
[c721105]933The advantage of wrapped is the abstraction of a data item from its list membership(s).
[0775468]934In the wrapped style, the @req@ type can come from a library that serves many independent uses,
935which generally have no need for listing.
[c721105]936Then, a novel use can put a @req@ in a list, without requiring any upstream change in the @req@ library.
937In intrusive, the ability to be listed must be planned during the definition of @req@.
[0775468]938
939\begin{figure}
[c721105]940        \lstinput[language=C++]{100-117}{lst-issues-attach-reduction.hpp}
941        \lstinput[language=C++]{150-150}{lst-issues-attach-reduction.hpp}
942        \caption{
943                Simulation of wrapped using intrusive.
944                Illustrated by pseudocode implementation of an STL-compatible API fragment using LQ as the underlying implementation.
945                The gap that makes it pseudocode is that
946                the LQ C macros do not expand to valid C++ when instantiated with template parameters---there is no \lstinline{struct El}.
947                When using a custom-patched version of LQ to work around this issue,
948                the programs of Figure~\ref{f:WrappedRef} and wrapped value work with this shim in place of real STL.
949                Their executions lead to the same memory layouts.
950        }
951        \label{fig:lst-issues-attach-reduction}
[0775468]952\end{figure}
953
[c721105]954It is possible to simulate wrapped using intrusive, illustrated in Figure~\ref{fig:lst-issues-attach-reduction}.
[0775468]955This shim layer performs the implicit dynamic allocations that pure intrusion avoids.
956But there is no reduction going the other way.
957No shimming can cancel the allocations to which wrapped membership commits.
958
[c721105]959Because intrusion is a lower-level listing primitive, the system design choice is not between forcing users to use intrusion or wrapping.
[0775468]960The choice is whether or not to provide access to an allocation-free layer of functionality.
961An intrusive-primitive library like LQ lets users choose when to make this tradeoff.
[c721105]962A wrapped-primitive library like STL forces users to incur the costs of wrapping, whether or not they access its benefits.
[0775468]963
964
[1e110bf]965\subsection{Simultaneity: single vs.\ multi-static vs.\ dynamic}
[0775468]966\label{toc:lst:issue:simultaneity}
967
968\begin{figure}
[c721105]969        \parbox[t]{3.5in} {
970                \lstinput[language=C++]{20-60}{lst-issues-multi-static.run.c}
971        }\parbox[t]{20in} {
972                ~\\
973                \includegraphics[page=1]{lst-issues-direct.pdf} \\
974                ~\\
975                \hspace*{1.5in}\includegraphics[page=2]{lst-issues-direct.pdf}
976        }
977        \caption{
978                Example of simultaneity using LQ lists.
979                The zoomed-out diagram (right/top) shows the complete multi-linked data structure.
980                This structure can navigate all requests in priority order ({\color{blue}blue}), and navigate among requests with a common request value ({\color{orange}orange}).
981                The zoomed-in diagram (right/bottom) shows how the link fields connect the nodes on different lists.
982        }
983        \label{fig:lst-issues-multi-static}
[0775468]984\end{figure}
985
986\newterm{Simultaneity} deals with the question:
987In how many different lists can a node be stored, at the same time?
988Figure~\ref{fig:lst-issues-multi-static} shows an example that can traverse all requests in priority order (field @pri@) or navigate among requests with the same request value (field @rqr@).
989Each of ``by priority'' and ``by common request value'' is a separate list.
990For example, there is a single priority-list linked in order [1, 2, 2, 3, 3, 4], where nodes may have the same priority, and there are three common request-value lists combining requests with the same values: [42, 42], [17, 17, 17], and [99], giving four head nodes one for each list.
991The example shows a list can encompass all the nodes (by-priority) or only a subset of the nodes (three request-value lists).
992
[c721105]993As stated, the limitation of intrusive is knowing apriori how many groups of links are needed for the maximum number of simultaneous lists.
[0775468]994Thus, the intrusive LQ example supports multiple, but statically many, link lists.
[c721105]995Note, it is possible to reuse links for different purposes, \eg if a list in linked one way at one time and another way at another time, and these times do not overlap, the two different linkings can use the same link fields.
[520fa9e]996This feature is used in the \CFA runtime, where a thread node may be on a blocked or running list, but never on both simultaneously.
[0775468]997
998Now consider the STL in the wrapped-reference arrangement of Figure~\ref{f:WrappedRef}.
999Here it is possible to construct the same simultaneity by creating multiple STL lists, each pointing at the appropriate nodes.
1000Each group of intrusive links become the links for each separate STL list.
[520fa9e]1001The upside is the unlimited number of lists a node can be associated with simultaneously, as any number of STL lists can be created dynamically.
[ca4f2b2]1002The downside is the dynamic allocation of the link nodes and managing multiple lists.
[0775468]1003Note, it might be possible to wrap the multiple lists in another type to hide this implementation issue.
1004
1005Now consider the STL in the wrapped-value arrangement of Figure~\ref{f:WrappedValue}.
1006Again, it is possible to construct the same simultaneity by creating multiple STL lists, each copying the appropriate nodes, where the intrusive links become the links for each separate STL list.
[520fa9e]1007The upside is the same as for wrapped-reference arrangement with an unlimited number of list bindings.
1008The downside is the dynamic allocation, significant storage usage, and cost of copying nodes.
[0775468]1009As well, it is unclear how node updates work in this scenario, without some notation of ultimately merging node information.
1010
1011% https://www.geeksforgeeks.org/introduction-to-multi-linked-list/ -- example of building a bespoke multi-linked list out of STL primitives (providing indication that STL doesn't offer one); offers dynamic directionality by embedding `vector<struct node*> pointers;`
1012
1013% When allowing multiple static directions, frameworks differ in their ergonomics for
1014% the typical case: when the user needs only one direction, vs.\ the atypical case, when the user needs several.
1015% LQ's ergonomics are well-suited to the uncommon case of multiple list directions.
1016% Its intrusion declaration and insertion operation both use a mandatory explicit parameter naming the direction.
1017% This decision works well in Figure~\ref{fig:lst-issues-multi-static}, where the names @by_pri@ and @by_rqr@ work well,
1018% but it clutters Figure~\ref{f:Intrusive}, where a contrived name must be invented and used.
1019% The example uses @x@; @reqs@ would be a more readily ignored choice. \PAB{wording?}
1020
[520fa9e]1021An alternative system providing intrusive data-structures is \uCpp, a concurrent extension of \CC.
1022It provides a basic set of intrusive lists~\cite[appx.~F]{uC++}, where the link fields are defined with the data fields using inheritance.
[c721105]1023\begin{cquote}
1024\setlength{\tabcolsep}{15pt}
1025\begin{tabular}{@{}ll@{}}
1026\multicolumn{1}{c}{singly-linked list} & \multicolumn{1}{c}{doubly-linked list} \\
1027\begin{c++}
1028struct Node : public uColable {
1029        int i;  // data
1030        Node( int i ) : i{ i } {}
1031};
1032\end{c++}
1033&
1034\begin{c++}
1035struct Node : public uSeqable {
1036        int i;  // data
1037        Node( int i ) : i{ i } {}
1038};
1039\end{c++}
1040\end{tabular}
1041\end{cquote}
1042A node can be placed in the following data structures depending on its link fields: @uStack@ and @uQueue@ (singly linked), and @uSequence@ (doubly linked).
1043A node inheriting from @uSeqable@ can appear in a singly or doubly linked structure.
1044Structure operations implicitly know the link-field location through the inheritance.
1045\begin{c++}
1046uStack<Node> stack;
1047Node node;
1048stack.push( node );  // link fields at beginning of node
1049\end{c++}
1050
1051Simultaneity cannot be done with multiple inheritance, because there is no mechanism to either know the order of inheritance fields or name each inheritance.
1052Instead, a special type is require that contains the link fields and points at the node.
1053\begin{cquote}
1054\setlength{\tabcolsep}{10pt}
1055\begin{tabular}{@{}ll@{}}
1056\begin{c++}
1057struct NodeDL : public uSeqable {
1058        @Node & node;@  // node pointer
1059        NodeDL( Node & node ) : node( node ) {}
1060        Node & get() const { return node; }
1061};
1062\end{c++}
1063&
1064\begin{c++}
1065struct Node : public uColable {
1066        int i;  // data
1067        @NodeDL nodeseq;@  // embedded intrusive links
1068        Node( int i ) : i{ i }, @nodeseq{ this }@ {}
1069};
1070\end{c++}
1071\end{tabular}
1072\end{cquote}
1073This node can now be inserted into a doubly-linked list through the embedded intrusive links.
1074\begin{c++}
1075uSequence<NodeDL> sequence;
[4fc7388]1076sequence.add_front( @node.nodeseq@ );   $\C{// link fields in embedded type}$
1077NodeDL nodedl = sequence.remove( @node.nodeseq@ );
1078int i = nodedl.@get()@.i;                               $\C{// indirection to node}$
[c721105]1079\end{c++}
1080Hence, the \uCpp approach optimizes one set of intrusive links through the \CC inheritance mechanism, and falls back onto the LQ approach of explicit declarations for additional intrusive links.
1081However, \uCpp cannot apply the LQ trick for finding the links and node.
1082
1083The major ergonomic difference among the approaches is naming and name usage.
1084The intrusive model requires naming each set of intrusive links, \eg @by_pri@ and @by_rqr@ in \VRef[Figure]{fig:lst-issues-multi-static}.
1085\uCpp cheats by using inheritance for the first intrusive links, after which explicit naming of intrusive links is required.
1086Furthermore, these link names must be used in all list operations, including iterating, whereas wrapped reference and value hide the list names in the implicit dynamically-allocated node-structure.
[520fa9e]1087At issue is whether an API for simultaneity can support one \emph{implicit} list, when several are not wanted.
[c721105]1088\uCpp allows it, LQ does not, and the STL does not have this question.
[0775468]1089
1090
[1e110bf]1091\subsection{User integration: preprocessed vs.\ type-system mediated}
[0775468]1092
[520fa9e]1093\PAB{What do you want to say here?}
1094
[0775468]1095% example of poor error message due to LQ's preprocessed integration
1096% programs/lst-issues-multi-static.run.c:46:1: error: expected identifier or '(' before 'do'
1097%    46 | LIST_INSERT_HEAD(&reqs_rtr_42, &r42b, by_rqr);
1098%       | ^~~~~~~~~~~~~~~~
1099%
1100% ... not a wonderful example; it was a missing semicolon on the preceding line; but at least real
1101
1102
[1e110bf]1103\subsection{List identity: headed vs.\ ad-hoc}
[0775468]1104\label{toc:lst:issue:ident}
1105
[520fa9e]1106All examples so far use distinct user-facing types:
1107an item found in a list (type @req@ of variable @r1@, see \VRef[Figure]{fig:lst-issues-attach}), and a list (type @reql@ of variable @reqs_pri@, see \VRef[Figure]{fig:lst-issues-ident}).
1108The latter type is a head.
1109The resulting identity model (empty list) is just the head.
1110A bespoke ``pointer to next @req@'' implementation often omits the header;
1111hence, a pointer to any node can traverse its link fields: right or left and around, depending on the data structure.
[006d4c4]1112The resulting identity model is ad-hoc.
[0775468]1113
[520fa9e]1114Figure~\ref{fig:lst-issues-ident} shows the identity model's impact on
1115the existence of certain conceptual constructs, like zero-lengths lists and unlisted elements.
1116In headed thinking, there are length-zero lists (heads with no elements), and an element can be listed or not listed.
1117In ad-hoc thinking, there are no length-zero lists and every element belongs to a list of length at least one.
1118By omitting the head, elements can enter into an adjacency relationship, without requiring allocation for a head for the list, or finding a correct existing head.
1119
[006d4c4]1120\begin{figure}
1121        \centering
1122        \includegraphics{lst-issues-ident.pdf}
1123        \caption{
1124                Comparison of headed and ad-hoc list identities, for various list lengths.
1125                Pointers are logical, meaning that no claim is intended about which part of an object is being targeted.
1126        }
1127        \label{fig:lst-issues-ident}
1128\end{figure}
1129
[0775468]1130A head defines one or more element roles, among elements that share a transitive adjacency.
1131``First'' and ``last'' are element roles.
1132One moment's ``first'' need not be the next moment's.
1133
1134There is a cost to maintaining these roles.
[520fa9e]1135A runtime component of this cost is evident in LQ's offering the choice of type generators @LIST@ \vs @TAILQ@.
[0775468]1136Its @LIST@ maintains a ``first,'' but not a ``last;'' its @TAILQ@ maintains both roles.
1137(Both types are doubly linked and an analogous choice is available for singly linked.)
1138
1139TODO: finish making this point
1140
1141See WIP in lst-issues-adhoc-*.ignore.*.
1142
1143The code-complexity component of the cost ...
1144
1145Ability to offer heads is good.  Point: Does maintaining a head mean that the user has to provide more state when manipulating the list?  Requiring the user to do so is bad, because the user may have lots of "list" typed variables in scope, and detecting that the user passed the wrong one requires testing all the listing edge cases.
1146
[c4024b46]1147
[1e110bf]1148\subsection{End treatment: cased vs.\ uniform }
[006d4c4]1149
[520fa9e]1150A linear (non-circular), nonempty linked-list has a first element and a last element, whether or not the list is headed.
[006d4c4]1151A first element has no predecessor and a last element has no successor.
1152
1153\begin{figure}
1154        \centering
1155        \includegraphics{lst-issues-end.pdf}
1156        \caption{
1157                LQ sub-object-level representation of links and ends.
1158                Each object's memory is pictured as a vertical strip.
1159                Pointers' target locations, within these strips, are significant.
1160                Uniform treatment of the first-end is evident from an assertion like \lstinline{(**this.pred == this)} holding for all nodes \lstinline{this}, including the first one.
1161                Cased treatment of the last-end is evident from the symmetric proposition, \lstinline{(this.succ.pred == &this.succ)}, failing when \lstinline{this} is the last node.
1162        }
1163        \label{fig:lst-issues-end}
1164\end{figure}
1165
1166End treatment refers to how the list represents the lack of a predecessor/successor.
1167The following elaboration refers to the LQ representations, detailed in Figure~\ref{fig:lst-issues-end}.
1168The most obvious representation, a null pointer, mandates a cased end treatment.
1169LQ uses this representation for its successor/last.
1170Consider the operation of inserting after a given element.
[520fa9e]1171A doubly-linked list must update the given node's successor, to make its predecessor-pointer refer to the new node.
[006d4c4]1172This step must happen when the given node has a successor (when its successor pointer is non-null),
1173and must be skipped when it does not (when its successor pointer cannot be navigated).
1174So this operation contains a branch, to decide which case is happening.
1175All branches have pathological cases where branch prediction's success rate is low and the execution pipeline is stalling often.
[520fa9e]1176Hence, this issue is implementation-level, relevant to achieving high performance.
[006d4c4]1177
1178This branch is sometimes avoidable; the result is a uniform end treatment.
[520fa9e]1179Here is one example of such an implementation that works for a headed list.
1180LQ uses this representation for its predecessor/first.  (All LQ offerings are headed at the front.)
[006d4c4]1181For predecessor/first navigation, the relevant operation is inserting before a given element.
1182LQ's predecessor representation is not a pointer to a node, but a pointer to a pseudo-successor pointer.
1183When there is a predecessor node, that node contains a real-successor pointer; it is the target of the reference node's predecessor pointer.
1184When there is no predecessor node, the reference node (now known to be first node) acts as the pseudo-successor of the list head.
1185The list head contains a pointer to the first node.
1186When inserting before the first node, the list head's first-pointer is the one that must change.
1187So, the first node's ``predecessor'' pointer (to a pseudo-successor pointer) is set as the list head's first-pointer.
1188Now, inserting before a given element does the same logic in both cases:
1189follow the guaranteed-non-null predecessor pointer, and update what you find there to refer to the new node.
[520fa9e]1190Applying this trick makes it possible to have list management routines that are completely free of conditional control-flow.
[006d4c4]1191Considering a path length of only a few instructions (less than the processor's pipeline length),
1192such list management operations are often practically free,
1193with all the variance being due to the (inevitable) cache status of the nodes being managed.
[40ab446]1194
[f5fbcad]1195\section{String}
[c4024b46]1196
[ca4f2b2]1197A string is a sequence of symbols, where the form of a symbol can vary significantly: 7/8-bit characters (ASCII/Latin-1), or 2/4/8-byte (UNICODE) characters/symbols or variable length (UTF-8/16/32) characters.
[c4024b46]1198A string can be read left-to-right, right-to-left, top-to-bottom, and have stacked elements (Arabic).
1199
[ca4f2b2]1200A C character constant is an ASCII/Latin-1 character enclosed in single-quotes, \eg @'x'@, @'@\textsterling@'@.
1201A wide C character constant is the same, except prefixed by the letter @L@, @u@, or @U@, \eg @u'\u25A0'@ (black square), where the @\u@ identifies a universal character name.
[c721105]1202A character can be formed from an escape sequence, which expresses a non-typable character @'\f'@ form feed, a delimiter character @'\''@ embedded single quote, or a raw character @'\xa3'@ \textsterling.
[c4024b46]1203
[c721105]1204A C character string is zero or more regular, wide, or escape characters enclosed in double-quotes @"xyz\n"@.
[ca4f2b2]1205The kind of characters in the string is denoted by a prefix: UTF-8 characters are prefixed by @u8@, wide characters are prefixed by @L@, @u@, or @U@.
[c4024b46]1206
[63cf80e]1207For UTF-8 string literals, the array elements have type @char@ and are initialized with the characters of the multi-byte character sequences, \eg @u8"\xe1\x90\x87"@ (Canadian syllabics Y-Cree OO).
1208For wide string literals prefixed by the letter @L@, the array elements have type @wchar_t@ and are initialized with the wide characters corresponding to the multi-byte character sequence, \eg @L"abc@$\mu$@"@ and are read/printed using @wsanf@/@wprintf@.
[ca4f2b2]1209The value of a wide-character is implementation-defined, usually a UTF-16 character.
[63cf80e]1210For wide string literals prefixed by the letter @u@ or @U@, the array elements have type @char16_t@ or @char32_t@, respectively, and are initialized with wide characters corresponding to the multi-byte character sequence, \eg @u"abc@$\mu$@"@, @U"abc@$\mu$@"@.
[ca4f2b2]1211The value of a @"u"@ character is an UTF-16 character;
1212the value of a @"U"@ character is an UTF-32 character.
[63cf80e]1213The value of a string literal containing a multi-byte character or escape sequence not represented in the execution character set is implementation-defined.
[c4024b46]1214
[ca4f2b2]1215C strings are null-terminated rather than maintaining a separate string length.
[c4024b46]1216\begin{quote}
1217Technically, a string is an array whose elements are single characters.
1218The compiler automatically places the null character @\0@ at the end of each such string, so programs can conveniently find the end.
[4fc7388]1219This representation means that there is no real limit to how long a string can be, but programs have to scan one completely to determine its length.~\cite[p.~36]{C:old}
[c4024b46]1220\end{quote}
[ca4f2b2]1221Unfortunately, this design decision is both unsafe and inefficient.
[4fc7388]1222It is common error in C to forget the storage in a character array for the terminator or overwrite the terminator, resulting in array overruns in string operations.
[c721105]1223The need to repeatedly scan an entire string to determine its length can result in significant cost, as it is impossible to cache the length in many cases.
[ca4f2b2]1224
1225C strings are fixed size because arrays are used for the implementation.
[4fc7388]1226However, string manipulation commonly results in dynamically-sized temporary and final string values, \eg @strcpy@, @strcat@, @strcmp@, @strlen@, @strstr@, \etc.
[ca4f2b2]1227As a result, storage management for C strings is a nightmare, quickly resulting in array overruns and incorrect results.
1228
[c721105]1229Collectively, these design decisions make working with strings in C, awkward, time consuming, and unsafe.
[4fc7388]1230While there are companion string routines that take the maximum lengths of strings to prevent array overruns, \eg @strncpy@, @strncat@, @strncpy@, that means the semantics of the operation can fail because strings are truncated.
1231Suffice it to say, C is not a go-to language for string applications, which is why \CC introduced the dynamically-sized @string@ type.
Note: See TracBrowser for help on using the repository browser.