473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Pointer to container in STL

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

Jun 28 '06 #1
4 4002

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

Jun 28 '06 #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
}


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.
Jun 28 '06 #3

"Daniel Marques" <da***********@gmail.com> 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
Jun 28 '06 #4
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.
Jun 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Vivi Orunitia | last post by:
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs...
3
by: jjleto | last post by:
I have just learn about "pointer to member". Does someone have an actual example where "pointers to member" are usefull or necessary ? Can't always &(myClass.member) be used ? Thank you.
16
by: forester | last post by:
lets say its common situation when object have subobjects in container and receives callbacks from contained items. and object want to move objects in containers on signal(callback). iterator is...
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
19
by: Michael | last post by:
Hi, typedef std::vector<Vehicle* VehicleList; Vehicle is a UDT, or a class. Why is Vehicle* not just Vehicle in < >. Which one is better and why? If you could explain it by laying out some...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
8
by: mathieu | last post by:
Hi there I have implemented a very simple smartpointer class (invasive design). And I was wondering what should be the natural API when using those in a Container. I choose to define the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.