Changeset 10a99d87 for doc/theses/jiada_liang_MMath/background.tex
- Timestamp:
- Jul 24, 2024, 11:24:08 AM (3 days ago)
- Branches:
- master
- Children:
- d1276f8
- Parents:
- 46651fb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/background.tex
r46651fb r10a99d87 6 6 The following discussion covers C enumerations. 7 7 8 As discussed in \VRef{s:Aliasing}, it is common for C programmers to ``believe'' there are three equivalent forms of named constants.8 As mentioned in \VRef{s:Aliasing}, it is common for C programmers to ``believe'' there are three equivalent forms of named constants. 9 9 \begin{clang} 10 10 #define Mon 0 … … 33 33 C can simulate the aliasing @const@ declarations \see{\VRef{s:Aliasing}}, with static and dynamic initialization. 34 34 \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}} \\ 37 37 \begin{clang} 38 38 static const int one = 0 + 1; … … 56 56 \end{tabular} 57 57 \end{cquote} 58 However, statically initialized identifiers can 58 However, statically initialized identifiers cannot appear in constant-expression contexts, \eg @case@. 59 59 Dynamically initialized identifiers may appear in initialization and array dimensions in @g++@, which allows variable-sized arrays on the stack. 60 60 Again, this form of aliasing is not an enumeration. … … 130 130 131 131 \subsection{Implementation} 132 \label{s:CenumImplementation} 132 133 133 134 In 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). 135 In 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). 136 However, type @int@ is defined as: 137 \begin{quote} 138 A ``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} 140 Howeveer, @int@ means a 4 bytes on both 32/64-bit architectures, which does not seem like the ``natural'' size for a 64-bit architecture. 141 Whereas, @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. 142 In reality, both @gcc@ and @clang@ partially ignore this specification and type the integral size of an enumerator based its initialization. 143 \begin{cfa} 144 enum E { IMin = INT_MIN, IMax = INT_MAX, 145 ILMin = LONG_MIN, ILMax = LONG_MAX, 146 ILLMin = LLONG_MIN, ILLMax = LLONG_MAX }; 147 int 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 } 153 4 -2147483648 2147483647 154 8 -9223372036854775808 9223372036854775807 155 8 -9223372036854775808 9223372036854775807 156 \end{cfa} 157 Hence, initialization in the range @INT_MIN@..@INT_MAX@ is 4 bytes, and outside this range is 8 bytes. 135 158 136 159 … … 151 174 enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; 152 175 switch ( week ) { 153 case Mon : case Tue: case Wed: case Thu: case Fri:176 case Mon ... Fri: $\C{// gcc case range}$ 154 177 printf( "weekday\n" ); 155 178 case Sat: case Sun: 156 179 printf( "weekend\n" ); 157 180 } 158 for ( enum Week day = Mon; day <= Sun; day += 1 ) { // step of 1181 for ( enum Week day = Mon; day <= Sun; day += 1 ) { $\C{// step of 1}$ 159 182 printf( "day %d\n", day ); // 0-6 160 183 } … … 178 201 } 179 202 \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 withmatching companion information.203 However, for non-integral typed enumerations, \see{\VRef{f:EumeratorTyping}}, this idiom fails. 204 205 This idiom is used in another C idiom for matching companion information. 183 206 For example, an enumeration is linked with a companion array of printable strings. 184 207 \begin{cfa} … … 197 220 198 221 \bigskip 199 While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide useful enumeration features in other programming languages.222 While 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.