From 8ffad9760738c8b1ed6e73332bdbe1648898b3d3 Mon Sep 17 00:00:00 2001 From: zleyyij <75810274+zleyyij@users.noreply.github.com> Date: Tue, 19 Nov 2024 14:05:06 -0700 Subject: [PATCH] vault backup: 2024-11-19 14:05:06 --- .../ECE1400/Chapter 17 Exercises.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/education/software development/ECE1400/Chapter 17 Exercises.md b/education/software development/ECE1400/Chapter 17 Exercises.md index e69de29..bce3cde 100644 --- a/education/software development/ECE1400/Chapter 17 Exercises.md +++ b/education/software development/ECE1400/Chapter 17 Exercises.md @@ -0,0 +1,18 @@ +> **1.** Having to check the return function of `malloc` (or any other memory allocation function) each time we call it can be an annoyance. Write a function named `my_malloc` that serves as a "wrapper" for `malloc`. When we call `my_malloc` and ask it to allocate `n` bytes, it in turn calls `malloc`, tests to make sure that `malloc` doesn't return a null pointer, then returns the pointer from `malloc`. Have `my_malloc` print an error message and terminate the program if `malloc` returns a null pointer. + +```c +void *my_malloc(size_t n) { + void *p = malloc(n); + if (p == NULL) { + printf("Failed to allocate memory"); + exit(EXIT_FAILURE); + } + return p; +} +``` + +--- + +> **2.** + +