Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    r128c4e4 rc6dc7f2  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Mon Oct  5 08:57:29 2020
    14 %% Update Count     : 3998
     13%% Last Modified On : Sat Jul 13 18:36:18 2019
     14%% Update Count     : 3876
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    3030\usepackage{upquote}                                                                    % switch curled `'" to straight
    3131\usepackage{calc}
     32\usepackage{xspace}
    3233\usepackage{varioref}                                                                   % extended references
    33 \usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt]{subfig}
    34 \renewcommand{\thesubfigure}{\alph{subfigure})}
     34\usepackage{listings}                                                                   % format program code
    3535\usepackage[flushmargin]{footmisc}                                              % support label/reference in footnote
    3636\usepackage{latexsym}                                   % \Box glyph
    3737\usepackage{mathptmx}                                   % better math font with "times"
    3838\usepackage[usenames]{color}
    39 \newcommand{\CFALatin}{}
     39\input{common}                                          % common CFA document macros
     40\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
     41\usepackage{breakurl}
     42
     43\usepackage[pagewise]{lineno}
     44\renewcommand{\linenumberfont}{\scriptsize\sffamily}
     45\usepackage[firstpage]{draftwatermark}
     46\SetWatermarkLightness{0.9}
     47
     48% Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
     49% removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
     50% AFTER HYPERREF.
     51\renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
     52
     53\setlength{\topmargin}{-0.45in}                                                 % move running title into header
     54\setlength{\headsep}{0.25in}
     55
     56%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     57
     58\CFAStyle                                                                                               % use default CFA format-style
     59\lstnewenvironment{C++}[1][]                            % use C++ style
     60{\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}}
     61{}
     62
    4063% inline code ©...© (copyright symbol) emacs: C-q M-)
    4164% red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
     
    4568% keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
    4669% math escape $...$ (dollar symbol)
    47 \input{common}                                          % common CFA document macros
    48 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
    49 \usepackage{breakurl}
    50 
    51 \renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt}
    52 
    53 \usepackage[pagewise]{lineno}
    54 \renewcommand{\linenumberfont}{\scriptsize\sffamily}
    55 \usepackage[firstpage]{draftwatermark}
    56 \SetWatermarkLightness{0.9}
    57 
    58 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
    59 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
    60 % AFTER HYPERREF.
    61 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
    62 
    63 \setlength{\topmargin}{-0.45in}                                                 % move running title into header
    64 \setlength{\headsep}{0.25in}
    65 
    66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    67 
    68 \CFAStyle                                                                                               % use default CFA format-style
    69 \lstnewenvironment{C++}[1][]                            % use C++ style
    70 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}}
    71 {}
    72 
    73 \newsavebox{\myboxA}
    74 \newsavebox{\myboxB}
    7570
    7671%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    8479\newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
    8580\newcommand{\KWC}{K-W C\xspace}
     81
     82\newsavebox{\LstBox}
    8683
    8784%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    214211Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction.
    215212For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is usually the only language of choice.
    216 The TIOBE index~\cite{TIOBE} for February 2020 ranks the top six most \emph{popular} programming languages as \Index*{Java} 17.4\%, C 16.8\%, Python 9.3\%, \Index*[C++]{\CC{}} 6.2\%, \Csharp 5.9\%, Visual Basic 5.9\% = 61.5\%, where the next 50 languages are less than 2\% each, with a long tail.
    217 The top 4 rankings over the past 35 years are:
     213The TIOBE index~\cite{TIOBE} for July 2018 ranks the top five most \emph{popular} programming languages as \Index*{Java} 16\%, C 14\%, \Index*[C++]{\CC{}} 7.5\%, Python 6\%, Visual Basic 4\% = 47.5\%, where the next 50 languages are less than 4\% each, with a long tail.
     214The top 3 rankings over the past 30 years are:
    218215\begin{center}
    219216\setlength{\tabcolsep}{10pt}
    220 \begin{tabular}{@{}rcccccccc@{}}
    221                 & 2020  & 2015  & 2010  & 2005  & 2000  & 1995  & 1990  & 1985  \\ \hline
    222 Java    & 1             & 2             & 1             & 2             & 3             & -             & -             & -             \\
    223 \R{C}   & \R{2} & \R{1} & \R{2} & \R{1} & \R{1} & \R{2} & \R{1} & \R{1} \\
    224 Python  & 3             & 7             & 6             & 6             & 22    & 21    & -             & -             \\
    225 \CC             & 4             & 4             & 4             & 3             & 2             & 1             & 2             & 12    \\
     217\begin{tabular}{@{}rccccccc@{}}
     218                & 2018  & 2013  & 2008  & 2003  & 1998  & 1993  & 1988  \\ \hline
     219Java    & 1             & 2             & 1             & 1             & 16    & -             & -             \\
     220\R{C}   & \R{2} & \R{1} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} \\
     221\CC             & 3             & 4             & 3             & 3             & 2             & 2             & 5             \\
    226222\end{tabular}
    227223\end{center}
     
    256252
    257253The signature feature of \CFA is \emph{\Index{overload}able} \Index{parametric-polymorphic} functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name):
    258 \begin{cfa}
     254\begin{lstlisting}
    259255®forall( otype T )® T identity( T val ) { return val; }
    260256int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§
    261 \end{cfa}
     257\end{lstlisting}
    262258% extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions.
    263259\CFA{}\hspace{1pt}'s polymorphism was originally formalized by \Index*{Glen Ditchfield}\index{Ditchfield, Glen}~\cite{Ditchfield92}, and first implemented by \Index*{Richard Bilson}\index{Bilson, Richard}~\cite{Bilson03}.
     
    278274\begin{comment}
    279275A simple example is leveraging the existing type-unsafe (©void *©) C ©bsearch© to binary search a sorted floating array:
    280 \begin{cfa}
     276\begin{lstlisting}
    281277void * bsearch( const void * key, const void * base, size_t dim, size_t size,
    282278                                int (* compar)( const void *, const void * ));
     
    287283double key = 5.0, vals[10] = { /* 10 sorted floating values */ };
    288284double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§
    289 \end{cfa}
     285\end{lstlisting}
    290286which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers:
    291 \begin{cfa}
     287\begin{lstlisting}
    292288forall( otype T | { int ?<?( T, T ); } ) T * bsearch( T key, const T * arr, size_t size ) {
    293289        int comp( const void * t1, const void * t2 ) { /* as above with double changed to T */ }
     
    300296double * val = bsearch( 5.0, vals, 10 ); §\C{// selection based on return type}§
    301297int posn = bsearch( 5.0, vals, 10 );
    302 \end{cfa}
     298\end{lstlisting}
    303299The nested function ©comp© provides the hidden interface from typed \CFA to untyped (©void *©) C, plus the cast of the result.
    304300Providing a hidden ©comp© function in \CC is awkward as lambdas do not use C calling-conventions and template declarations cannot appear at block scope.
     
    308304\CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations.
    309305For example, it is possible to write a type-safe \CFA wrapper ©malloc© based on the C ©malloc©:
    310 \begin{cfa}
     306\begin{lstlisting}
    311307forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); }
    312308int * ip = malloc(); §\C{// select type and size from left-hand side}§
    313309double * dp = malloc();
    314310struct S {...} * sp = malloc();
    315 \end{cfa}
     311\end{lstlisting}
    316312where the return type supplies the type/size of the allocation, which is impossible in most type systems.
    317313\end{comment}
     
    516512Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism:
    517513\begin{cfa}
    518 int ®``®otype = 3; §\C{// make keyword an identifier}§
    519 double ®``®forall = 3.5;
     514int ®`®otype®`® = 3; §\C{// make keyword an identifier}§
     515double ®`®forall®`® = 3.5;
    520516\end{cfa}
    521517
     
    528524// include file uses the CFA keyword "with".
    529525#if ! defined( with ) §\C{// nesting ?}§
    530 #define with ®``®with §\C{// make keyword an identifier}§
     526#define with ®`®with®`® §\C{// make keyword an identifier}§
    531527#define __CFA_BFD_H__
    532528#endif
    533 §{\color{red}\#\textbf{include\_next} <bfdlink.h>}§ §\C{// must have internal check for multiple expansion}§
     529
     530®#include_next <bfdlink.h> §\C{// must have internal check for multiple expansion}§
     531®
    534532#if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§
    535533#undef with
     
    578576\section{Exponentiation Operator}
    579577
    580 C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation.
    581 \CFA extends the basic operators with the exponentiation operator ©?®\®?©\index{?\\?@©?®\®?©} and ©?\=?©\index{?\\=?@©®\®=?©}, as in, ©x ®\® y© and ©x ®\®= y©, which means $x^y$ and $x \leftarrow x^y$.
     578C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow}, to perform the exponentiation operation.
     579\CFA extends the basic operators with the exponentiation operator ©?\?©\index{?\\?@©?\?©} and ©?\=?©\index{?\\=?@©\=?©}, as in, ©x \ y© and ©x \= y©, which means $x^y$ and $x \leftarrow x^y$.
    582580The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©((w * (((int)x) \ ((int)y))) * z)©.
    583581
    584 There are exponentiation operators for integral and floating types, including the builtin \Index{complex} types.
     582As for \Index{division}, there are exponentiation operators for integral and floating types, including the builtin \Index{complex} types.
    585583Integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication\footnote{The multiplication computation is $O(\log y)$.} (or shifting if the exponent is 2).
    586 Overflow for a large exponent or negative exponent returns zero.
     584Overflow from large exponents or negative exponents return zero.
    587585Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative.
    588586\begin{cfa}
     
    5915891 1 256 -64 125 ®0® 3273344365508751233 ®0® ®0® -0.015625 18.3791736799526 0.264715-1.1922i
    592590\end{cfa}
    593 Note, ©5 \ 32© and ©5L \ 64© overflow, and ©-4 \ -3© is a fraction but stored in an integer so all three computations generate an integral zero.
     591Note, ©5 ®\® 32© and ©5L ®\® 64© overflow, and ©-4 ®\® -3© is a fraction but stored in an integer so all three computations generate an integral zero.
    594592Parenthesis are necessary for complex constants or the expression is parsed as ©1.0f+®(®2.0fi \ 3.0f®)®+2.0fi©.
    595593The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation version is available.
     
    600598OT ?®\®?( OT ep, unsigned long int y );
    601599\end{cfa}
    602 The user type ©T© must define multiplication, one (©1©), and ©*©.
     600The user type ©T© must define multiplication, one, ©1©, and, ©*©.
    603601
    604602
     
    628626
    629627
    630 %\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}}
    631 \subsection{\texorpdfstring{\LstKeywordStyle{case} Clause}{case Clause}}
    632 
    633 C restricts the ©case© clause of a ©switch© statement to a single value.
    634 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values.
    635 Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C.
    636 Therefore, the ©case© clause is extended with a list of values, as in:
     628\subsection{Loop Control}
     629
     630The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}).
     631\begin{itemize}
     632\item
     633An empty conditional implies ©1©.
     634\item
     635The up-to range ©~©\index{~@©~©} means exclusive range [M,N).
     636\item
     637The up-to range ©~=©\index{~=@©~=©} means inclusive range [M,N].
     638\item
     639The down-to range ©-~©\index{-~@©-~©} means exclusive range [N,M).
     640\item
     641The down-to range ©-~=©\index{-~=@©-~=©} means inclusive range [N,M].
     642\item
     643©@© means put nothing in this field.
     644\item
     645©0© is the implicit start value;
     646\item
     647©1© is the implicit increment value.
     648\item
     649The up-to range uses ©+=© for increment;
     650\item
     651The down-to range uses ©-=© for decrement.
     652\item
     653The loop index is polymorphic in the type of the start value or comparison value when start is implicitly ©0©.
     654\end{itemize}
     655
     656\begin{figure}
    637657\begin{cquote}
    638 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
    639 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
    640 \begin{cfa}
    641 switch ( i ) {
    642   case ®1, 3, 5®:
    643         ...
    644   case ®2, 4, 6®:
    645         ...
    646 }
     658\begin{tabular}{@{}l|l@{}}
     659\multicolumn{1}{c|}{loop control} & \multicolumn{1}{c}{output} \\
     660\hline
     661\begin{cfa}
     662sout | nlOff;
     663while ®()® { sout | "empty"; break; } sout | nl;
     664do { sout | "empty"; break; } while ®()®; sout | nl;
     665for ®()® { sout | "empty"; break; } sout | nl;
     666for ( ®0® ) { sout | "A"; } sout | "zero" | nl;
     667for ( ®1® ) { sout | "A"; } sout | nl;
     668for ( ®10® ) { sout | "A"; } sout | nl;
     669for ( ®1 ~= 10 ~ 2® ) { sout | "B"; } sout | nl;
     670for ( ®10 -~= 1 ~ 2® ) { sout | "C"; } sout | nl;
     671for ( ®0.5 ~ 5.5® ) { sout | "D"; } sout | nl;
     672for ( ®5.5 -~ 0.5® ) { sout | "E"; } sout | nl;
     673for ( ®i; 10® ) { sout | i; } sout | nl;
     674for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; } sout | nl;
     675for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; } sout | nl;
     676for ( ®i; 0.5 ~ 5.5® ) { sout | i; } sout | nl;
     677for ( ®i; 5.5 -~ 0.5® ) { sout | i; } sout | nl;
     678for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; } sout | nl;
     679for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; } sout | nl;
     680enum { N = 10 };
     681for ( ®N® ) { sout | "N"; } sout | nl;
     682for ( ®i; N® ) { sout | i; } sout | nl;
     683for ( ®i; N -~ 0® ) { sout | i; } sout | nl;
     684const int start = 3, comp = 10, inc = 2;
     685for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; } sout | nl;
     686for ( ®i; 1 ~ @® ) { if ( i > 10 ) break;
     687        sout | i; } sout | nl;
     688for ( ®i; 10 -~ @® ) { if ( i < 0 ) break;
     689        sout | i; } sout | nl;
     690for ( ®i; 2 ~ @ ~ 2® ) { if ( i > 10 ) break;
     691        sout | i; } sout | nl;
     692for ( ®i; 2.1 ~ @ ~ @® ) { if ( i > 10.5 ) break;
     693        sout | i; i += 1.7; } sout | nl;
     694for ( ®i; 10 -~ @ ~ 2® ) { if ( i < 0 ) break;
     695        sout | i; } sout | nl;
     696for ( ®i; 12.1 ~ @ ~ @® ) { if ( i < 2.5 ) break;
     697        sout | i; i -= 1.7; } sout | nl;
     698for ( ®i; 5 : j; -5 ~ @® ) { sout | i | j; } sout | nl;
     699for ( ®i; 5 : j; -5 -~ @® ) { sout | i | j; } sout | nl;
     700for ( ®i; 5 : j; -5 ~ @ ~ 2® ) { sout | i | j; } sout | nl;
     701for ( ®i; 5 : j; -5 -~ @ ~ 2® ) { sout | i | j; } sout | nl;
     702for ( ®j; -5 ~ @ : i; 5® ) { sout | i | j; } sout | nl;
     703for ( ®j; -5 -~ @ : i; 5® ) { sout | i | j; } sout | nl;
     704for ( ®j; -5 ~ @ ~ 2 : i; 5® ) { sout | i | j; } sout | nl;
     705for ( ®j; -5 -~ @ ~ 2 : i; 5® ) { sout | i | j; } sout | nl;
     706for ( ®j; -5 -~ @ ~ 2 : i; 5 : k; 1.5 ~ @® ) {
     707        sout | i | j | k; } sout | nl;
     708for ( ®j; -5 -~ @ ~ 2 : k; 1.5 ~ @ : i; 5® ) {
     709        sout | i | j | k; } sout | nl;
     710for ( ®k; 1.5 ~ @ : j; -5 -~ @ ~ 2 : i; 5® ) {
     711        sout | i | j | k; } sout | nl;
    647712\end{cfa}
    648713&
    649714\begin{cfa}
    650 switch ( i ) {
    651   case 1: case 3 : case 5:
    652         ...
    653   case 2: case 4 : case 6:
    654         ...
    655 }
    656 \end{cfa}
    657 &
    658 \begin{cfa}
    659 
    660 // odd values
    661 
    662 // even values
    663 
    664 
     715
     716empty
     717empty
     718empty
     719zero
     720A
     721A A A A A A A A A A
     722B B B B B
     723C C C C C
     724D D D D D
     725E E E E E
     7260 1 2 3 4 5 6 7 8 9
     7271 3 5 7 9
     72810 8 6 4 2
     7290.5 1.5 2.5 3.5 4.5
     7305.5 4.5 3.5 2.5 1.5
     7312 4 6 8 10
     73210 8 6 4 2
     733
     734N N N N N N N N N N
     7350 1 2 3 4 5 6 7 8 9
     73610 9 8 7 6 5 4 3 2 1
     737
     7383 6 9
     739
     7401 2 3 4 5 6 7 8 9 10
     741
     74210 9 8 7 6 5 4 3 2 1 0
     743
     7442 4 6 8 10
     745
     7462.1 3.8 5.5 7.2 8.9
     747
     74810 8 6 4 2 0
     749
     75012.1 10.4 8.7 7 5.3 3.6
     7510 -5 1 -4 2 -3 3 -2 4 -1
     7520 -5 1 -6 2 -7 3 -8 4 -9
     7530 -5 1 -3 2 -1 3 1 4 3
     7540 -5 1 -7 2 -9 3 -11 4 -13
     7550 -5 1 -4 2 -3 3 -2 4 -1
     7560 -5 1 -6 2 -7 3 -8 4 -9
     7570 -5 1 -3 2 -1 3 1 4 3
     7580 -5 1 -7 2 -9 3 -11 4 -13
     759
     7600 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
     761
     7620 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
     763
     7640 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
    665765\end{cfa}
    666766\end{tabular}
    667767\end{cquote}
    668 In addition, subranges are allowed to specify case values.\footnote{
    669 gcc has the same mechanism but awkward syntax, \lstinline@2 ...42@, because a space is required after a number, otherwise the period is a decimal point.}
    670 \begin{cfa}
    671 switch ( i ) {
    672   case ®1~5:® §\C{// 1, 2, 3, 4, 5}§
    673         ...
    674   case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§
    675         ...
    676 }
    677 \end{cfa}
    678 Lists of subranges are also allowed.
    679 \begin{cfa}
    680 case ®1~5, 12~21, 35~42®:
    681 \end{cfa}
     768\caption{Loop Control Examples}
     769\label{f:LoopControlExamples}
     770\end{figure}
    682771
    683772
     
    888977
    889978
    890 \subsection{Non-terminating and Labelled \texorpdfstring{\LstKeywordStyle{fallthrough}}{Non-terminating and Labelled fallthrough}}
    891 
    892 The ©fallthrough© clause may be non-terminating within a ©case© clause or have a target label to common code from multiple case clauses.
    893 \begin{center}
    894 \begin{tabular}{@{}lll@{}}
    895 \begin{cfa}
    896 choose ( ... ) {
    897   case 3:
    898         if ( ... ) {
    899                 ... ®fallthru;® // goto case 4
    900         } else {
    901                 ...
    902         }
    903         // implicit break
    904   case 4:
    905 
    906 
    907 
    908 
     979%\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}}
     980\subsection{\texorpdfstring{\LstKeywordStyle{case} Statement}{case Statement}}
     981
     982C restricts the ©case© clause of a ©switch© statement to a single value.
     983For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values.
     984Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C.
     985Therefore, the ©case© clause is extended with a list of values, as in:
     986\begin{cquote}
     987\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
     988\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
     989\begin{cfa}
     990switch ( i ) {
     991  case ®1, 3, 5®:
     992        ...
     993  case ®2, 4, 6®:
     994        ...
     995}
    909996\end{cfa}
    910997&
    911998\begin{cfa}
    912 choose ( ... ) {
    913   case 3:
    914         ... ®fallthrough common;®
    915   case 4:
    916         ... ®fallthrough common;®
    917 
    918   ®common:® // below fallthrough
    919                           // at case-clause level
    920         ...     // common code for cases 3/4
    921         // implicit break
    922   case 4:
    923 
    924 
     999switch ( i ) {
     1000  case 1: case 3 : case 5:
     1001        ...
     1002  case 2: case 4 : case 6:
     1003        ...
     1004}
    9251005\end{cfa}
    9261006&
    9271007\begin{cfa}
    928 choose ( ... ) {
    929   case 3:
    930         choose ( ... ) {
    931           case 4:
    932                 for ( ... ) {
    933                         // multi-level transfer
    934                         ... ®fallthru common;®
    935                 }
    936                 ...
    937         }
     1008
     1009// odd values
     1010
     1011// even values
     1012
     1013
     1014\end{cfa}
     1015\end{tabular}
     1016\end{cquote}
     1017In addition, subranges are allowed to specify case values.\footnote{
     1018gcc has the same mechanism but awkward syntax, \lstinline@2 ...42@, because a space is required after a number, otherwise the period is a decimal point.}
     1019\begin{cfa}
     1020switch ( i ) {
     1021  case ®1~5:® §\C{// 1, 2, 3, 4, 5}§
    9381022        ...
    939   ®common:® // below fallthrough
    940                           // at case-clause level
    941 \end{cfa}
    942 \end{tabular}
    943 \end{center}
    944 The target label must be below the ©fallthrough© and may not be nested in a control structure, and
    945 the target label must be at the same or higher level as the containing ©case© clause and located at
    946 the same level as a ©case© clause; the target label may be case ©default©, but only associated
    947 with the current ©switch©/©choose© statement.
    948 
    949 \begin{figure}
    950 \begin{tabular}{@{}l|l@{}}
    951 \multicolumn{1}{c|}{loop control} & \multicolumn{1}{c}{output} \\
    952 \hline
    953 \begin{cfa}[xleftmargin=0pt]
    954 while ®()® { sout | "empty"; break; }
    955 do { sout | "empty"; break; } while ®()®;
    956 for ®()® { sout | "empty"; break; }
    957 for ( ®0® ) { sout | "A"; } sout | "zero";
    958 for ( ®1® ) { sout | "A"; }
    959 for ( ®10® ) { sout | "A"; }
    960 for ( ®= 10® ) { sout | "A"; }
    961 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; }
    962 for ( ®10 -~= 1 ~ 2® ) { sout | "C"; }
    963 for ( ®0.5 ~ 5.5® ) { sout | "D"; }
    964 for ( ®5.5 -~ 0.5® ) { sout | "E"; }
    965 for ( ®i; 10® ) { sout | i; }
    966 for ( ®i; = 10® ) { sout | i; }
    967 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; }
    968 for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; }
    969 for ( ®i; 0.5 ~ 5.5® ) { sout | i; }
    970 for ( ®i; 5.5 -~ 0.5® ) { sout | i; }
    971 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; }
    972 for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; }
    973 enum { N = 10 };
    974 for ( ®N® ) { sout | "N"; }
    975 for ( ®i; N® ) { sout | i; }
    976 for ( ®i; N -~ 0® ) { sout | i; }
    977 const int start = 3, comp = 10, inc = 2;
    978 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; }
    979 for ( i; 1 ~ ®@® ) { if ( i > 10 ) break; sout | i; }
    980 for ( i; 10 -~ ®@® ) { if ( i < 0 ) break; sout | i; }
    981 for ( i; 2 ~ ®@® ~ 2 ) { if ( i > 10 ) break; sout | i; }
    982 for ( i; 2.1 ~ ®@® ~ ®@® ) { if ( i > 10.5 ) break; sout | i; i += 1.7; }
    983 for ( i; 10 -~ ®@® ~ 2 ) { if ( i < 0 ) break; sout | i; }
    984 for ( i; 12.1 ~ ®@® ~ ®@® ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; }
    985 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; }
    986 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; }
    987 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; }
    988 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; }
    989 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; }
    990 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; }
    991 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; }
    992 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; }
    993 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; }
    994 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; }
    995 for ( i; 5 ®:® k; 1.5 ~ @ ®:® j; -5 -~ @ ~ 2 ) { sout | i | j | k; }
    996 \end{cfa}
    997 &
    998 \begin{cfa}
    999 empty
    1000 empty
    1001 empty
    1002 zero
    1003 A
    1004 A A A A A A A A A A
    1005 A A A A A A A A A A A
    1006 B B B B B
    1007 C C C C C
    1008 D D D D D
    1009 E E E E E
    1010 0 1 2 3 4 5 6 7 8 9
    1011 0 1 2 3 4 5 6 7 8 9 10
    1012 1 3 5 7 9
    1013 10 8 6 4 2
    1014 0.5 1.5 2.5 3.5 4.5
    1015 5.5 4.5 3.5 2.5 1.5
    1016 2 4 6 8 10
    1017 10 8 6 4 2
    1018 
    1019 N N N N N N N N N N
    1020 0 1 2 3 4 5 6 7 8 9
    1021 10 9 8 7 6 5 4 3 2 1
    1022 
    1023 3 6 9
    1024 1 2 3 4 5 6 7 8 9 10
    1025 10 9 8 7 6 5 4 3 2 1 0
    1026 2 4 6 8 10
    1027 2.1 3.8 5.5 7.2 8.9
    1028 10 8 6 4 2 0
    1029 12.1 10.4 8.7 7. 5.3 3.6
    1030 0 -5 1 -4 2 -3 3 -2 4 -1
    1031 0 -5 1 -6 2 -7 3 -8 4 -9
    1032 0 -5 1 -3 2 -1 3 1 4 3
    1033 0 -5 1 -7 2 -9 3 -11 4 -13
    1034 0 -5 1 -4 2 -3 3 -2 4 -1
    1035 0 -5 1 -6 2 -7 3 -8 4 -9
    1036 0 -5 1 -3 2 -1 3 1 4 3
    1037 0 -5 1 -7 2 -9 3 -11 4 -13
    1038 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
    1039 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
    1040 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
    1041 \end{cfa}
    1042 \end{tabular}
    1043 \caption{Loop Control Examples}
    1044 \label{f:LoopControlExamples}
    1045 \end{figure}
     1023  case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§
     1024        ...
     1025}
     1026\end{cfa}
     1027Lists of subranges are also allowed.
     1028\begin{cfa}
     1029case ®1~5, 12~21, 35~42®:
     1030\end{cfa}
     1031
    10461032
    10471033% for ()  => for ( ;; )
     
    10541040
    10551041
    1056 \subsection{Loop Control}
    1057 
    1058 The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}).
    1059 \begin{itemize}
    1060 \item
    1061 The loop index is polymorphic in the type of the comparison value N (when the start value is implicit) or the start value M.
    1062 \item
    1063 An empty conditional implies comparison value of ©1© (true).
    1064 \item
    1065 A comparison N is implicit up-to exclusive range [0,N©®)®©.
    1066 \item
    1067 A comparison ©=© N is implicit up-to inclusive range [0,N©®]®©.
    1068 \item
    1069 The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N©®)®©.
    1070 \item
    1071 The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N©®]®©.
    1072 \item
    1073 The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M©®)®©.
    1074 \item
    1075 The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M©®]®©.
    1076 \item
    1077 ©0© is the implicit start value;
    1078 \item
    1079 ©1© is the implicit increment value.
    1080 \item
    1081 The up-to range uses operator ©+=© for increment;
    1082 \item
    1083 The down-to range uses operator ©-=© for decrement.
    1084 \item
    1085 ©@© means put nothing in this field.
    1086 \item
    1087 ©:© means start another index.
    1088 \end{itemize}
    1089 
    1090 
    10911042%\subsection{\texorpdfstring{Labelled \protect\lstinline@continue@ / \protect\lstinline@break@}{Labelled continue / break}}
    10921043\subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}}
     
    10981049for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement.
    10991050\VRef[Figure]{f:MultiLevelExit} shows ©continue© and ©break© indicating the specific control structure, and the corresponding C program using only ©goto© and labels.
    1100 The innermost loop has 8 exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s.
     1051The innermost loop has 7 exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s.
    11011052
    11021053\begin{figure}
    1103 \centering
    1104 \begin{lrbox}{\myboxA}
    1105 \begin{cfa}[tabsize=3]
    1106 ®Compound:® {
    1107         ®Try:® try {
    1108                 ®For:® for ( ... ) {
    1109                         ®While:® while ( ... ) {
    1110                                 ®Do:® do {
    1111                                         ®If:® if ( ... ) {
    1112                                                 ®Switch:® switch ( ... ) {
    1113                                                         case 3:
    1114                                                                 ®break Compound®;
    1115                                                                 ®break Try®;
    1116                                                                 ®break For®;      /* or */  ®continue For®;
    1117                                                                 ®break While®;  /* or */  ®continue While®;
    1118                                                                 ®break Do®;      /* or */  ®continue Do®;
    1119                                                                 ®break If®;
    1120                                                                 ®break Switch®;
    1121                                                         } // switch
    1122                                                 } else {
    1123                                                         ... ®break If®; ...     // terminate if
    1124                                                 } // if
    1125                                 } while ( ... ); // do
    1126                         } // while
    1127                 } // for
    1128         } ®finally® { // always executed
    1129         } // try
     1054\begin{tabular}{@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}}
     1055\multicolumn{1}{@{\hspace{\parindentlnth}}c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}   & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}}      \\
     1056\begin{cfa}
     1057®LC:® {
     1058        ... §declarations§ ...
     1059        ®LS:® switch ( ... ) {
     1060          case 3:
     1061                ®LIF:® if ( ... ) {
     1062                        ®LF:® for ( ... ) {
     1063                                ®LW:® while ( ... ) {
     1064                                        ... break ®LC®; ...
     1065                                        ... break ®LS®; ...
     1066                                        ... break ®LIF®; ...
     1067                                        ... continue ®LF;® ...
     1068                                        ... break ®LF®; ...
     1069                                        ... continue ®LW®; ...
     1070                                        ... break ®LW®; ...
     1071                                } // while
     1072                        } // for
     1073                } else {
     1074                        ... break ®LIF®; ...
     1075                } // if
     1076        } // switch
    11301077} // compound
    11311078\end{cfa}
    1132 \end{lrbox}
    1133 
    1134 \begin{lrbox}{\myboxB}
    1135 \begin{cfa}[tabsize=3]
     1079&
     1080\begin{cfa}
    11361081{
    1137 
    1138                 ®ForC:® for ( ... ) {
    1139                         ®WhileC:® while ( ... ) {
    1140                                 ®DoC:® do {
    1141                                         if ( ... ) {
    1142                                                 switch ( ... ) {
    1143                                                         case 3:
    1144                                                                 ®goto Compound®;
    1145                                                                 ®goto Try®;
    1146                                                                 ®goto ForB®;      /* or */  ®goto ForC®;
    1147                                                                 ®goto WhileB®;  /* or */  ®goto WhileC®;
    1148                                                                 ®goto DoB®;      /* or */  ®goto DoC®;
    1149                                                                 ®goto If®;
    1150                                                                 ®goto Switch®;
    1151                                                         } ®Switch:® ;
    1152                                                 } else {
    1153                                                         ... ®goto If®; ...      // terminate if
    1154                                                 } ®If:®;
    1155                                 } while ( ... ); ®DoB:® ;
    1156                         } ®WhileB:® ;
    1157                 } ®ForB:® ;
    1158 
    1159 
    1160 } ®Compound:® ;
    1161 \end{cfa}
    1162 \end{lrbox}
    1163 
    1164 \subfloat[\CFA]{\label{f:CFibonacci}\usebox\myboxA}
    1165 \hspace{2pt}
    1166 \vrule
    1167 \hspace{2pt}
    1168 \subfloat[C]{\label{f:CFAFibonacciGen}\usebox\myboxB}
     1082        ... §declarations§ ...
     1083        switch ( ... ) {
     1084          case 3:
     1085                if ( ... ) {
     1086                        for ( ... ) {
     1087                                while ( ... ) {
     1088                                        ... goto ®LC®; ...
     1089                                        ... goto ®LS®; ...
     1090                                        ... goto ®LIF®; ...
     1091                                        ... goto ®LFC®; ...
     1092                                        ... goto ®LFB®; ...
     1093                                        ... goto ®LWC®; ...
     1094                                        ... goto ®LWB®; ...
     1095                                  ®LWC®: ; } ®LWB:® ;
     1096                          ®LFC:® ; } ®LFB:® ;
     1097                } else {
     1098                        ... goto ®LIF®; ...
     1099                } ®L3:® ;
     1100        } ®LS:® ;
     1101} ®LC:® ;
     1102\end{cfa}
     1103&
     1104\begin{cfa}
     1105
     1106
     1107
     1108
     1109
     1110
     1111
     1112// terminate compound
     1113// terminate switch
     1114// terminate if
     1115// continue loop
     1116// terminate loop
     1117// continue loop
     1118// terminate loop
     1119
     1120
     1121
     1122// terminate if
     1123
     1124
     1125
     1126\end{cfa}
     1127\end{tabular}
    11691128\caption{Multi-level Exit}
    11701129\label{f:MultiLevelExit}
     
    14211380try {
    14221381        f(...);
    1423 } catch( E e ; §boolean-predicate§ ) {          §\C{// termination handler}§
     1382} catch( E e ; §boolean-predicate§ ) {          §\C[8cm]{// termination handler}§
    14241383        // recover and continue
    1425 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}§
     1384} catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}\CRT§
    14261385        // repair and return
    14271386} finally {
     
    34863445For implicit formatted input, the common case is reading a sequence of values separated by whitespace, where the type of an input constant must match with the type of the input variable.
    34873446\begin{cquote}
    3488 \begin{lrbox}{\myboxA}
     3447\begin{lrbox}{\LstBox}
    34893448\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    34903449int x;   double y   char z;
     
    34923451\end{lrbox}
    34933452\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{3em}}l@{}}
    3494 \multicolumn{1}{@{}l@{}}{\usebox\myboxA} \\
     3453\multicolumn{1}{@{}l@{}}{\usebox\LstBox} \\
    34953454\multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CC}}       & \multicolumn{1}{c}{\textbf{Python}}   \\
    34963455\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     
    65886547hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}).
    65896548All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling.
    6590 This approach is different from \Index*[C++]{\CC{}} where the name-mangling issue is handled internally in C header-files through checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"© qualifiers.
     6549For \Index*[C++]{\CC{}}, the name-mangling issue is often handled internally in many C header-files through checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"© qualifiers.
    65916550
    65926551
     
    66026561The storage-management routines extend their C equivalents by overloading, alternate names, providing shallow type-safety, and removing the need to specify the allocation size for non-array types.
    66036562
    6604 C storage management provides the following capabilities:
     6563Storage management provides the following capabilities:
    66056564\begin{description}
    6606 \item[filled]
    6607 after allocation with a specified character or value.
     6565\item[fill]
     6566after allocation the storage is filled with a specified character.
    66086567\item[resize]
    6609 an existing allocation to decreased or increased its size.
    6610 In either case, new storage may or may not be allocated and, if there is a new allocation, as much data from the existing allocation is copied into the new allocation.
     6568an existing allocation is decreased or increased in size.
     6569In either case, new storage may or may not be allocated and, if there is a new allocation, as much data from the existing allocation is copied.
    66116570For an increase in storage size, new storage after the copied data may be filled.
    6612 \item[align]
    6613 an allocation on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes.
     6571\item[alignment]
     6572an allocation starts on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes.
    66146573\item[array]
    66156574the allocation size is scaled to the specified number of array elements.
    66166575An array may be filled, resized, or aligned.
    66176576\end{description}
    6618 \VRef[Table]{t:AllocationVersusCapabilities} shows allocation routines supporting different combinations of storage-management capabilities.
    6619 \begin{table}
    6620 \centering
    6621 \begin{minipage}{0.75\textwidth}
    6622 \begin{tabular}{@{}r|l|l|l|l|l@{}}
     6577The table shows allocation routines supporting different combinations of storage-management capabilities:
     6578\begin{center}
     6579\begin{tabular}{@{}r|r|l|l|l|l@{}}
    66236580\multicolumn{1}{c}{}&           & \multicolumn{1}{c|}{fill}     & resize        & alignment     & array \\
    66246581\hline
    66256582C               & ©malloc©                      & no                    & no            & no            & no    \\
    66266583                & ©calloc©                      & yes (0 only)  & no            & no            & yes   \\
    6627                 & ©realloc©                     & copy                  & yes           & no            & no    \\
     6584                & ©realloc©                     & no/copy               & yes           & no            & no    \\
    66286585                & ©memalign©            & no                    & no            & yes           & no    \\
    6629                 & ©aligned_alloc©\footnote{Same as ©memalign© but size is an integral multiple of alignment, which is universally ignored.}
    6630                                                         & no                    & no            & yes           & no    \\
    66316586                & ©posix_memalign©      & no                    & no            & yes           & no    \\
    6632                 & ©valloc©                      & no                    & no            & yes (page size)& no   \\
    6633                 & ©pvalloc©\footnote{Same as ©valloc© but rounds size to multiple of page size.}
    6634                                                         & no                    & no            & yes (page size)& no   \\
    66356587\hline
    6636 \CFA    & ©cmemalign©           & yes (0 only)  & no            & yes           & yes   \\
    6637                 & ©realloc©                     & copy                  & yes           & yes           & no    \\
    6638                 & ©alloc©                       & no                    & yes           & no            & yes   \\
    6639                 & ©alloc_set©           & yes                   & yes           & no            & yes   \\
    6640                 & ©alloc_align©         & no                    & yes           & yes           & yes   \\
    6641                 & ©alloc_align_set©     & yes                   & yes           & yes           & yes   \\
     6588C11             & ©aligned_alloc©       & no                    & no            & yes           & no    \\
     6589\hline
     6590\CFA    & ©alloc©                       & no/copy/yes   & no/yes        & no            & yes   \\
     6591                & ©align_alloc©         & no/yes                & no            & yes           & yes   \\
    66426592\end{tabular}
    6643 \end{minipage}
    6644 \caption{Allocation Routines versus Storage-Management Capabilities}
    6645 \label{t:AllocationVersusCapabilities}
    6646 \end{table}
    6647 
    6648 \CFA memory management extends the type safety of all allocations by using the type of the left-hand-side type to determine the allocation size and return a matching type for the new storage.
    6649 Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in
    6650 \begin{cfa}
    6651 int * ip = (int *)malloc( sizeof(int) );                §\C{// C}§
    6652 int * ip = malloc();                                                    §\C{// \CFA type-safe version of C malloc}§
    6653 int * ip = alloc();                                                             §\C{// \CFA type-safe uniform alloc}§
    6654 \end{cfa}
    6655 the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©.
    6656 
    6657 \CFA memory management extends allocation safety by implicitly honouring all alignment requirements, \eg in
    6658 \begin{cfa}
    6659 struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment
    6660 S * sp = malloc();                                                              §\C{// honour type alignment}§
    6661 \end{cfa}
    6662 the storage allocation is implicitly aligned to 128 rather than the default 16.
    6663 The alignment check is performed at compile time so there is no runtime cost.
    6664 
    6665 \CFA memory management extends the resize capability with the notion of \newterm{sticky properties}.
    6666 Hence, initial allocation capabilities are remembered and maintained when resize requires copying.
    6667 For example, an initial alignment and fill capability are preserved during a resize copy so the copy has the same alignment and extended storage is filled.
    6668 Without sticky properties it is dangerous to use ©realloc©, resulting in an idiom of manually performing the reallocation to maintain correctness.
    6669 \begin{cfa}
    6670 
    6671 \end{cfa}
    6672 
    6673 \CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in
    6674 \begin{cfa}
    6675 struct S { int i; };                                                    §\C{// cache-line aglinment}§
    6676 void ?{}( S & s, int i ) { s.i = i; }
    6677 // assume ?|? operator for printing an S
    6678 
    6679 S & sp = *®new®( 3 );                                                   §\C{// call constructor after allocation}§
    6680 sout | sp.i;
    6681 ®delete®( &sp );
    6682 
    6683 S * spa = ®anew®( 10, 5 );                                              §\C{// allocate array and initialize each array element}§
    6684 for ( i; 10 ) sout | spa[i] | nonl;
    6685 sout | nl;
    6686 ®adelete®( 10, spa );
    6687 \end{cfa}
    6688 Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor.
    6689 Note, the matching deallocation routines ©delete©/©adelete©.
     6593\end{center}
     6594It is impossible to resize with alignment because the underlying ©realloc© allocates storage if more space is needed, and it does not honour alignment from the original allocation.
    66906595
    66916596\leavevmode
    66926597\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     6598// C unsafe allocation
    66936599extern "C" {
    6694         // C unsafe allocation
    6695         void * malloc( size_t size );§\indexc{malloc}§
    6696         void * calloc( size_t dim, size_t size );§\indexc{calloc}§
    6697         void * realloc( void * ptr, size_t size );§\indexc{realloc}§
    6698         void * memalign( size_t align, size_t size );§\indexc{memalign}§
    6699         void * aligned_alloc( size_t align, size_t size );§\indexc{aligned_alloc}§
    6700         int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§
    6701         void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );§\indexc{cmemalign}§ // CFA
    6702 
    6703         // C unsafe initialization/copy
    6704         void * memset( void * dest, int c, size_t size );§\indexc{memset}§
    6705         void * memcpy( void * dest, const void * src, size_t size );§\indexc{memcpy}§
    6706 }
    6707 
    6708 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
     6600void * malloc( size_t size );§\indexc{memset}§
     6601void * calloc( size_t dim, size_t size );§\indexc{calloc}§
     6602void * realloc( void * ptr, size_t size );§\indexc{realloc}§
     6603void * memalign( size_t align, size_t size );§\indexc{memalign}§
     6604int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§
     6605
     6606// C unsafe initialization/copy
     6607void * memset( void * dest, int c, size_t size );
     6608void * memcpy( void * dest, const void * src, size_t size );
     6609}
    67096610
    67106611forall( dtype T | sized(T) ) {
    6711         // §\CFA§ safe equivalents, i.e., implicit size specification
     6612// §\CFA§ safe equivalents, i.e., implicit size specification
    67126613        T * malloc( void );
    67136614        T * calloc( size_t dim );
    67146615        T * realloc( T * ptr, size_t size );
    67156616        T * memalign( size_t align );
    6716         T * cmemalign( size_t align, size_t dim  );
    67176617        T * aligned_alloc( size_t align );
    67186618        int posix_memalign( T ** ptr, size_t align );
    67196619
    6720         // §\CFA§ safe general allocation, fill, resize, alignment, array
    6721         T * alloc( void );§\indexc{alloc}§                                      §\C[3.5in]{// variable, T size}§
    6722         T * alloc( size_t dim );                                                        §\C{// array[dim], T size elements}§
    6723         T * alloc( T ptr[], size_t dim );                                       §\C{// realloc array[dim], T size elements}§
    6724 
    6725         T * alloc_set( char fill );§\indexc{alloc_set}§         §\C{// variable, T size, fill bytes with value}§
    6726         T * alloc_set( T fill );                                                        §\C{// variable, T size, fill with value}§
    6727         T * alloc_set( size_t dim, char fill );                         §\C{// array[dim], T size elements, fill bytes with value}§
    6728         T * alloc_set( size_t dim, T fill );                            §\C{// array[dim], T size elements, fill elements with value}§
    6729         T * alloc_set( size_t dim, const T fill[] );            §\C{// array[dim], T size elements, fill elements with array}§
    6730         T * alloc_set( T ptr[], size_t dim, char fill );        §\C{// realloc array[dim], T size elements, fill bytes with value}§
    6731 
    6732         T * alloc_align( size_t align );                                        §\C{// aligned variable, T size}§
    6733         T * alloc_align( size_t align, size_t dim );            §\C{// aligned array[dim], T size elements}§
    6734         T * alloc_align( T ptr[], size_t align );                       §\C{// realloc new aligned array}§
    6735         T * alloc_align( T ptr[], size_t align, size_t dim ); §\C{// realloc new aligned array[dim]}§
    6736 
    6737         T * alloc_align_set( size_t align, char fill );         §\C{// aligned variable, T size, fill bytes with value}§
    6738         T * alloc_align_set( size_t align, T fill );            §\C{// aligned variable, T size, fill with value}§
    6739         T * alloc_align_set( size_t align, size_t dim, char fill ); §\C{// aligned array[dim], T size elements, fill bytes with value}§
    6740         T * alloc_align_set( size_t align, size_t dim, T fill ); §\C{// aligned array[dim], T size elements, fill elements with value}§
    6741         T * alloc_align_set( size_t align, size_t dim, const T fill[] ); §\C{// aligned array[dim], T size elements, fill elements with array}§
    6742         T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); §\C{// realloc new aligned array[dim], fill new bytes with value}§
    6743 
    6744         // §\CFA§ safe initialization/copy, i.e., implicit size specification
    6745         T * memset( T * dest, char fill );§\indexc{memset}§
     6620// §\CFA§ safe general allocation, fill, resize, array
     6621        T * alloc( void );§\indexc{alloc}§
     6622        T * alloc( char fill );
     6623        T * alloc( size_t dim );
     6624        T * alloc( size_t dim, char fill );
     6625        T * alloc( T ptr[], size_t dim );
     6626        T * alloc( T ptr[], size_t dim, char fill );
     6627
     6628// §\CFA§ safe general allocation, align, fill, array
     6629        T * align_alloc( size_t align );
     6630        T * align_alloc( size_t align, char fill );
     6631        T * align_alloc( size_t align, size_t dim );
     6632        T * align_alloc( size_t align, size_t dim, char fill );
     6633
     6634// §\CFA§ safe initialization/copy, i.e., implicit size specification
     6635        T * memset( T * dest, char c );§\indexc{memset}§
    67466636        T * memcpy( T * dest, const T * src );§\indexc{memcpy}§
    67476637
    6748         // §\CFA§ safe initialization/copy, i.e., implicit size specification, array types
    6749         T * amemset( T dest[], char fill, size_t dim );
     6638// §\CFA§ safe initialization/copy array
     6639        T * amemset( T dest[], char c, size_t dim );
    67506640        T * amemcpy( T dest[], const T src[], size_t dim );
    67516641}
    67526642
    6753 // §\CFA§ allocation/deallocation and constructor/destructor, non-array types
    6754 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );§\indexc{new}§
    6755 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );§\indexc{delete}§
    6756 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
     6643// §\CFA§ allocation/deallocation and constructor/destructor
     6644forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p );§\indexc{new}§
     6645forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );§\indexc{delete}§
     6646forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
    67576647  void delete( T * ptr, Params rest );
    67586648
    6759 // §\CFA§ allocation/deallocation and constructor/destructor, array types
    6760 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§
    6761 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§
    6762 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
     6649// §\CFA§ allocation/deallocation and constructor/destructor, array
     6650forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§
     6651forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§
     6652forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } )
    67636653  void adelete( size_t dim, T arr[], Params rest );
    67646654\end{cfa}
Note: See TracChangeset for help on using the changeset viewer.