Skip to content

Commit

Permalink
error handling if realloc fails
Browse files Browse the repository at this point in the history
  • Loading branch information
pegeler committed Sep 10, 2024
1 parent b61d4b4 commit ca3ee88
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions c/eddington.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

typedef struct {
int *data;
int n;
int size;
} Vector;

void usage(char *prog) {
Expand All @@ -28,16 +28,21 @@ void usage(char *prog) {
* @param[in] file the file stream to read from
*/
Vector read_values(FILE *file) {
Vector v = {0};
char line[MAX_LEN];
int len = 1024;
v.data = malloc(len * sizeof(int));
Vector v = {
malloc(len * sizeof(int)),
0
};
char line[MAX_LEN];
while (fgets(line, MAX_LEN, file) != NULL) {
if (v.n >= len) {
if (v.size >= len) {
len *= 2;
v.data = realloc(v.data, len * sizeof(int));
if ((v.data = realloc(v.data, len * sizeof(int))) == NULL) {
fprintf(stderr, "Failed to allocate memory\n");
exit(EXIT_FAILURE);
}
}
v.data[v.n++] = atoi(line);
v.data[v.size++] = atoi(line);
}
return v;
}
Expand All @@ -49,11 +54,11 @@ Vector read_values(FILE *file) {
*/
void compute_eddington_number(Vector v, int c) {
int E = 0;
int *h = calloc(v.n + 1, sizeof(int));
int *h = calloc(v.size + 1, sizeof(int));

for (int i=0, above=0; i < v.n; i++) {
for (int i=0, above=0; i < v.size; i++) {
if (v.data[i] > E) {
if (v.data[i] < v.n) h[v.data[i]]++;
if (v.data[i] < v.size) h[v.data[i]]++;
if (++above > E) above -= h[++E];
}

Expand Down

0 comments on commit ca3ee88

Please sign in to comment.