[7972603] | 1 | #include <stdio.h> |
---|
| 2 | #include <assert.h> |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | |
---|
| 5 | #define SHOW(x, fmt) printf( #x ": " fmt "\n", x ) |
---|
| 6 | |
---|
| 7 | #ifdef ERRS |
---|
| 8 | #define ERR(...) __VA_ARGS__ |
---|
| 9 | #else |
---|
| 10 | #define ERR(...) |
---|
| 11 | #endif |
---|
| 12 | |
---|
| 13 | // int main( int argc, const char *argv[] ) { |
---|
| 14 | // assert(argc == 2); |
---|
| 15 | // const int n = atoi(argv[1]); |
---|
| 16 | // assert(0 < n && n < 1000); |
---|
| 17 | |
---|
| 18 | // float a1[42]; |
---|
| 19 | // float a2[n]; |
---|
| 20 | // SHOW(sizeof(a1), "%zd"); |
---|
| 21 | // SHOW(sizeof(a2), "%zd"); |
---|
| 22 | |
---|
| 23 | // } |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | // SHOW(sizeof( a ), "%zd"); |
---|
| 27 | // SHOW(sizeof(&a ), "%zd"); |
---|
| 28 | // SHOW(sizeof( a[0]), "%zd"); |
---|
| 29 | // SHOW(sizeof(&a[0]), "%zd"); |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | int main() { |
---|
| 34 | |
---|
| 35 | /* |
---|
| 36 | When a programmer works with an array, C semantics provide access to a type that is different in every way from ``pointer to its first element.'' |
---|
| 37 | |
---|
| 38 | Its qualities become apparent by inspecting the declaration |
---|
| 39 | */ |
---|
| 40 | float a[10]; |
---|
| 41 | /* |
---|
| 42 | |
---|
| 43 | The inspection begins by using @sizeof@ to provide definite program semantics for the intuition of an expression's type. |
---|
| 44 | |
---|
| 45 | Assuming a target platform keeps things concrete: |
---|
| 46 | */ |
---|
| 47 | static_assert(sizeof(float)==4); // floats (array elements) are 4 bytes |
---|
| 48 | static_assert(sizeof(void*)==8); // pointers are 8 bytes |
---|
| 49 | /* |
---|
| 50 | |
---|
| 51 | Consider the sizes of expressions derived from @a@, modified by adding ``pointer to'' and ``first element'' (and including unnecessary parentheses to avoid confusion about precedence). |
---|
| 52 | */ |
---|
| 53 | static_assert(sizeof( a ) == 40); // array |
---|
| 54 | static_assert(sizeof(& a ) == 8 ); // pointer to array |
---|
| 55 | static_assert(sizeof( a[0] ) == 4 ); // first element |
---|
| 56 | static_assert(sizeof(&(a[0])) == 8 ); // pointer to first element |
---|
| 57 | /* |
---|
| 58 | That @a@ takes up 40 bytes is common reasoning for C programmers. |
---|
| 59 | Set aside for a moment the claim that this first assertion is giving information about a type. |
---|
| 60 | For now, note that an array and a pointer to its first element are, sometimes, different things. |
---|
| 61 | |
---|
| 62 | The idea that there is such a thing as a pointer to an array may be surprising. |
---|
| 63 | It is not the same thing as a pointer to the first element: |
---|
| 64 | */ |
---|
| 65 | typeof(& a ) x; // x is pointer to array |
---|
| 66 | typeof(&(a[0])) y; // y is pointer to first element |
---|
| 67 | ERR( |
---|
| 68 | x = y; // ill-typed |
---|
| 69 | y = x; // ill-typed |
---|
| 70 | ) |
---|
| 71 | /* |
---|
| 72 | The first gets |
---|
| 73 | warning: warning: assignment to `float (*)[10]' from incompatible pointer type `float *' |
---|
| 74 | and the second gets the opposite. |
---|
| 75 | */ |
---|
| 76 | |
---|
| 77 | /* |
---|
| 78 | We now refute a concern that @sizeof(a)@ is reporting on special knowledge from @a@ being an local variable, |
---|
| 79 | say that it is informing about an allocation, rather than simply a type. |
---|
| 80 | |
---|
| 81 | First, recognizing that @sizeof@ has two forms, one operating on an expression, the other on a type, we observe that the original answers are unaffected by using the type-parameterized form: |
---|
| 82 | */ |
---|
| 83 | static_assert(sizeof(typeof( a )) == 40); |
---|
| 84 | static_assert(sizeof(typeof(& a )) == 8 ); |
---|
| 85 | static_assert(sizeof(typeof( a[0] )) == 4 ); |
---|
| 86 | static_assert(sizeof(typeof(&(a[0]))) == 8 ); |
---|
| 87 | |
---|
| 88 | /* |
---|
| 89 | Finally, the same sizing is reported when there is no allocation at all, and we launch the analysis instead from the pointer-to-array type. |
---|
| 90 | */ |
---|
| 91 | void f( float (*pa)[10] ) { |
---|
| 92 | static_assert(sizeof( *pa ) == 40); // array |
---|
| 93 | static_assert(sizeof( pa ) == 8 ); // pointer to array |
---|
| 94 | static_assert(sizeof( (*pa)[0] ) == 4 ); // first element |
---|
| 95 | static_assert(sizeof(&((*pa)[0])) == 8 ); // pointer to first element |
---|
| 96 | } |
---|
| 97 | f( & a ); |
---|
| 98 | |
---|
| 99 | /* |
---|
| 100 | So, in spite of considerable programmer success enabled by an understanding that |
---|
| 101 | an array just a pointer to its first element (revisited TODO pointer decay), |
---|
| 102 | this understanding is simplistic. |
---|
| 103 | */ |
---|
| 104 | |
---|
| 105 | /* |
---|
| 106 | A shortened form for declaring local variables exists, provided that length information is given in the initializer: |
---|
| 107 | */ |
---|
| 108 | float fs[] = {3.14, 1.707}; |
---|
| 109 | char cs[] = "hello"; |
---|
| 110 | |
---|
| 111 | static_assert( sizeof(fs) == 2 * sizeof(float) ); |
---|
| 112 | static_assert( sizeof(cs) == 6 * sizeof(char) ); // 5 letters + 1 null terminator |
---|
| 113 | |
---|
| 114 | /* |
---|
| 115 | In these declarations, the resulting types are both arrays, but their lengths are inferred. |
---|
| 116 | */ |
---|
| 117 | |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | void syntaxReferenceCheck(void) { |
---|
| 122 | // $\rightarrow$ & (base element) |
---|
| 123 | // & @float@ |
---|
| 124 | // & @float x;@ |
---|
| 125 | // & @[ float ]@ |
---|
| 126 | // & @[ float ]@ |
---|
| 127 | float x0; |
---|
| 128 | |
---|
| 129 | // $\rightarrow$ & pointer |
---|
| 130 | // & @float *@ |
---|
| 131 | // & @float * x;@ |
---|
| 132 | // & @[ * float ]@ |
---|
| 133 | // & @[ * float ]@ |
---|
| 134 | float * x1; |
---|
| 135 | |
---|
| 136 | // $\rightarrow$ & array |
---|
| 137 | // & @float[10]@ |
---|
| 138 | // & @float x[10];@ |
---|
| 139 | // & @[ [10] float ]@ |
---|
| 140 | // & @[ array(float, 10) ]@ |
---|
| 141 | float x2[10]; |
---|
| 142 | |
---|
| 143 | typeof(float[10]) x2b; |
---|
| 144 | |
---|
| 145 | // & array of pointers |
---|
| 146 | // & @(float*)[10]@ |
---|
| 147 | // & @float *x[10];@ |
---|
| 148 | // & @[ [10] * float ]@ |
---|
| 149 | // & @[ array(*float, 10) ]@ |
---|
| 150 | float *x3[10]; |
---|
| 151 | // (float *)x3a[10]; NO |
---|
| 152 | |
---|
| 153 | // $\rightarrow$ & pointer to array |
---|
| 154 | // & @float(*)[10]@ |
---|
| 155 | // & @float (*x)[10];@ |
---|
| 156 | // & @[ * [10] float ]@ |
---|
| 157 | // & @[ * array(float, 10) ]@ |
---|
| 158 | float (*x4)[10]; |
---|
| 159 | |
---|
| 160 | // & pointer to array |
---|
| 161 | // & @(float*)(*)[10]@ |
---|
| 162 | // & @float *(*x)[10];@ |
---|
| 163 | // & @[ * [10] * float ]@ |
---|
| 164 | // & @[ * array(*float, 10) ]@ |
---|
| 165 | float *(*x5)[10]; |
---|
| 166 | x5 = (float*(*)[10]) x4; |
---|
| 167 | // x5 = (float(*)[10]) x4; // wrong target type; meta test suggesting above cast uses correct type |
---|
| 168 | |
---|
| 169 | // [here] |
---|
| 170 | // const |
---|
| 171 | |
---|
| 172 | // [later] |
---|
| 173 | // static |
---|
| 174 | // star as dimension |
---|
| 175 | // under pointer decay: int p1[const 3] being int const *p1 |
---|
| 176 | |
---|
| 177 | const float * y1; |
---|
| 178 | float const * y2; |
---|
| 179 | float * const y3; |
---|
| 180 | |
---|
| 181 | y1 = 0; |
---|
| 182 | y2 = 0; |
---|
| 183 | // y3 = 0; // bad |
---|
| 184 | |
---|
| 185 | // *y1 = 3.14; // bad |
---|
| 186 | // *y2 = 3.14; // bad |
---|
| 187 | *y3 = 3.14; |
---|
| 188 | |
---|
| 189 | const float z1 = 1.414; |
---|
| 190 | float const z2 = 1.414; |
---|
| 191 | |
---|
| 192 | // z1 = 3.14; // bad |
---|
| 193 | // z2 = 3.14; // bad |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | #define T float |
---|
| 199 | void stx2() { const T x[10]; |
---|
| 200 | // x[5] = 3.14; // bad |
---|
| 201 | } |
---|
| 202 | void stx3() { T const x[10]; |
---|
| 203 | // x[5] = 3.14; // bad |
---|
| 204 | } |
---|