| View previous topic :: View next topic |
| Author |
8. Smart Pointers |
Cuchulainn

Joined: 18 Dec 2006 Posts: 461 Location: Amsterdam, the Netherlands
|
Posted: Tue May 05, 2009 1:37 pm Post subject: |
|
|
Some exam questions on memory management, smart pointers. (Word 2003 and 2007).
| Description: |
|
 Download |
| Filename: |
ExercisesMemoryManagement.docx |
| Filesize: |
22.55 KB |
| Downloaded: |
146 Time(s) |
| Description: |
|
 Download |
| Filename: |
ExercisesMemoryManagement.doc |
| Filesize: |
49.5 KB |
| Downloaded: |
459 Time(s) |
|
|
| Back to top |
|
 |
Cuchulainn

Joined: 18 Dec 2006 Posts: 461 Location: Amsterdam, the Netherlands
|
Posted: Sun Feb 01, 2009 11:51 am Post subject: |
|
|
Explain what is happening here:
// TestScopedPtr.cpp
//
// Testing scoped pointers in boost
//
// (C)Datasim Education BV 2008
//
#include "boost/scoped_ptr.hpp"
#include "Point.hpp"
int main()
{
{
// Create dynamic memory
boost::scoped_ptr <Point> myPoint (new Point(1.0, 23.3));
// Scoped pointer has same syntax as a raw pointer
if (myPoint != 0)
{
cout << *myPoint;
}
// Assign to another point
Point yourPoint (7.3, -9.9);
*myPoint = yourPoint;
cout << *myPoint;
// Use operator '->'
myPoint -> X(8. 6);
cout << *myPoint;
// Cannot assign scoped pointers, because operator '=' is private
boost::scoped_ptr <Point> myPoint2 (new Point(1.0, 23.3));
// THIS CODE DOES NOT COMPILE myPoint = myPoint2;
// Illegal, cannot convert
// boost::scoped_ptr <Point> illegalVar (new double);
}
return 0;
}
|
|
| Back to top |
|
 |
Cuchulainn

Joined: 18 Dec 2006 Posts: 461 Location: Amsterdam, the Netherlands
|
Posted: Wed Jan 21, 2009 1:05 pm Post subject: 8. Smart Pointers |
|
|
In C++ we use raw pointers; boost has functionality for smart pointers.
Questions:
1. What are the 3 largest potential pitfalls with raw pointers?
2. How does boost resolve these problems?
3. What is the difference between scoped pointers and auto_ptr? Which one to use and when?
4. Which boost smart pointers implements the Gamma (GOF) Flyweight pattern?
5. How to avoid dangling pointers?
|
|
| Back to top |
|
 |
|