vault backup: 2024-11-19 14:10:06
This commit is contained in:
parent
8ffad97607
commit
c4e36ab804
@ -1,18 +1,8 @@
|
|||||||
> **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.
|
> **4.** Suppose that the following declarations are in effect:
|
||||||
|
|
||||||
```c
|
```c
|
||||||
void *my_malloc(size_t n) {
|
struct point {int x, y; };
|
||||||
void *p = malloc(n);
|
struct rectangle { struct point upper_left, lower_right; };
|
||||||
if (p == NULL) {
|
struct rectangle *p;
|
||||||
printf("Failed to allocate memory");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
return p;
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
> Assume that we want `p` to point to a rectangle structure whose upper left corner is at $(10, 25)$, and whose lower right corner is at $(20, 15)$. Write a series of statements that allocate such a structure and initialize it as indicated.
|
||||||
---
|
|
||||||
|
|
||||||
> **2.**
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user