-
Notifications
You must be signed in to change notification settings - Fork 0
/
3darr.c
313 lines (301 loc) · 8.96 KB
/
3darr.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
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
/**
* @file 3darr.c
* Code comprising the `3darr` program.
* Allocates a 3D array with dimensions specified by the arguments, populates it
* with unique values, and prints it.
* Unless specified otherwise, all functions have the implicit precondition of
* all their arguments being defined. Unless specified otherwise, all functions
* taking pointers have the implicit precondition that these pointers are valid,
* and their pointees, if they are pointers, also satisfy the property the
* argument itself satisfies. If an effect is not qualified by "may" or does not
* express a mere possibility, it is guaranteed. A pointer/array being nul-terminated
* implies it is a valid pointer.
*/
/**
* Element of our array.
* The fact that this is a unsigned long is relied on.
*/
typedef unsigned long elem;
/**
* Report number of successful allocations.
* @param allocs number of allocations to report.
*
* @return if printing was successful.
*
* **Effects**: Prints to stdout, may print to stderr.
*/
static bool print_allocs(size_t allocs) {
if (printf("successfully allocated %zu times\n", allocs) < 0) {
perror("value output");
return false;
}
return true;
}
/**
* Parse argument to `size_t`.
*
* @param arg argument string to be processed.
* @param name argument name to be printed.
*
* @return parsed argument as `size_t`.
*
* @pre
* - `arg` is nul-terminated.
* - `name` is nul-terminated.
*
* **Effects**: may exit program, may print to stderr.
*/
static size_t get_arg_size_t(char *arg, char *name) {
char *argptrcpy = arg;
while (isspace(*argptrcpy))
argptrcpy++;
if (*argptrcpy == '-') {
fprintf(stderr, "argument %s must be positive\n", name);
exit(EXIT_FAILURE);
}
char *endptr;
errno = 0;
uintmax_t val = strtoumax(arg, &endptr, 10);
if (arg[0] == '\0' || *endptr != '\0') {
fprintf(stderr, "failed to parse argument %s\n", name);
exit(EXIT_FAILURE);
}
if (errno == ERANGE || val > SIZE_MAX) {
fprintf(stderr, "argument %s is too large\n", name);
exit(EXIT_FAILURE);
}
return (size_t)val;
}
/** Free subarrays up to excluding arr[i].
*
* @param arr array to free.
* @param i index of latest element of `arr` for which allocation has begun,
* or the size of `arr` if allocation is finished.
* @param y size of elements of `arr`.
*
* @pre
* - (1) for all `i_ < i`, `arr[i_]` is defined and allocated.
* - (2) for all `i_ < i`, `j < y`, `arr[i_][j]` is defined and allocated.
*
* **Correctness conditions**
* - in (1), these are the only such `i_`.
* - in (2), these are the only such `i_`, `j`.
*
* **Frees**
* - `arr[i_]` for `i_` in (1).
* - `arr[i_][j]` for `i_`, `j` in (2).
*
* Effects: frees some pointers derived from `arr`.
*/
static void free_sub_arr_up_to(elem ***arr, size_t i, size_t y) {
for (size_t i_ = 0; i_ < i; i_++) {
for (size_t j = 0; j < y; j++)
free(arr[i_][j]);
free(arr[i_]);
}
}
/** Free completely allocated array.
*
* @param arr array to free.
* @param x size of `arr`.
* @param y size of elements of `arr`.
*
* @pre
* - (1) for all `i < x`, `arr[i]` is defined and allocated.
* - (2) for all `i < x`, `j < y, arr[i][j]` is defined and allocated.
* - `arr` is allocated.
*
* **Correctness conditions**
* - in (1), these are the only such `i`.
* - in (2), these are the only such `i`, `j`.
*
* **Frees**
* - `arr[i]` for `i` in (1).
* - `arr[i][j]` for `i`, `j` in (2).
*
* **Effects**: frees arr and all valid pointers derived from it.
*/
static void free_complete_arr(elem ***arr, size_t x, size_t y) {
free_sub_arr_up_to(arr, x, y);
free(arr);
}
/** Free incomplete array.
*
* @param arr array to free.
* @param y size of elements of arr.
* @param i index of latest element of arr for which allocation has begun.
* @param j index of latest element of `arr[i]` for which allocation has
* begun, or y if allocation of that subarray is finished.
*
* @pre
* - (1) for all `i_ < i`, `arr[i_]` is defined and allocated.
* - (2) for all `i_ < i`, `j_ < y`, `arr[i_][j_]` is defined and allocated.
* - (3) for all `j_ < j`, `arr[i][j_]` is defined and allocated.
* - `arr` is allocated.
*
* **Correctness conditions**
* - in (1), these are the only such `i_`.
* - in (2), these are the only such `i_`, `j_`.
* - in (3), these are the only such `j_`.
*
* **Frees**
* - `arr`
* - `arr[i_]` for `i_` in (1).
* - `arr[i_][j_]` for `i_`, `j_` in (2).
* - `arr[i][j_]` for `j_` in (3).
*
* **Effects**: frees `arr` and all valid pointers derived from it.
*/
static void free_incomplete_arr(elem ***arr, size_t y, size_t i, size_t j) {
free_sub_arr_up_to(arr, i, y);
for (size_t j_ = 0; j_ < j; j_++)
free(arr[i][j_]);
free(arr[i]);
free(arr);
}
/** Calculate `x` to the `y`-th power using exponentiation by squaring.
*
* @param x base of exponentiation.
* @param y exponent.
*/
static elem elem_pow(elem x, size_t y) {
elem result = 1;
while (true) {
if ((y & 1) == 1)
result *= x;
y >>= 1;
if (y == 0)
break;
x *= x;
}
return result;
}
/** Allocate and initialize 3D array.
*
* @param x desired size of first layer of array.
* @param y desired size of each second layer of array.
* @param z desired size of each third layer of array.
* @param[out] allocs pointer to store allocation count.
*
* **Effects**: allocates, writes *allocs, may print to stderr, may exit
* program.
*
* @post
* for all `i < x`, `j < y`, `k < z`, `x[i][j][k]` is defined, where `x` is the
* return value.
*/
static elem ***mk_arr(size_t x, size_t y, size_t z, size_t *allocs) {
*allocs = 0;
elem ***arr = malloc(x * sizeof(elem **));
if (arr == NULL) {
perror("array allocation");
print_allocs(*allocs);
exit(EXIT_FAILURE);
}
++*allocs;
for (size_t i = 0; i < x; i++) {
arr[i] = malloc(y * sizeof(elem *));
if (arr[i] == NULL) {
perror("array allocation");
print_allocs(*allocs);
free_incomplete_arr(arr, y, i, 0);
exit(EXIT_FAILURE);
}
++*allocs;
for (size_t j = 0; j < y; j++) {
arr[i][j] = malloc(z * sizeof(elem));
if (arr[i][j] == NULL) {
perror("array allocation");
print_allocs(*allocs);
free_incomplete_arr(arr, y, i, j);
exit(EXIT_FAILURE);
}
++*allocs;
for (size_t k = 0; k < z; k++)
// First three prime numbers
arr[i][j][k] = elem_pow(2, i) * elem_pow(3, j) * elem_pow(5, k);
}
}
return arr;
}
/** Check argument count, correct user, and exit.
*
* @param argc argument count.
* @param argv_0 first argument string (usually the program name).
*
* @pre
* - only if `argc == 0`, `argv_0` may be undefined.
* - `argv_0`, if defined, is nul-terminated.
*
* **Effects**: may print to stderr, may exit program.
*
* @post
* `i == 4`
*/
static void ensure_usage(int argc, char *argv_0) {
if (argc != 4) {
char *pname = argc == 0 || argv_0[0] == '\0' ? "<program>" : argv_0;
fprintf(stderr,
"wrong usage!\n"
"usage: %s <x> <y> <z>\n",
pname);
exit(EXIT_FAILURE);
}
}
/** Print elements of array.
*
* @param arr array to print.
* @param x size of `arr`.
* @param y size of elements of `arr`.
* @param z size of elements of elements of `arr`.
*
* @pre
* for all `i < x`, `j < y`, `k < z`, `arr[i][j][k]` is defined.
*
* **Owns**
* - `arr`.
* - `arr[i]` for all `i < x`.
* - `arr[i][j]` for all `i < x`, `j < y`.
*
* **Effects**: prints to stdout, may print to stderr, may exit program.
*/
static void print_arr(elem ***arr, size_t x, size_t y, size_t z) {
for (size_t i = 0; i < x; i++)
for (size_t j = 0; j < y; j++)
for (size_t k = 0; k < z; k++)
if (printf("arr[%zu][%zu][%zu] = %lu\n", i, j, k,
arr[i][j][k]) < 0) {
perror("value output");
free_complete_arr(arr, x, y);
exit(EXIT_FAILURE);
}
}
/** Main function of the `3darr` program.
*
* Allocates a 3D array with dimensions specified by the arguments, populates it
* with unique values, and prints it.
*/
int main(int argc, char **argv) {
ensure_usage(argc, argv[0]);
size_t x = get_arg_size_t(argv[1], "x");
size_t y = get_arg_size_t(argv[2], "y");
size_t z = get_arg_size_t(argv[3], "z");
size_t allocs;
elem ***arr = mk_arr(x, y, z, &allocs);
if (!print_allocs(allocs)) {
perror("value output");
free_complete_arr(arr, x, y);
return EXIT_FAILURE;
}
print_arr(arr, x, y, z);
free_complete_arr(arr, x, y);
return EXIT_SUCCESS;
}