Changeset a03ed29 for doc/theses/jiada_liang_MMath/background.tex
- Timestamp:
- Jul 24, 2024, 7:11:32 PM (6 weeks ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/background.tex
re561551 ra03ed29 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; … … 51 51 int va[r]; 52 52 } 53 54 55 53 \end{clang} 56 54 \end{tabular} 57 55 \end{cquote} 58 However, statically initialized identifiers can 56 However, statically initialized identifiers cannot appear in constant-expression contexts, \eg @case@. 59 57 Dynamically initialized identifiers may appear in initialization and array dimensions in @g++@, which allows variable-sized arrays on the stack. 60 58 Again, this form of aliasing is not an enumeration. 61 62 59 63 60 \section{C Enumeration} … … 129 126 130 127 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 131 In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerator values. 132 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). 133 However, type @int@ is defined as: 134 \begin{quote} 135 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} 136 \end{quote} 137 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. 138 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. 139 In reality, both @gcc@ and @clang@ partially ignore this specification and type the integral size of an enumerator based its initialization. 140 \begin{cfa} 141 enum E { IMin = INT_MIN, IMax = INT_MAX, 142 ILMin = LONG_MIN, ILMax = LONG_MAX, 143 ILLMin = LLONG_MIN, ILLMax = LLONG_MAX }; 144 int 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 } 150 4 -2147483648 2147483647 151 8 -9223372036854775808 9223372036854775807 152 8 -9223372036854775808 9223372036854775807 153 \end{cfa} 154 Hence, initialization in the range @INT_MIN@..@INT_MAX@ is 4 bytes, and outside this range is 8 bytes. 135 155 136 156 \subsection{Usage} … … 150 170 enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; 151 171 switch ( week ) { 152 case Mon : case Tue: case Wed: case Thu: case Fri:172 case Mon ... Fri: $\C{// gcc case range}$ 153 173 printf( "weekday\n" ); 154 174 case Sat: case Sun: 155 175 printf( "weekend\n" ); 156 176 } 157 for ( enum Week day = Mon; day <= Sun; day += 1 ) { // step of 1177 for ( enum Week day = Mon; day <= Sun; day += 1 ) { $\C{// step of 1}$ 158 178 printf( "day %d\n", day ); // 0-6 159 179 } … … 177 197 } 178 198 \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 withmatching companion information.199 However, for non-integral typed enumerations, \see{\VRef{f:EumeratorTyping}}, this idiom fails. 200 201 This idiom is used in another C idiom for matching companion information. 182 202 For example, an enumeration is linked with a companion array of printable strings. 183 203 \begin{cfa} … … 196 216 197 217 \bigskip 198 While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide useful enumeration features in other programming languages.218 While C provides a true enumeration, it is restricted, has unsafe semantics, and does not provide useful enumeration features in other programming languages. 199 219 200 220 \section{\CFA Polymorphism} … … 238 258 \subsection{Constructor and Destructor} 239 259 In \CFA, all objects are initialized by @constructors@ during its allocation, including basic types, 240 which are initialized by constructors default-generated by a compiler.260 which are initialized by auto-generated basic type constructors. 241 261 242 262 Constructors 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.