Ignore:
Timestamp:
Jul 24, 2024, 11:24:08 AM (3 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
d1276f8
Parents:
46651fb
Message:

proofread last push of CFA enumerations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/background.tex

    r46651fb r10a99d87  
    66The following discussion covers C enumerations.
    77
    8 As discussed in \VRef{s:Aliasing}, it is common for C programmers to ``believe'' there are three equivalent forms of named constants.
     8As mentioned in \VRef{s:Aliasing}, it is common for C programmers to ``believe'' there are three equivalent forms of named constants.
    99\begin{clang}
    1010#define Mon 0
     
    3333C can simulate the aliasing @const@ declarations \see{\VRef{s:Aliasing}}, with static and dynamic initialization.
    3434\begin{cquote}
    35 \begin{tabular}{@{}l@{}l@{}}
    36 \multicolumn{1}{@{}c@{}}{\textbf{static initialization}} &  \multicolumn{1}{c@{}}{\textbf{dynamic intialization}} \\
     35\begin{tabular}{@{}ll@{}}
     36\multicolumn{1}{@{}c}{\textbf{static initialization}} &  \multicolumn{1}{c@{}}{\textbf{dynamic intialization}} \\
    3737\begin{clang}
    3838static const int one = 0 + 1;
     
    5656\end{tabular}
    5757\end{cquote}
    58 However, statically initialized identifiers can not appear in constant-expression contexts, \eg @case@.
     58However, statically initialized identifiers cannot appear in constant-expression contexts, \eg @case@.
    5959Dynamically initialized identifiers may appear in initialization and array dimensions in @g++@, which allows variable-sized arrays on the stack.
    6060Again, this form of aliasing is not an enumeration.
     
    130130
    131131\subsection{Implementation}
     132\label{s:CenumImplementation}
    132133
    133134In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerator values.
    134 In practice, C uses @int@ as the underlying type for enumeration variables, because of the restriction to integral constants, which have type @int@ (unless qualified with a size suffix).
     135In practice, C defines @int@~\cite[\S~6.4.4.3]{C11} as the underlying type for enumeration variables, restricting initialization to integral constants, which have type @int@ (unless qualified with a size suffix).
     136However, type @int@ is defined as:
     137\begin{quote}
     138A ``plain'' @int@ object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range @INT_MIN@ to @INT_MAX@ as defined in the header @<limits.h>@).~\cite[\S~6.2.5(5)]{C11}
     139\end{quote}
     140Howeveer, @int@ means a 4 bytes on both 32/64-bit architectures, which does not seem like the ``natural'' size for a 64-bit architecture.
     141Whereas, @long int@ means 4 bytes on a 32-bit and 8 bytes on 64-bit architectures, and @long long int@ means 8 bytes on both 32/64-bit architectures, where 64-bit operations are simulated on 32-bit architectures.
     142In reality, both @gcc@ and @clang@ partially ignore this specification and type the integral size of an enumerator based its initialization.
     143\begin{cfa}
     144enum E { IMin = INT_MIN, IMax = INT_MAX,
     145                         ILMin = LONG_MIN, ILMax = LONG_MAX,
     146                         ILLMin = LLONG_MIN, ILLMax = LLONG_MAX };
     147int main() {
     148        printf( "%zd %d %d\n%zd %ld %ld\n%zd %ld %ld\n",
     149                         sizeof(IMin), IMin, IMax,
     150                         sizeof(ILMin), ILMin, ILMax,
     151                         sizeof(ILLMin), ILLMin, ILLMax );
     152}
     1534 -2147483648 2147483647
     1548 -9223372036854775808 9223372036854775807
     1558 -9223372036854775808 9223372036854775807
     156\end{cfa}
     157Hence, initialization in the range @INT_MIN@..@INT_MAX@ is 4 bytes, and outside this range is 8 bytes.
    135158
    136159
     
    151174enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun };
    152175switch ( week ) {
    153         case Mon: case Tue: case Wed: case Thu: case Fri:
     176        case Mon ... Fri:                               $\C{// gcc case range}$
    154177                printf( "weekday\n" );
    155178        case Sat: case Sun:
    156179                printf( "weekend\n" );
    157180}
    158 for ( enum Week day = Mon; day <= Sun; day += 1 ) { // step of 1
     181for ( enum Week day = Mon; day <= Sun; day += 1 ) { $\C{// step of 1}$
    159182        printf( "day %d\n", day ); // 0-6
    160183}
     
    178201}
    179202\end{cfa}
    180 However, for typed enumerations, \see{\VRef{f:EumeratorTyping}}, this idiom fails.
    181 
    182 This idiom leads to another C idiom using an enumeration with matching companion information.
     203However, for non-integral typed enumerations, \see{\VRef{f:EumeratorTyping}}, this idiom fails.
     204
     205This idiom is used in another C idiom for matching companion information.
    183206For example, an enumeration is linked with a companion array of printable strings.
    184207\begin{cfa}
     
    197220
    198221\bigskip
    199 While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide useful enumeration features in other programming languages.
     222While C provides a true enumeration, it is restricted, has unsafe semantics, and does not provide useful enumeration features in other programming languages.
Note: See TracChangeset for help on using the changeset viewer.