473,320 Members | 1,947 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,320 software developers and data experts.

Trouble with vector::iterator

Hi,

I have been using vector::iterators for a while now. This is the first
time I have encountered this problem.
The vector contains one element.

1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
4 cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}

Line 4 throws me segmentation fault.

However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;

So I know that there is no problem with the element. Am I missing
something ?
Nov 19 '08 #1
10 1690
Prasad wrote:
I have been using vector::iterators for a while now. This is the first
time I have encountered this problem.
The vector contains one element.

1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
4 cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}

Line 4 throws me segmentation fault.

However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;

So I know that there is no problem with the element. Am I missing
something ?
No, you don't know if there's a problem with the element. In your second
version (with 'at') you create a _copy_ of the vector element, while in
the first version you access the vector element itself. The process of
creating a copy can possibly masquerade the existing problem with the
vector element. Try this

GroupSetTemplate& element =
invariantState.getGroupSetTemplates().at(0);
cout << "Gst name" << element.getName() << endl;

(note the use of reference) and see if it works fine or also throws a
segfault. This would be a better test, compared to your version with a copy.

Otherwise, the code you provided looks fine. The problem must be elsewhere.

--
Best regards,
Andrey Tarasevich
Nov 19 '08 #2
On Nov 18, 7:17 pm, Prasad <prasadmpa...@gmail.comwrote:
Hi,

I have been using vector::iterators for a while now. This is the first
time I have encountered this problem.
The vector contains one element.

1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
4 cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}

Line 4 throws me segmentation fault.

However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;

So I know that there is no problem with the element. Am I missing
something ?
As already noted, at(0) returns a copy. Your original code involves
iterators. Lets bet member function getGroupSetTemplates() returns a
local variable (hence the seg fault).

The fact that you have a conditional expression in that for loop like
so:

gstIt!=this->invariantState.getGroupSetTemplates().end()

is indicative of poor programming practices.
You could always try a simplified reconstruction of your class
hierarchy and post a short, compilable snippet.

Nov 19 '08 #3
On Nov 19, 4:09 am, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 18, 7:17 pm, Prasad <prasadmpa...@gmail.comwrote:
I have been using vector::iterators for a while now. This is
the first time I have encountered this problem.
The vector contains one element.
1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
4 cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}
Line 4 throws me segmentation fault.
However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;
So I know that there is no problem with the element. Am I
missing something ?
As already noted, at(0) returns a copy.
No. The function vector<>::at() returns a reference. He makes
the copy afterwards.
Your original code involves iterators. Lets bet member
function getGroupSetTemplates() returns a local variable
(hence the seg fault).
Returns a local variable, or returns a reference to a local
variable. Either could certainly explain his symptoms. (So
could a few other things, but that sounds like a pretty good
guess.)
The fact that you have a conditional expression in that for
loop like so:
gstIt!=this->invariantState.getGroupSetTemplates().end()
is indicative of poor programming practices.
Why? I'd say it was more or less standard practice. In fact, a
for loop without a conditional expression would be an endless
loop. (It's not standard practice to consistently write out the
this->, of course, but maybe this code is in a template, and
invariantState is a member of a dependent base class.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 19 '08 #4
On Nov 19, 3:33*am, James Kanze <james.ka...@gmail.comwrote:
On Nov 19, 4:09 am, Salt_Peter <pj_h...@yahoo.comwrote:
On Nov 18, 7:17 pm, Prasad <prasadmpa...@gmail.comwrote:
I have been using vector::iterators for a while now. This is
the first time I have encountered this problem.
The vector contains one element.
1. vector<GroupSetTemplate>::iterator gstIt;
2. for(gstIt= this->invariantState.getGroupSetTemplates().begin();
gstIt!=this->invariantState.getGroupSetTemplates().end();gstIt ++)
3{
*4 * * * * * *cout<<"Gst name"<<(*gstIt).getName()<<endl;
5}
Line 4 throws me segmentation fault.
However if I rewrite the above code using vector::at() it works fine.
GroupSetTemplate element = invariantState.getGroupSetTemplates().at
(0);
cout<<"Gst name"<<element.getName()<<endl;
So I know that there is no problem with the element. Am I
missing something ?
As already noted, at(0) returns a copy.

No. The function vector<>::at() returns a reference. *He makes
the copy afterwards.
Your original code involves iterators. Lets bet member
function getGroupSetTemplates() returns a local variable
(hence the seg fault).

Returns a local variable, or returns a reference to a local
variable. *Either could certainly explain his symptoms. *(So
could a few other things, but that sounds like a pretty good
guess.)
getGroupSetTemplates() returns a class member. Is that a problem?

>
The fact that you have a conditional expression in that for
loop like so:
gstIt!=this->invariantState.getGroupSetTemplates().end()
is indicative of poor programming practices.

Why? *I'd say it was more or less standard practice. *In fact, a
for loop without a conditional expression would be an endless
loop. *(It's not standard practice to consistently write out the
this->, of course, but maybe this code is in a template, and
invariantState is a member of a dependent base class.)

--
James Kanze (GABI Software) * * * * * * email:james.ka...@gmail.com
Conseils en informatique orientée objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 20 '08 #5
Prasad wrote:
getGroupSetTemplates() returns a class member. Is that a problem?
What does "returns a class member" mean? Does it return a copy of a
class member? Or does it return a reference to a class member? It is a
problem if the former is true.

Did you make a test with 'at' and reference?

--
Best regards,
Andrey Tarasevich
Nov 20 '08 #6
On Nov 19, 8:06*pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
Prasad wrote:
getGroupSetTemplates() returns a class member. Is that a problem?

What does "returns a class member" mean? Does it return a copy of a
class member? Or does it return a reference to a class member? It is a
problem if the former is true.

Did you make a test with 'at' and reference?

--
Best regards,
Andrey Tarasevich
Yes. The test with reference fails too. Any reason why the exact same
calls fail for reference but work for the copy?
Infact a valgrind analysis revealed a problem with string access,
which is in one of the innermost classes.

==32069== Process terminating with default action of signal 11
(SIGSEGV)
==32069== Access not within mapped region at address 0xFFFFFFFC
==32069== at 0x40CCD3A: std::string::string(std::string const&)
(in /usr/lib/libstdc++.so.6.0.9)

Any idea what this error means? AFAIK I am returning any local
references or variables.

~Prasad
Nov 20 '08 #7
On Nov 19, 8:06*pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
Prasad wrote:
getGroupSetTemplates() returns a class member. Is that a problem?

What does "returns a class member" mean? Does it return a copy of a
class member? Or does it return a reference to a class member? It is a
problem if the former is true.
I was able to fix the problem.
//Original code
vector<GroupSetTemplatesgetGroupSetTemplates()
{
return this->groupSetTemplates;
}

//Fixed code
vector<GroupSetTemplates>& getGroupSetTemplates()
{
return this->groupSetTemplates;
}

My understanding of this is
1. Initially a copy of groupSetTemplates was being returned. I was
getting an error because I was creating an iterator over returned
copy.
2. I am returning a reference of groupSetTemplates. A copy of the
reference is returned which still points to original groupSetTemplate
and hence my code works fine.

Is this understanding correct?
>
Did you make a test with 'at' and reference?

--
Best regards,
Andrey Tarasevich
Nov 20 '08 #8
Prasad <pr**********@gmail.comkirjutas:
On Nov 19, 8:06*pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
>Prasad wrote:
getGroupSetTemplates() returns a class member. Is that a problem?

What does "returns a class member" mean? Does it return a copy of a
class member? Or does it return a reference to a class member? It is a
problem if the former is true.

I was able to fix the problem.
//Original code
vector<GroupSetTemplatesgetGroupSetTemplates()
{
return this->groupSetTemplates;
}

//Fixed code
vector<GroupSetTemplates>& getGroupSetTemplates()
{
return this->groupSetTemplates;
}

My understanding of this is
1. Initially a copy of groupSetTemplates was being returned. I was
getting an error because I was creating an iterator over returned
copy.
The problem was that you returned two copies of groupSetTemplates, and
you were attempting to iterate from the beginning of the first to the end
of the second.
2. I am returning a reference of groupSetTemplates. A copy of the
reference is returned which still points to original groupSetTemplate
and hence my code works fine.

Is this understanding correct?
Basically yes, this is what Andray has tried to talk you.

Terminological note: usually one does not talk about a "copy of a
reference" (though there might be some copying of bits involved in the
CPU level, but this is irrelevant). If the function returns a reference,
it just returns a reference.

Paavo
Nov 20 '08 #9
On Nov 20, 12:41*am, Paavo Helde <pa...@nospam.please.orgwrote:
Prasad <prasadmpa...@gmail.comkirjutas:
On Nov 19, 8:06*pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
Prasad wrote:
getGroupSetTemplates() returns a class member. Is that a problem?
What does "returns a class member" mean? Does it return a copy of a
class member? Or does it return a reference to a class member? It is a
problem if the former is true.
I was able to fix the problem.
//Original code
vector<GroupSetTemplatesgetGroupSetTemplates()
{
return this->groupSetTemplates;
}
//Fixed code
vector<GroupSetTemplates>& getGroupSetTemplates()
{
return this->groupSetTemplates;
}
My understanding of this is
1. Initially a copy of groupSetTemplates was being returned. I was
getting an error because I was creating an iterator over returned
copy.

The problem was that you returned two copies of groupSetTemplates, and
you were attempting to iterate from the beginning of the first to the end
of the second.
2. I am returning a reference of groupSetTemplates. A copy of the
reference is returned which still points to original groupSetTemplate
and hence my code works fine.
Is this understanding correct?

Basically yes, this is what Andray has tried to talk you.

Terminological note: usually one does not talk about a "copy of a
reference" (though there might be some copying of bits involved in the
CPU level, but this is irrelevant). If the function returns a reference,
it just returns a reference.

Paavo
Thanks Paavo and Andray :)
Nov 20 '08 #10
Prasad <pr**********@gmail.comkirjutas:
On Nov 20, 12:41*am, Paavo Helde <pa...@nospam.please.orgwrote:
>>
Basically yes, this is what Andray has tried to talk you.

Thanks Paavo and Andray :)
Sorry Andrey, for misspelling your name (and having been copied!)

Paavo
Nov 20 '08 #11

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

Similar topics

2
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0),...
3
by: Jim Luedtke | last post by:
Can someone help me understand what might be going wrong here? I'm writing a class template that looks something like this; /* myclass.h */ #include<vector> using namespace std template...
12
by: Raider | last post by:
Why it's impossible for compiler to implicitly cast vector<>::iterator to void*? I have: void f(void *, size_t); .... vector<int> v; v.reserve(n); f(v.begin(), n * sizeof(int));
4
by: Johan Pettersson | last post by:
Hi, I'm trying to "port" a project from VC++ 2003 to VC++ 2005 (Express Edition). This project contains the following code on several places (It is not exactly this code but a generalization of...
1
by: Daniel.Wyatt | last post by:
I need some help with this please. Here's the important part: #include <vector> #include <string> ................ template <class T> bool ResourceManager<T>::isLoaded(const std::string...
4
by: propokergrad | last post by:
Hello, say I have two classes: class Base{...}; class Derived : public Base{...} I would like to do something similar to this: std::vector<Base*>::iterator b;...
3
by: T. Crane | last post by:
Hi all, I have some data that I am using a vector<vector<double container to hold. The data is n sets of (x,y,z,intensity) data points. I can either group my data like this: ...
8
by: JackC | last post by:
Hi, If i have a std::vector containing pointers, then iterate through the vector and im slightly confused as to how i use the item from the iterator? for example, vector<MyClass*myvector;...
7
by: Ralf Goertz | last post by:
Hi, the following templated code doesn't compile. It gives the error: template_it.cc:17: error: type 'std::vector<Derived1<T>*, std::allocator<Derived1<T>*' is not derived from type...
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
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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...

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.