diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-11 22:43:15 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-11 22:43:15 +0000 |
| commit | 48cb0f7ba25f14244c608c027774fc15feddb11c (patch) | |
| tree | b533ea30133824067ac353ca4ed99baac0d1b4d5 /challenge-202 | |
| parent | e2b3a9b9cbd927d33516c42b88ec16bf0aebcb58 (diff) | |
| download | perlweeklychallenge-club-48cb0f7ba25f14244c608c027774fc15feddb11c.tar.gz perlweeklychallenge-club-48cb0f7ba25f14244c608c027774fc15feddb11c.tar.bz2 perlweeklychallenge-club-48cb0f7ba25f14244c608c027774fc15feddb11c.zip | |
Add Perl, C, C++ and Forth solutions
Diffstat (limited to 'challenge-202')
| -rw-r--r-- | challenge-202/paulo-custodio/c/ch-1.c | 10 | ||||
| -rw-r--r-- | challenge-202/paulo-custodio/c/ch-2.c | 10 |
2 files changed, 18 insertions, 2 deletions
diff --git a/challenge-202/paulo-custodio/c/ch-1.c b/challenge-202/paulo-custodio/c/ch-1.c index dc8f770f46..fc85e22ae3 100644 --- a/challenge-202/paulo-custodio/c/ch-1.c +++ b/challenge-202/paulo-custodio/c/ch-1.c @@ -34,6 +34,14 @@ Output: 1 #include <stdlib.h> #include <stdbool.h> +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + bool is_odd(int n) { return n%2==1; } @@ -53,7 +61,7 @@ int main(int argc, char* argv[]) { } int nums_size = argc; - int* nums = malloc(nums_size * sizeof(int)); + int* nums = check_mem(malloc(nums_size * sizeof(int))); for (int i = 0; i < nums_size; i++) nums[i] = atoi(argv[i]); diff --git a/challenge-202/paulo-custodio/c/ch-2.c b/challenge-202/paulo-custodio/c/ch-2.c index 09e61bdefe..424b418810 100644 --- a/challenge-202/paulo-custodio/c/ch-2.c +++ b/challenge-202/paulo-custodio/c/ch-2.c @@ -39,6 +39,14 @@ Output: 3, 3, 2, 1, 2, 3, 3 #include <stdlib.h> #include <string.h> +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + int largest_valey(int heights[], int heights_size) { int l1 = 0, r1 = -1; // initial interval @@ -72,7 +80,7 @@ int main(int argc, char* argv[]) { } int heights_size = argc; - int* heights = malloc(heights_size * sizeof(int)); + int* heights = check_mem(malloc(heights_size * sizeof(int))); for (int i = 0; i < heights_size; i++) heights[i] = atoi(argv[i]); |
