commit 1e6942b9f9ad1018f0ed75947ebe69cda5b047f5
parent ee23eff931ba11a8789ed2d47b9b42802da4fa7d
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Thu, 18 Mar 2021 14:41:37 +0100
arrays.[ch]: check memory allocation and exit on fail, add some helper functions
Diffstat:
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/arrays.c b/arrays.c
@@ -106,7 +106,7 @@ zeros(const int n)
double *x;
check_magnitude(__func__, 1, n);
- if (!(x = malloc(n * sizeof(double))))
+ if (!(x = calloc(n, sizeof(double))))
err(1, "%s: x calloc", __func__);
for (i = 0; i < n; ++i)
x[i] = 0.0;
@@ -150,8 +150,14 @@ initval(const double value, const int n)
double *
empty(const int n)
{
+ double *out;
+
check_magnitude(__func__, 1, n);
- return malloc(n * sizeof(double));
+
+ if (!(out = malloc(n * sizeof(double))))
+ err(1, "%s: calloc", __func__);
+
+ return out;
}
/* Return largest value in array `a` with size `n` */