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

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter13: Structures
Section: Chapter Questions
Problem 5PP
icon
Related questions
Question

Please answer in C++ language. 

Show all work! 

———

I will upvote answer if correct. 

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 should first
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
a = new int (2);
allocate3(a, b, c); // a’s previous memory is deleted
cout <« *a « *b_ << *c_ « endl; // prints 000
nullptr, *c = nullptr;
%3D
%3D
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
nullptr;
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 should first 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 a = new int (2); allocate3(a, b, c); // a’s previous memory is deleted cout <« *a « *b_ << *c_ « endl; // prints 000 nullptr, *c = nullptr; %3D %3D 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 nullptr;
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr