1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"
size_t text2array(unsigned **dst, const char *r) {
unsigned *a = malloc(512 * sizeof *a);
size_t na = 0, sa = 512;
char *err;
unsigned v;
for (;;) {
if (na == sa) {
// 13/8 is within 0.5% of the golden ratio
unsigned *tmp = realloc(a, ((13*sa) / 8) * sizeof *tmp);
if (!tmp) exit(EXIT_FAILURE);
a = tmp;
sa = (13*sa) / 8;
}
v = strtoul(r, &err, 10);
a[na++] = v;
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"
size_t text2array(unsigned **dst, const char *r) {
unsigned *a = malloc(512 * sizeof *a);
size_t na = 0, sa = 512;
char *err;
unsigned v;
for (;;) {
if (na == sa) {
// grow the array (by golden ratio)
unsigned *tmp = realloc(a, ((13*sa) / 8) * sizeof *tmp);
if (!tmp) exit(EXIT_FAILURE);
a = tmp;
sa = (13*sa) / 8;
}
v = strtoul(r, &err, 10);
a[na++] = v;
|
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
int ch;
char *tmp = malloc(512);
size_t s = 512;
size_t r = 0;
while ((ch = fgetc(h)) != EOF) {
if (r == s) {
//grow tmp
// 13/8 is within 0.5% of the golden ratio
char *ttmp = realloc(tmp, (13*s) / 8);
if (ttmp) {
tmp = ttmp;
s = (13*s) / 8;
} else {
free(tmp);
return 0;
|
|
<
|
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
int ch;
char *tmp = malloc(512);
size_t s = 512;
size_t r = 0;
while ((ch = fgetc(h)) != EOF) {
if (r == s) {
// grow the array (by golden ratio)
char *ttmp = realloc(tmp, (13*s) / 8);
if (ttmp) {
tmp = ttmp;
s = (13*s) / 8;
} else {
free(tmp);
return 0;
|