Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer to container in STL

Reply
Thread Tools

Pointer to container in STL

 
 
Daniel Marques
Guest
Posts: n/a
 
      06-28-2006
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

 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      06-28-2006

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

 
Reply With Quote
 
 
 
 
Henning Hasemann
Guest
Posts: n/a
 
      06-28-2006
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.
 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      06-28-2006

"Daniel Marques" <(E-Mail Removed)> 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(1;
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


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      06-28-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
container inside container in stl wolverine C++ 2 07-24-2006 03:08 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
item callback on container or why STL does not have pointer to iterator conversion open? forester C++ 16 08-25-2005 03:37 PM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments