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 should fırst 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 nullptr; int *x, *y = nullptr, *z = X = new int (2); try { // allocating memory for y throws badalloc below allocate3(x, y, z); } catch (bad_alloc) { } cout <« *x « endl; // prints 2

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please answer in C++ language. 

Show all work! 

———

I will upvote answer if correct. 

### Question 11

**Task**: Write a function called `allocate3`.

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

The function dynamically allocates space for three integers initialized to 0. If the pointers already point to dynamic memory, that memory should first be deleted. The function should have a strong exception guarantee. If any of the allocations fail by `new` throwing a `bad_alloc` exception, the function should also throw that exception after fulfilling its guarantee.

Example usage:

```cpp
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
```

This code demonstrates how to handle dynamic memory allocation with strong exception safety in C++. The `allocate3` function ensures that memory is properly managed and exceptions are handled cleanly.
Transcribed Image Text:### Question 11 **Task**: Write a function called `allocate3`. ```cpp // Precondition: p1, p2 and p3 either point // to dynamically created integers or are // equal to nullptr void allocate3(int* &p1, int* &p2, int* &p3) ``` The function dynamically allocates space for three integers initialized to 0. If the pointers already point to dynamic memory, that memory should first be deleted. The function should have a strong exception guarantee. If any of the allocations fail by `new` throwing a `bad_alloc` exception, the function should also throw that exception after fulfilling its guarantee. Example usage: ```cpp 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 ``` This code demonstrates how to handle dynamic memory allocation with strong exception safety in C++. The `allocate3` function ensures that memory is properly managed and exceptions are handled cleanly.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education