Ignore:
Timestamp:
Jul 24, 2024, 7:11:32 PM (6 weeks ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
5aeb1a9
Parents:
e561551 (diff), b6923b17 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

conclude merge

File:
1 edited

Legend:

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

    re561551 ra03ed29  
    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;
     
    5151        int va[r];
    5252}
    53 
    54 
    5553\end{clang}
    5654\end{tabular}
    5755\end{cquote}
    58 However, statically initialized identifiers can not appear in constant-expression contexts, \eg @case@.
     56However, statically initialized identifiers cannot appear in constant-expression contexts, \eg @case@.
    5957Dynamically initialized identifiers may appear in initialization and array dimensions in @g++@, which allows variable-sized arrays on the stack.
    6058Again, this form of aliasing is not an enumeration.
    61 
    6259
    6360\section{C Enumeration}
     
    129126
    130127
    131 \subsection{Representation}
    132 
    133 C standard specifies 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).
     128\subsection{Implementation}
     129\label{s:CenumImplementation}
     130
     131In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerator values.
     132In 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).
     133However, type @int@ is defined as:
     134\begin{quote}
     135A ``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}
     136\end{quote}
     137Howeveer, @int@ means a 4 bytes on both 32/64-bit architectures, which does not seem like the ``natural'' size for a 64-bit architecture.
     138Whereas, @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.
     139In reality, both @gcc@ and @clang@ partially ignore this specification and type the integral size of an enumerator based its initialization.
     140\begin{cfa}
     141enum E { IMin = INT_MIN, IMax = INT_MAX,
     142                         ILMin = LONG_MIN, ILMax = LONG_MAX,
     143                         ILLMin = LLONG_MIN, ILLMax = LLONG_MAX };
     144int main() {
     145        printf( "%zd %d %d\n%zd %ld %ld\n%zd %ld %ld\n",
     146                         sizeof(IMin), IMin, IMax,
     147                         sizeof(ILMin), ILMin, ILMax,
     148                         sizeof(ILLMin), ILLMin, ILLMax );
     149}
     1504 -2147483648 2147483647
     1518 -9223372036854775808 9223372036854775807
     1528 -9223372036854775808 9223372036854775807
     153\end{cfa}
     154Hence, initialization in the range @INT_MIN@..@INT_MAX@ is 4 bytes, and outside this range is 8 bytes.
    135155
    136156\subsection{Usage}
     
    150170enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun };
    151171switch ( week ) {
    152         case Mon: case Tue: case Wed: case Thu: case Fri:
     172        case Mon ... Fri:                               $\C{// gcc case range}$
    153173                printf( "weekday\n" );
    154174        case Sat: case Sun:
    155175                printf( "weekend\n" );
    156176}
    157 for ( enum Week day = Mon; day <= Sun; day += 1 ) { // step of 1
     177for ( enum Week day = Mon; day <= Sun; day += 1 ) { $\C{// step of 1}$
    158178        printf( "day %d\n", day ); // 0-6
    159179}
     
    177197}
    178198\end{cfa}
    179 However, for typed enumerations, \see{\VRef{f:EumeratorTyping}}, this idiom fails.
    180 
    181 This idiom leads to another C idiom using an enumeration with matching companion information.
     199However, for non-integral typed enumerations, \see{\VRef{f:EumeratorTyping}}, this idiom fails.
     200
     201This idiom is used in another C idiom for matching companion information.
    182202For example, an enumeration is linked with a companion array of printable strings.
    183203\begin{cfa}
     
    196216
    197217\bigskip
    198 While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide useful enumeration features in other programming languages.
     218While C provides a true enumeration, it is restricted, has unsafe semantics, and does not provide useful enumeration features in other programming languages.
    199219
    200220\section{\CFA Polymorphism}
     
    238258\subsection{Constructor and Destructor}
    239259In \CFA, all objects are initialized by @constructors@ during its allocation, including basic types,
    240 which are initialized by constructors default-generated by a compiler.
     260which are initialized by auto-generated basic type constructors.
    241261
    242262Constructors are overloadable functions with name @?{}@, return @void@, and have at least one parameter, which is a reference
Note: See TracChangeset for help on using the changeset viewer.