1. Home
  2. Tutorials
  3. C/C++
  4. Boost List of Pointers
Yolinux.com Tutorial

C++ boost and STL template list of pointers:

A tutorial on how to manage a list of C++ pointers using boost and STL template libraries.

Boost ptr_list: Boost has the ptr_list container class specifically programmed to manage a list of pointers. It is different than the STL list which is more generic in nature. The boost ptr_list will perform memory management and "delete" pointers it is containing.

STL list: This generic container template is discussed in detail in the YoLinux.com STL vector and STL list tutorial. This tutorial will show examples of an STL list used to manage a list of pointers.

Also see our C++ Templates tutorial



Free Information Technology Magazines and Document Downloads
TradePub link image

Free Information Technology Software and Development Magazine Subscriptions and Document Downloads

Example: Boost ptr_list:

 

 
#include <boost/ptr_container/ptr_list.hpp>
#include <iostream>

using namespace std;

class ABC
{
public:
   int i;
   float j;
};

main()
{
   boost::ptr_list<ABC> intList;
   boost::ptr_list<ABC>::iterator iterIntList;

   ABC *a= new ABC;
   ABC *b= new ABC;
   ABC *c= new ABC;

   a->i = 1;
   b->i = 2;
   c->i = 3;

   intList.push_back(a);
   intList.push_back(b);
   intList.push_back(c);

   for (iterIntList = intList.begin();
        iterIntList != intList.end();
        iterIntList++)
   {
      cout << iterIntList->i << endl;
   }

   intList.clear(); // All pointers held in list are deleted.

}

Compile: g++ testBoostPtrList.cpp
Run: ./a.out

1
2
3
See http://www.boost.org/libs/ptr_container/doc/ptr_list.html

Red Hat RPM packages: boost, boost-devel Newer Linux releases like Red Hat Enterprise 5/CentOS 5 include boost "ptr_list". RHEL4 included boost libraries but did not include "ptr_list".

Ubuntu installation: apt-get install libboost-dev libboost-doc

Example: STL list of pointers:

 

 
// g++ -g  testStlPtrList.cpp
#include <iostream>
#include <list>

using namespace std;

class ABC
{
public:
   int i;
   float j;
};

main()
{
   list<ABC*> intList;
   list<ABC*>::iterator iterIntList;

   ABC *a= new ABC;
   ABC *b= new ABC;
   ABC *c= new ABC;

   a->i = 1;
   b->i = 2;
   c->i = 3;

   intList.push_back(a);
   intList.push_back(b);
   intList.push_back(c);

   for (iterIntList = intList.begin();
        iterIntList != intList.end();
        iterIntList++)
   {
      cout << (*iterIntList)->i << endl;
   }

   // Free pointers
   for (iterIntList = intList.begin();
        iterIntList != intList.end();
        iterIntList++)
   {
      delete *iterIntList;
   }

   intList.clear(); // List is deleted.

}
This example shows how one must delete each pointer individually.

For more information on STL list, see the YoLinux.com STL vector and STL list tutorial

GDB: pointers and memory investigation

You can test the above programs and investigate their respective memory clean-up in GDB.

See the YoLinux.com GDB tutorial.

Using GDB:

  • Compile with debugging enabled: g++ -g -o testStlPtrList testStlPtrList.cpp
  • Start GDB: gdb testStlPtrList
  • Show listing with line numbers: list
  • Set break point: break 30
  • Run program till break point: run
  • Find memory location of variable in GDB: print &(c->i)
    (gdb) p &(c->i)
    $4 = (int *) 0x503050
  • Dereference memory location: p (*0x503050)
    (gdb) p (*0x503050)
    $5 = 3
  • Dereference memory location after memory is freed: p (*0x503050)
    (gdb) p (*0x503050)
    $7 = 0
    Using RHEL5. Note older systems may give you a nonsense value.

Books:

The C++ Standard Library: A Tutorial Reference
Nicolai M. Josuttis
ISBN #0201379260, Addison Wesley Longman

This book is the only book I have seen which covers string classes as implemented by current Linux distributions. It also offers a fairly complete coverage of the C++ Standard Template Library (STL). Good reference book.

Amazon.com
STL for C++ programmers
Leen Ammeraal
ISBN #0 471 97181 2, John Wiley & Sons Ltd.

Short book which teaches C++ Standard Template Library (STL) by example. Not as great as a reference but is the best at introducing all the concepts necessary to grasp STL completely and good if you want to learn STL quickly. This book is easy to read and follow.

Amazon.com
Data Structures with C++ Using STL
William Ford, Willaim Topp
ISBN #0130858501, Prentice Hall
Amazon.com
STL Tutorial and Reference Guide: C++ Programming with the Standard Template Library
David R. Musser, Gillmer J. Derge, Atul Saini
ISBN #0201379236, Addison-Wesley Publications
Amazon.com
The C++ Templates: The complete guide.
David Vandevoorde, Nicolai Josuttis
ISBN #0201734842, Addison Wesley Pub Co.

Covers complex use of C++ Templates.

Amazon.com
C++ How to Program
by Harvey M. Deitel, Paul J. Deitel
ISBN #0131857576, Prentice Hall

Fifth edition. The first edition of this book (and Professor Sheely at UTA) taught me to program C++. It is complete and covers all the nuances of the C++ language. It also has good code examples. Good for both learning and reference.

Amazon.com

   
Bookmark and Share

Advertisements