472,978 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 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 1678
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 oriente objet/
* * * * * * * * * *Beratung in objektorientierter Datenverarbeitung
9 place Smard, 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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.