Topic: STL and Pointers


Author: herwin@osf1.gmu.edu (HARRY R. ERWIN)
Date: 1997/05/22
Raw View
No books that I know of address these issues yet. Mumit Khan does have a
good web page on the subject.  See

    http://www.xraylith.wisc.edu/~khan/software/stl.

--
Harry Erwin, Internet: herwin@gmu.edu,
Web Page: http://osf1.gmu.edu/~herwin
PhD student in computational neuroscience (how bats echolocate)
Lecturer for CS 211 (data structures and advanced C++)
Senior Software Analyst supporting the FAA
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Cynosure Computer Tech <cynosure@kos.net>
Date: 1997/05/22
Raw View
As a relative 'newbie' to STL I have what I think is
a fundamental question.  It appears that all of the
containers and algorithms are geared towards storing
and manipulating objects.

How do things work when you want to dynamically allocate
memory for the objects and store the pointers rather than
the objects themselves?

I realize that storing the pointers can be done but all of
the algorithms seem geared to manipulating the objects themselves.

What am I missing?

Larry
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: jbuck@synopsys.com (Joe Buck)
Date: 1997/05/22
Raw View
Cynosure Computer Tech <cynosure@kos.net> writes:
>As a relative 'newbie' to STL I have what I think is
>a fundamental question.  It appears that all of the
>containers and algorithms are geared towards storing
>and manipulating objects.
>
>How do things work when you want to dynamically allocate
>memory for the objects and store the pointers rather than
>the objects themselves?

The approach I take is to use the envelope-and-letter pattern; the
"envelope class" is a pointer and the "letter class" is an object with a
reference count.  It's quite similar to the most common implementation
of the standard string class.

It would be possible to do the whole thing with a template that delegates
all of the member function calls to the "letter class", except that the
committee rejected overloaded operator dot (Stepanov, the STL designer,
has also written that this rejection was a mistake that makes STL less
useful).
--
-- Joe Buck http://www.synopsys.com/pubs/research/people/jbuck.html

Help stamp out Internet spam: see http://spam.abuse.net/spam/
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "John I. Moore, Jr." <70672.1744@compuserve.com>
Date: 1997/05/22
Raw View
Cynosure Computer Tech <cynosure@kos.net> wrote in article
<3383ef27.0@news.kosone.com>...
> As a relative 'newbie' to STL I have what I think is
> a fundamental question.  It appears that all of the
> containers and algorithms are geared towards storing
> and manipulating objects.
>
> How do things work when you want to dynamically allocate
> memory for the objects and store the pointers rather than
> the objects themselves?

Actually I store pointers in STL container classes frequently.
There are a few things that you need to be careful about, but
technically there is nothing wrong with a declaration such as

vector<C*> v;

You can use v to hold pointers to C objects.

_____________________________________________________________

John I. Moore, Jr.          phone:  (301) 924-0680
SoftMoore Consulting        email:  70672.1744@compuserve.com
16233 Monty Court
Rockville, MD  20853-1344
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/05/23
Raw View
Cynosure Computer Tech <cynosure@kos.net> writes:

> As a relative 'newbie' to STL I have what I think is
> a fundamental question.  It appears that all of the
> containers and algorithms are geared towards storing
> and manipulating objects.
>
> How do things work when you want to dynamically allocate
> memory for the objects and store the pointers rather than
> the objects themselves?

Don't forget that pointers are objects.  That is, a pointer is "a
region of storage ... created by a definition, by a new-expression,
or by the implementation when needed."  A pointer has a storage
duration, a lifetime, and a type, and it can have a name.
(See section 1.7 of the CD2, "The C++ object model".)

Since pointers are objects, you can put pointers into STL containers.
I often use containers of pointers.

By contrast, incidentally, references are not objects.  You cannot
have a container of references.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Cynosure Computer Tech <cynosure@kos.net>
Date: 1997/05/24
Raw View
Thanks to all that provided information and insight.

It appears that I will have to provide some addition
wrapping of classes.  This is unfortunate as I was
hoping that this would all be transparent.

If using the STL isn't easy or intuitive I'm afraid
people might pass it by.

Then again maybe I just haven't overcome the learning
curve.

Larry
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: scott@eviews.com (Scott Ellsworth)
Date: 1997/05/22
Raw View
In article <3383ef27.0@news.kosone.com>, Cynosure Computer Tech <cynosure@kos.net> wrote:
>As a relative 'newbie' to STL I have what I think is
>a fundamental question.  It appears that all of the
>containers and algorithms are geared towards storing
>and manipulating objects.

That has been my experience.

If you want to store pointers or references to objects, use an object that
holds those references or pointers.  I do this regularly, as my data
structures tend to be quite large, and the way the are created and destroyed
works very well with vectors or deques, depending on details.

I often need to use references or pointers to those already created objects
living in a vector, though, in various maps or sets.  Depending on need, I
will use anything from a very simple wrapper up to a reference counted and
tracked smart pointer.  This is reasonably efficient, though there is a
forward to the underlying object to consider.  I have never had that be marked
by my profiler as a problem, but I do check occasionally.

As a good place to start, look at auto_ptr as one variant of a smart pointer,
and look at Scott Meyer's More Effective C++ for advice n how to design a
smart pointer class.

Scott

Scott Ellsworth          scott@eviews.com
"When a great many people are unable to find work, unemployment
results" - Calvin Coolidge, (Stanley Walker, City Editor, p. 131 (1934))
"The barbarian is thwarted at the moat." - Scott Adams
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]