Pointer to container in STL

Discussion in 'C++' started by Daniel Marques, Jun 28, 2006.

  1. I would like to know how to get the address of a container of the STL
    without using iterators. I have a class with three STL lists and a
    index which points to an element of any ot the other lists, but i don't
    know how to access these elements addresses.
    Just as an example:
    class C {
    public:
    int a;
    }

    int main() {
    C obj;
    obj.a=2;
    list<C> letter;
    letter.push_back(obj);
    list<C>::iterator it = letter.begin();
    // How do i know where 'it' is pointing
    }

    Thanks in advance, Daniel Marques
     
    Daniel Marques, Jun 28, 2006
    #1
    1. Advertising

  2. Daniel Marques wrote:
    > I would like to know how to get the address of a container of the STL
    > without using iterators. I have a class with three STL lists and a
    > index which points to an element of any ot the other lists, but i don't
    > know how to access these elements addresses.
    > Just as an example:
    > class C {
    > public:
    > int a;
    > }
    >
    > int main() {
    > C obj;
    > obj.a=2;
    > list<C> letter;
    > letter.push_back(obj);
    > list<C>::iterator it = letter.begin();
    > // How do i know where 'it' is pointing
    > }


    C* ptr = &(*it);

    Best regards,

    Tom
     
    Thomas Tutone, Jun 28, 2006
    #2
    1. Advertising

  3. Daniel Marques wrote:
    > I would like to know how to get the address of a container of the STL
    > without using iterators. I have a class with three STL lists and a
    > index which points to an element of any ot the other lists, but i don't
    > know how to access these elements addresses.
    > Just as an example:
    > class C {
    > public:
    > int a;
    > }
    >
    > int main() {
    > C obj;
    > obj.a=2;
    > list<C> letter;
    > letter.push_back(obj);
    > list<C>::iterator it = letter.begin();
    > // How do i know where 'it' is pointing
    > }


    Im not sure if this is a preferable way but I whould use here:

    C* c_ptr = &(*it);

    Yes this looks very strange, but it should give what you want: The
    physical* address of the element.

    HTH,
    Henning

    *) Well I know in fact it is not the real physical adress but the linear
    or logical one or whatever.
    "Physical" here just should denote that what you get is not an
    STL-iterator but a primitive pointer.
     
    Henning Hasemann, Jun 28, 2006
    #3
  4. "Daniel Marques" <> wrote:

    > I would like to know how to get the address of a container of the STL
    > without using iterators.


    This "question" contains a false two false pre-assumptions:
    1. That iterators are pointers (they're not; they're classes)
    2. That iterators point to containers (they don't; they point
    to elements IN containers)

    > I have a class with three STL lists and a index which points
    > to an element of any ot the other lists, but i don't
    > know how to access these elements addresses.


    Don't do that!

    STL containers do their own memory management. A pointer to
    an element in such a container tends to get "stale" real fast.
    You can get a pointer to an element of a container easy:

    std::vector<int> Bark;
    Bark.push_back(42);
    Bark.push_back(18);
    Bark.push_back(87);
    int* Fred = &(Bark[1]);

    But will it still point to the same element when you get around
    to using it, later? Maybe, or maybe not. The element may
    have been erased! Or the container may have re-allocated
    itself and moved! You may be now dereferencing a pointer
    to God knows what. That often causes hard-to-debug sporadic
    runtime errors. The kind of bugs that can stay with a
    software project throughout its lifetime because no one
    could figure out what was going wrong.

    Instead, use iterators and algorithms. That's what they're
    for: accessing elements in STL containers.

    > Just as an example:
    > class C {
    > public:
    > int a;
    > }
    >
    > int main() {
    > C obj;
    > obj.a=2;
    > list<C> letter;
    > letter.push_back(obj);
    > list<C>::iterator it = letter.begin();
    > // How do i know where 'it' is pointing
    > }


    You don't need to know where "it" is pointing. Don't try to store
    the location of the beginning of an STL container. Instead, wait
    until you need to USE that location, then call begin() at that time.
    That way, you'll NEVER have to worry about whether your pointer is
    valid or stale.

    Just because C++ ALLOWS you to take addresses of things doesn't mean
    you necessarily should. That's very dangerous. So much so that many
    languages (eg, Perl, Java, etc.) have outlawed that. Pointers are evil.
    Don't use them except in those cases where they're a _necessary_ evil.
    (Eg, when doing polymorphism; or when passing a function pointer as
    an argument to a function.)

    Some day you may end up like me, maintaining a bunch of legacy code
    (perhaps your own). If that code contains pointers used with wild
    abandon, you'll hate yourself for having written it. I guarantee it.
    So don't do that.

    Instead, use iterators. The usual way to riffle through a std::list
    is like this:

    std::list<MyType> Blat;
    // ... load stuff into Blat here ...
    std::list<MyType>::iterator Iter;
    for (Iter = Blat.begin(); Iter != Blat.end(); ++Iter)
    {
    // do stuff with (*Iter)
    }

    Another way is by using std::for_each, like so:

    std::for_each(Blat.begin(), Blat.end(), MyFunction);

    where MyFunction is a function or functor with one argument, of the
    same type as the content-type of Blat; it will be applied for each
    element of Blat.

    Or use other algorithms, such as sort, find, replace, merge, etc.

    Consult a book on STL for more info:

    The whole nine yards: Josuttis: "The C++ Standard Library"
    Short 'n' Sweet: O'Reilly's STL pocket reference.


    --
    Cheers,
    Robbie Hatley
    Tustin, CA, USA
    lone wolf intj at pac bell dot net
    home dot pac bell dot net slant earnur slant
     
    Robbie Hatley, Jun 28, 2006
    #4
  5. Daniel Marques

    red floyd Guest

    Henning Hasemann wrote:
    > Daniel Marques wrote:
    >> I would like to know how to get the address of a container of the STL
    >> without using iterators. I have a class with three STL lists and a
    >> index which points to an element of any ot the other lists, but i don't
    >> know how to access these elements addresses.
    >> Just as an example:
    >> class C {
    >> public:
    >> int a;
    >> }
    >>
    >> int main() {
    >> C obj;
    >> obj.a=2;
    >> list<C> letter;
    >> letter.push_back(obj);
    >> list<C>::iterator it = letter.begin();
    >> // How do i know where 'it' is pointing
    >> }

    >
    > Im not sure if this is a preferable way but I whould use here:
    >
    > C* c_ptr = &(*it);
    >
    > Yes this looks very strange, but it should give what you want: The
    > physical* address of the element.
    >
    > HTH,
    > Henning
    >
    > *) Well I know in fact it is not the real physical adress but the linear
    > or logical one or whatever.
    > "Physical" here just should denote that what you get is not an
    > STL-iterator but a primitive pointer.


    Assuming C hasn't overridden operator&, of course.
     
    red floyd, Jun 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,545
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    551
    Maitre Bart
    Feb 11, 2004
  3. forester
    Replies:
    16
    Views:
    741
    Howard Hinnant
    Aug 25, 2005
  4. Replies:
    4
    Views:
    833
    Daniel T.
    Feb 16, 2006
  5. wolverine
    Replies:
    2
    Views:
    485
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page