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
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 shoule
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
Transcribed Image Text:Question 11 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 shoule 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
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
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