8. Smart Pointers

8. Smart Pointers

Postby Cuchulainn » Wed Jan 21, 2009 2:05 pm

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?
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun Feb 01, 2009 12:51 pm

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;

}
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Tue May 05, 2009 2:37 pm

Some exam questions on memory management, smart pointers. (Word 2003 and 2007).
Attachments
ExercisesMemoryManagement.docx
(22.55 KiB) Downloaded 643 times
ExercisesMemoryManagement.doc
(49.5 KiB) Downloaded 984 times
User avatar
Cuchulainn
 
Posts: 669
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands


Return to C++ Certification and Examinations: Advanced Level

Who is online

Users browsing this forum: No registered users and 1 guest

cron