Pointer to container in STL

D

Daniel Marques

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
 
T

Thomas Tutone

Daniel said:
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
 
H

Henning Hasemann

Daniel said:
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.
 
R

Robbie Hatley

Daniel Marques said:
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
 
R

red floyd

Henning said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top