From 846cb06c90fa27a52dff22655fc98a71ba60b970 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Sun, 22 Sep 2019 18:26:36 +0200 Subject: [PATCH] Improve error handling Added error handling in case of memory allocation error for problems 35, 46, 47, 49 and 50. --- C/p035.c | 6 +++++- C/p046.c | 6 +++++- C/p047.c | 6 +++++- C/p049.c | 6 +++++- C/p050.c | 6 +++++- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/C/p035.c b/C/p035.c index 0399886..8b0447c 100644 --- a/C/p035.c +++ b/C/p035.c @@ -18,7 +18,11 @@ int main(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &start); - primes = sieve(N); + if((primes = sieve(N)) == NULL) + { + fprintf(stderr, "Error! Sieve function returned NULL\n"); + return 1; + } for(i = 101; i < 1000000; i += 2) { diff --git a/C/p046.c b/C/p046.c index e51ac2e..50f8de5 100644 --- a/C/p046.c +++ b/C/p046.c @@ -18,7 +18,11 @@ int main(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &start); - primes = sieve(N); + if((primes = sieve(N)) == NULL) + { + fprintf(stderr, "Error! Sieve function returned NULL\n"); + return 1; + } for(i = 3; !found && i < N; i += 2) { diff --git a/C/p047.c b/C/p047.c index 372e3e5..38a80ff 100644 --- a/C/p047.c +++ b/C/p047.c @@ -18,7 +18,11 @@ int main(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &start); - primes = sieve(N); + if((primes = sieve(N)) == NULL) + { + fprintf(stderr, "Error! Sieve function returned NULL\n"); + return 1; + } for(i = 645; !found && i < N - 3; i++) { diff --git a/C/p049.c b/C/p049.c index 842d314..5d354e7 100644 --- a/C/p049.c +++ b/C/p049.c @@ -18,7 +18,11 @@ int main(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &start); - primes = sieve(N); + if((primes = sieve(N)) == NULL) + { + fprintf(stderr, "Error! Sieve function returned NULL\n"); + return 1; + } for(i = 1489; i < N && !found; i++) { diff --git a/C/p050.c b/C/p050.c index f7e9aba..aea0414 100644 --- a/C/p050.c +++ b/C/p050.c @@ -15,7 +15,11 @@ int main(int argc, char **argv) clock_gettime(CLOCK_MONOTONIC, &start); - primes = sieve(N); + if((primes = sieve(N)) == NULL) + { + fprintf(stderr, "Error! Sieve function returned NULL\n"); + return 1; + } for(i = 2; i < N; i++) {