Write a function called // Precondition: p1, p2 and p3 either point // to dynamically created integers or are // equal to nullptr void allocate3(int* &p1, int* &p2, int* &p3) that will dynamically allocate space for three integers initialized to 0. If the pointers already point to dynamic memory, that memory shoul be deleted. The function should have a strong exception guarantee. If any of the allocations fails by new throwing a bad_alloc exception, the function should also throw that exception after fulfilling its guarantee. int *a, *b = nullptr, *c = nullptr; a = new int (2); allocate3(a, b, c); // a’s previous memory is deleted cout << *a « *b << *c < endl; // prints 000 int *x, *y = nullptr, *z = nullptr; x = new int (2); try { // allocating memory for y throws badalloc below allocate3(x, y, z); } catch (bad_alloc) { } cout <« *x « endl; // prints 2

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
## Question 11

### Task: Develop a Function `allocate3`

Create a function named `allocate3` that will dynamically allocate space for three integers, each initialized to 0. The function should manage memory carefully:

```cpp
// Preconditions: p1, p2, and p3 either point to dynamically created integers or are equal to nullptr
void allocate3(int* &p1, int* &p2, int* &p3)
```

- **Functionality**: The function checks if the pointers `p1`, `p2`, and `p3` point to existing dynamic memory. If they do, it deletes that memory before allocating new memory.
  
- **Exception Handling**: The function guarantees strong exception safety. If memory allocation fails and throws a `bad_alloc` exception, the function should throw that exception after ensuring no resource leaks occur.

### Example Implementation

```cpp
int *a, *b = nullptr, *c = nullptr;
a = new int (2);
allocate3(a, b, c); // Previous memory of `a` is deleted
cout << *a << *b << *c << endl; // Outputs: 000

int *x, *y = nullptr, *z = nullptr;
x = new int (2);
try {
    // Memory allocation for `y` throws bad_alloc below
    allocate3(x, y, z);
} catch (bad_alloc) {}
cout << *x << endl; // Outputs: 2
```

### Explanation

- **Memory Management**: Before allocating new memory for `p1`, `p2`, and `p3`, the function deletes any existing dynamic memory they point to.
  
- **Exception Safety**: The function ensures that if a `bad_alloc` exception occurs, the program's state remains consistent by handling exception safety rigorously.

This design helps in efficient memory management and error handling in C++, especially when dealing with dynamic memory allocation.
Transcribed Image Text:## Question 11 ### Task: Develop a Function `allocate3` Create a function named `allocate3` that will dynamically allocate space for three integers, each initialized to 0. The function should manage memory carefully: ```cpp // Preconditions: p1, p2, and p3 either point to dynamically created integers or are equal to nullptr void allocate3(int* &p1, int* &p2, int* &p3) ``` - **Functionality**: The function checks if the pointers `p1`, `p2`, and `p3` point to existing dynamic memory. If they do, it deletes that memory before allocating new memory. - **Exception Handling**: The function guarantees strong exception safety. If memory allocation fails and throws a `bad_alloc` exception, the function should throw that exception after ensuring no resource leaks occur. ### Example Implementation ```cpp int *a, *b = nullptr, *c = nullptr; a = new int (2); allocate3(a, b, c); // Previous memory of `a` is deleted cout << *a << *b << *c << endl; // Outputs: 000 int *x, *y = nullptr, *z = nullptr; x = new int (2); try { // Memory allocation for `y` throws bad_alloc below allocate3(x, y, z); } catch (bad_alloc) {} cout << *x << endl; // Outputs: 2 ``` ### Explanation - **Memory Management**: Before allocating new memory for `p1`, `p2`, and `p3`, the function deletes any existing dynamic memory they point to. - **Exception Safety**: The function ensures that if a `bad_alloc` exception occurs, the program's state remains consistent by handling exception safety rigorously. This design helps in efficient memory management and error handling in C++, especially when dealing with dynamic memory allocation.
Expert Solution
Step 1

The solution for the above given question is given below:

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY