Question

unique_ptr<int> p1 (new int (15)); unique_ptr<int> p2 (p1); unique_ptr<int> p3; p3=p1; Which line(s) will trigger a...

unique_ptr<int> p1 (new int (15));

unique_ptr<int> p2 (p1);

unique_ptr<int> p3;

p3=p1;

  1. Which line(s) will trigger a compile error?

  2. Which lines will trigger a segmentation fault?

P3 = make_unique<int> (25);

cout<< *p1 << endl;

cout<< *p3 << endl;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A.Which line(s) will trigger a compile error?

unique_ptr<int> p1 (new int (15));

unique_ptr<int> ppq2 (p1);

unique_ptr<int> p3;

p3=p1;

Explanation:

The above underlined, bold lines will give compiler error. Because, those lines make conflict with semantics of unique_ptr.

Based on its name, it can't be shared between multiple owners. At any point of time, only one entity can have the ownership.

unique_ptr<int> p2 (p1); This line is trying to copy p1 using copy constructor, but copying unique pointer is not allowed.

p3=p1; This line is trying is trying to copy p1 using assignment operator, but copying unique pointer is not allowed.

B. Which lines will trigger a segmentation fault?

p3 = make_unique<int> (25);

cout<< *p1 << endl;

cout<< *p3 << endl;

p3 = make_unique<int>(25);

returns unique_pointer which manages integer pointer and initializes integer value to 25

cout<< *p1 << endl;

printing resided value in memory hold by p1

cout<< *p3 << endl;

printing resided value in memory hold by p3

So, no line will trigger a segmentation fault. All 3 statements are valid and generate proper output.

  

Add a comment
Know the answer?
Add Answer to:
unique_ptr<int> p1 (new int (15)); unique_ptr<int> p2 (p1); unique_ptr<int> p3; p3=p1; Which line(s) will trigger a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 6. Consider the weighted voting system [23:8,9,15,8]. Find the Banzhaf power distribution of this weighted voting system. (P1P2,P3) (P1,P2,P4) P1,P3,P4) P2 P3P4) (P1,P2,P3,P4) P1.P2) P1P3) Player...

    6. Consider the weighted voting system [23:8,9,15,8]. Find the Banzhaf power distribution of this weighted voting system. (P1P2,P3) (P1,P2,P4) P1,P3,P4) P2 P3P4) (P1,P2,P3,P4) P1.P2) P1P3) Player Times critical Power index P2.P3) (P2 P4) (P3,P4) P3 7. Cindy, Jamal, Monique, and Ryan are dividing a piece of land using the lone-divider method. The values of the four pieces of land in the eyes of the each player are: Piece 1 35% 20% 25% 15% Piece 2 15% 40% 25% 25% Piece...

  • Given the following: struct Val { int num1; float num2; } ; struct Person { int...

    Given the following: struct Val { int num1; float num2; } ; struct Person { int age; float income; }; Val t, s; Person p1, p2, p3;                                        s.num1 = 0; s.num2 = 2.5;                          //initialize s p2.age = 25; p2.income = 9999.99;                //initialize p2 t = s;                            //assignment one p1 = s;                       //assignment two p2 = p3;                      //assignment three p3.age = s.num1;     //assignment four p3.income = s.num1;   //assignment five p2.age = t.num2;      //assignment six For each assignment statements above...

  • TB MC Qu. 25-104 Rosie's Company has three products... Rosie's Company has three products, P1, P2, and P3. The maximum...

    TB MC Qu. 25-104 Rosie's Company has three products... Rosie's Company has three products, P1, P2, and P3. The maximum Rosie's can sell is 65,000 units of P1, 24,000 units of P2, and 12,000 units of P3. Rosie's has limited production capacity of 9,000 hours. It can produce 12 units of P1, 6 units of P2, and 3 units of P3 per hour. Contribution margin per unit is $5 for the P1, $15 for the P2, and $25 for the...

  • P1 P3 A B D C P2 а - b External forces on a bar are...

    P1 P3 A B D C P2 а - b External forces on a bar are as shown in the figure, If P1 = P3 = 15 kN, P2 = 25 kN, and 'x' = 275 mm. Which of the following expressions can correctly evaluate the reaction at B? Select one: A. RB = (25b - 30a + 15d + 8.25) / (b+c) O B. RB = (25b + 30a + 15d + 8.25) / (b+c) O C. RB =...

  • Prob. 4 (12.5 pts) The set of vectors S = {p1.p2.p3 } may be a basis...

    Prob. 4 (12.5 pts) The set of vectors S = {p1.p2.p3 } may be a basis for P2 p1 = 1 + x + x2 p2 = x + x2 p = x² a) Verify that this is the case. b) If it is a basis, find the coordinate vector of b relative to S. b = 7 - x + 2 x2

  • Q3. In a (7,4) Hamming Code, three parity bits p1, p2, p3 are added to four...

    Q3. In a (7,4) Hamming Code, three parity bits p1, p2, p3 are added to four data bits dl, d2, d3, and d4, and the coverage of each parity bit is as shown in the table below: Bit position 2 3 4 5 6 7 Encoded data bits p1 p2 di p3 d2 d3 d4 da X p1 X X X x Parity bit coverage p2 х X X p3 X X X х 1) (3 pts) Assume even parity...

  • P1 P2 P3 A | В +d- a b The maximum allowable value of each of...

    P1 P2 P3 A | В +d- a b The maximum allowable value of each of the reactions is 250.25. Neglecting the weight of the beam, determine the range of the distance 'd' for which the beam is safe. (Note in this case the reaction for the roller may be positive or negative.) If P1=50 N, P2=75 N, P3=260 N, a=450 mm, b=550 mm. i) d 2 (m, 3 sigfig) = ii) d s (m, 3 sigfig)=

  • Start by using the starter code provided for the Line class. It implements operator overloading so...

    Start by using the starter code provided for the Line class. It implements operator overloading so you can use cin/cout with line objects. A line will be made up of two points. Create an object to implement a “line” class which allows the programmer to store a line. This class must use the “point” class developed in Part B. The object should have two constructors, appropriate set/get functions, and overloaded I/O (cin/cout) functions. It should include functions the return the...

  • TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes...

    TRUE/FALSE 1. If pl is an integer pointer variable, with the value of 1000, pl++ changes P1 to point to the memory location 1001. ANSWER: T 2. When you return a dynamic array to the freestore, you must include the number of elements in the array 3. You can assign an array to a pointer variable. 4. The size of dynamic arrays must be declared at compile time. 5. int *pl; declares a static variable. ANSWER: F ANSWER: F ANSWER:...

  • B A P1 P2 P3 P4 obotex-fc- a External forces on a bracket are as shown...

    B A P1 P2 P3 P4 obotex-fc- a External forces on a bracket are as shown in the figure, If P1 = 20 kN,P2 = -10 kN, P3 =P4 = 15 kN, and 'x' = 0.45m. Which of the following expressions can correctly evaluate the reaction at B? Select one: O A. RB = (20a - 20(a+b) - 150 - 13.5) / (a+b) O B. RB = (20b + 150 + 13.5) / (a+b) X C. RB = (20a +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT