Question

TOE

PART A - 50 Points A.1 10 pts Please explain in detail Scott Meyerss point: Use weak_ptr for shared_ptr like pointers that c

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

In context of C,pointer is a variable which holds the address of another variable.
In context of C++,A pointer which directly addresses memory is referred to as raw pointer.
A raw pointer can hold the address of an object.
Dangling pointer points to the memory location of the deallocated memory.
Suppose a pointer is pointing to a memory location where an object is stored,
Now object has been deleted, but still pointer is pointing to the memory location of the deallocated memory.
such pointers are known as dangling raw pointers.


In programming, memory consumed by the object should be deallocated only if,there is no logical entity using that object.when reference count becomes zero,then object should be deleted.

The shared_ptr is a smart pointer available in C++ library that is designed to deal with the situation where more than one owner have to manage the lifetime of the object in memory.


As shown in the image part -2,there are two pointers ptr1 and ptr2 pointing to a same object of Class named "SomeClass".Here the Reference count is 2, because there are two pointers ptr1 and ptr2.
ptr1 and ptr2 have share access to one "control block".this control block increments/decrements the reference count whenever a new shared_ptr is added/ goes out of scope/ or is reset.

It is the case of shared_ptr.

weak_ptr

What is weak reference?
Garbage collector deallocates the memory of object which is no longer(expired object)
If an object has weak reference associated with other object(s),garbage collector de allocates its memory.

As discussed earlier, if there is a increase in the number of pointers(except weak_ptr),reference count will be increased.
but
There may be a situation when an object should store a way to access the object of a shared_ptr without causing the reference count to be incremented(such situation arises when there is cyclic references between shared_ptr instances.).

weak_ptr is used to break reference cycles posed by objects managed by shared_ptr. If such cycle is not handled , the shared_ptr reference counts cannot reach zero and an undesirable situation arises. To prevent this, one of the pointers in the cycle can be made weak.
weak_ptr will be deleted by garbage collector.
In this way , system will get rid of reference cycle.
finally reference cycle is broken now.

this is all about weak_ptr.

Raw foinse Pointer ___ pointe bean ddeleko Craus pointer) Cane care Pointer to Ceonit ftr2 Pointer to osj eck bletk clam


Add a comment
Know the answer?
Add Answer to:
TOE PART A - 50 Points A.1 10 pts Please explain in detail Scott Meyers's point: Use weak_ptr for shared_ptr like p...
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
  • 1.Please explain in detail Scott Meyers’s point: Use weak_ptr for shared_ptr like pointers that can dangle....

    1.Please explain in detail Scott Meyers’s point: Use weak_ptr for shared_ptr like pointers that can dangle. Hint: a correct explanation should also discuss dangling raw pointer. 2. In your understanding, why weak_ptr was not named strong_ptr? Please explain in detail how weak_ptr’s functions expired() and lock() work.

  • please provide clear answers. Thank you! 1. (8 pts) A crude reaction product (50 mg) is...

    please provide clear answers. Thank you! 1. (8 pts) A crude reaction product (50 mg) is to be purified by recrystallization from a single solvent. a) (5 pts) The crude solubility in ethanol at boiling temperature (BT) is 50 mg/mL and at O'C is 5 mg/mL. Read the statements and fill in True or False Describe in a few words the reason. True False Describe reason for true or false Statement The crude was dissolved in 5 mL of hot...

  • please explain Lock P { [ Delete ] End Pg Dn 7 Home 8 1 9...

    please explain Lock P { [ Delete ] End Pg Dn 7 Home 8 1 9 Pg Up Enter 5 6 (c) How can Kramerica use the volume break-even point value calculated in part (a)? NOTE: The Excel spreadsheet can be very useful for this part as it performs many of these calcu- lations.) - (15 points). In other words: a. What types of recommendations would you make to Kramerica if the expected number of calculator sales is 32,000 over...

  • Which method could be used to convert alkene 1 to alcohol 2? Please show the reaction...

    Which method could be used to convert alkene 1 to alcohol 2? Please show the reaction mechanism. Question number 6 is also needed please. Part 2 - Word problems (4 pts each) 5. Taxol (compound 3) used in the treatment of several kinds of cancer, especially breast cancer. It was discovered from the bark of the Pacific yew tree. Each yew tree contains only a very small quantity, enough for just one dose for one person, so chemists started to...

  • Please explain how you approach this so I can emulate your problem solving strategies on like...

    Please explain how you approach this so I can emulate your problem solving strategies on like problems Suppose we had a large, positively charged plate in an upright (vertical) position and a point charge with positive charge +Q. In lecture, you saw that a large charged plate gives rise to an electric field which is constant in space: we will call this electric field Eplate. Let us suppose also that we had a s bl with positive charge +q and...

  • Please do it like someone wouldve done it as a beginer programer. Dont use pointer unless...

    Please do it like someone wouldve done it as a beginer programer. Dont use pointer unless it asking for. /* Write a program designed to get ages and heights from the user, then find the average age, average height, and average age/height ratio. */ #include <stdlib.h> #include <stdio.h> #define MAXNUM 50 typedef struct person { int age; double height; } Person; int getData(Person people[], int max) /* Get the data from the user and put it in an array of...

  • Question 7 (80 points) Please USE an Excel spreadsheet to answer this question and upload the...

    Question 7 (80 points) Please USE an Excel spreadsheet to answer this question and upload the file to the dropbox. PART (A) 15 Points What is the intent of dividend integration? Does Current Canadian Tax Policy provide a perfect integration? Why or why not? What should the Canadian government do in order to provide a perfect integration (Please provide a hypothetically ideal solution)? Hint: No computation required. Three to four sentences would be sufficient to address these questions. PART (B)...

  • This assignment is due 2/25 and is worth 50 points Opinion Paper and Comparative Critique: Print...

    This assignment is due 2/25 and is worth 50 points Opinion Paper and Comparative Critique: Print Media and Website Information Objective: This assignment will test your ability to critically analyze popular media nutrition information, according to its content, whether you think the information is valid and/or accurate based on the principles you are learning from the course. This will help you to integrate and apply knowledge from class materials and/or relevant personal experiences. ______________________________________________________________________________________________________ Instructions: Note: This is one assignment...

  • Please, I need help answering questions 10, 11, 12, 13, 14, and 15. Use the other...

    Please, I need help answering questions 10, 11, 12, 13, 14, and 15. Use the other pages as information to solve the questions. Use the questions below to keep track of key concepts from this lesson's study activity. YOUR ASSIGNMENT: Splatapults Away! The engineering club has built a catapult and wants to test it out. The local supermarket has donated some overripe fruits and vegetables, and now the club is holding a Splatapult challenge to see who can hit the...

  • Please explain each line of code, all code will be in Java. Thank you JKL Restaurant...

    Please explain each line of code, all code will be in Java. Thank you JKL Restaurant maintains a members’ club for its customers.  There are three levels of membership: (1) Basic, (2) Silver, and (3) Gold. A certain member has to be exactly a Basic, Silver, or Gold member at any point in time.  Whenever a member spends money at JKL, he/she gets points and as these points are accumulated, one can redeem one or more $20 dining certificates.  Each Gold member can...

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
ADVERTISEMENT