-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathconst.c
333 lines (256 loc) · 8.47 KB
/
const.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
# const
*/
#include "common.h"
int* int_ptr_func_int_ptr(int *ip) {
(*ip)++;
return ip;
}
#if __STDC_VERSION__ >= 199901L
void const_array_size_argument(int is[const]) {
is[0] = 1;
int j;
/* Compilation ERROR: assignment of read-only parameter is: */
/*is = &j;*/
}
#endif
int main(void) {
int i = 0;
const int ic = 0;
/* Same. */
/*int const ic = 0*/
const int ic2 = i;
/*
BAD.
Legal and compiles without warning, but is bad since ic3 cannot get a value
unless you typecast its pointer with a warning.
consts should normally get a value at initialization time.
*/
const int ic3;
/* ERROR: assignment of read only variable ic. TODO Illegal or undefined?. */
{
const int ic = 0;
/*ic = 1;*/
}
/*
# Modify const through pointer cast
Casting a const to a non-const through a pointer is legal.
Modifying he const with the pointer is undefined behavior (C99 6.7.3.5).
For this reason, const it does not generate compile time constant expressions (C99 6.6):
the undefined behavior could be to change the value of the const.
In particular, existing implementaions may or may not put `const` in read only memory,
so that the undefined behavior may be a page fault.
`gcc` for example puts global constants on the `.rodata` section of the elf output.
In practice however it might work for local function variables,
which are just on the stack or registers.
Many compilers raise warnings or prevent compilation of such constructs.
In C++, discarding const is illegal, and const generates compile time constants.
*/
{
const int ic = 0;
#ifdef UNDEFINED_BEHAVIOUR
/*
WARN: initialization discards const qualifier from pointer type.
Likely to work since local variable.
*/
/*
int *ip = ⁣
*ip = 1;
assert(ic == 1);
*/
#endif
}
/*
# const pointers
There are 3 types of const pointers:
- `const X *` or `X const *` : cannot change the data of the thing pointer points to
- `X * const` : cannot change which thing the pointer points to
- `const X * const` or `X const * const` : both of the above
*/
{
/* const int * */
/* int const * */
{
int i = 0;
int j = 0;
const int *cip = &i;
int const *icp = &i;
/* Can change which opbject it points to. */
cip = &j;
icp = &j;
/* ERROR: const * prevents from changing the data of the object pointed to */
/**cip = 2;*/
/**icp = 2;*/
}
/* int * const */
{
int i = 0;
int j = 0;
int *const ipc = &i;
*ipc = 1;
assert(i == 1);
/* ERROR: cannot change what is being pointed to */
/*ipc = &j;*/
/*
# Single line declaration of multiple const pointers
Just like `*` must be repeated once per variable, `*const` must also be repeated.
*/
{
int i = 0;
int *const ipc2, *const ipc3, *ipcBad;
/* ^^^^^^ */
/* must repeat the `iconst` for each variable declared! */
i = 0;
ipcBad = &i;
*ipcBad = 1;
}
{
const int ic = 0;
/* WARN */
/* Initialization discards const. */
/* It would be possible to change the value of the const. */
/*int *const ipc = ⁣*/
/* BAD: we changed the value! */
/**ipc = 1;*/
}
}
const int* const cipc = ⁣
const int cis2[2] = { 1, 2 };
/* ERROR */
/*cis2[0] = 1;*/
/*
# const pointers to pointers
There are 7 possibilities at level 2 already!
To not mix up:
- const always applies to the pointer at its left.
- if there is no such pointer, it applies to the data
*/
{
int const ** icpp;
int * const * ipcp;
int ** const ippc;
int const * const * icpcp;
int const ** const icppc;
int * const * const ipcpc;
int const * const * const icpcpc;
}
/*
# add const is not possible
`const int * = int *` is possible, but it is not possible to do `const int ** = int **`.
This is a bit counter-intuitive at first since:
- we feel like we are adding a `const` qualifier increases restrictoins.
However, when it may lead to const modification, it is not acceptable.
- the phenomenon only appears at level 2 pointers to pointers, not with simple pointers
Similar considerations apply to the `volatile` qualifier.
http://stackoverflow.com/questions/1468148/initialization-between-types-const-int-const-and-int-is-not-allowed-why
*/
{
/* If `const int ** = int **` were possible then we could change constants. */
{
int* p = 0;
/* (1) THIS cannot be done: `const int ** = int **` */
/*int const** pp = &p; */
/*int const c = 123;*/
/* OK, &c is int const*, and *p is int const* lvalue. p points to c now! */
/**pp = &c;*/
/* OK: p is not const. We changed c! */
/**p = 666;*/
}
/* The problem only arises in multidimensional cases. */
/* Here it is impossible to change a const. */
{
int i = 0;
int const * p = &i;
}
}
}
/*
# const struct
*/
{
/*
The entire struct is const.
Members of a const struct cannot be modified.
*/
{
struct s { int i; };
const struct s s = { 1 };
/* ERROR */
/*s.i = 2;*/
}
/* Single members can be declared const. */
{
struct s {
int i;
const int j;
};
struct s s = { 0, 1 };
s.i = 2;
/* ERROR */
/*s.j = 2;*/
}
/* Entire structs cannot be declared const. */
{
/* WARN: useless type qualifier */
/*const struct s { int i; };*/
}
}
/*
# Return const from func
- <http://stackoverflow.com/questions/8716330/purpose-of-returning-by-const-value>
- <http://stackoverflow.com/questions/8406898/benefits-of-using-const-with-scalar-type-e-g-const-double-or-const-int?lq=1>
*/
{
/*
USELESS
There seem to be no noticeable effect of returning const for non pointer scalars.
*/
{
/*int_func();*/
/*const_int_func();*/
}
/*
For pointer types this has a noticeable effect
*/
{
/* OK */
{
int i = 0;
(*int_ptr_func_int_ptr(&i)) = 2;
assert(i == 2);
}
/* ERROR */
{
int i = 0;
/*(*const_int_ptr_func_int_ptr(&i)) = 2;*/
/*assert(i == 2);*/
}
}
/*
For structs this also has a noticeable effect.
In C++ however there can be noticeable effect
because the returned object may have a non-const function that changes it
so that the following is possible:
objFunc().nonConst();
but the following would not be:
constObjFunc().nonConst();
*/
{
/*struct_func*/
/*const_struct_func*/
}
}
#if __STDC_VERSION__ >= 199901L
/*
# const in array size function argument
Same effect as declaring `int *const`, but without cast to pointer.
*/
{
int is2[2];
is2[0] = 0;
const_array_size_argument(is2);
assert(is2[0] == 1);
}
#endif
return EXIT_SUCCESS;
}