Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL Vector question?

Reply
Thread Tools

STL Vector question?

 
 
Elzbieta Burnett
Guest
Posts: n/a
 
      12-17-2012
I have:

struct point
{
double alpha, beta;
double Force;

point( double a, double b, double f ) : alpha(a), beta(b), Force(f)
{}
};

struct data
{
int dof;
int XYZ;
double X, Y, Z;
int node;
vector<point> pt;
};

map<int, data> AeroDOF;

I my initialization code I had:

AeroDOF[ dof[j] ].pt.push_back( point( alpha[i], beta[i],
Force[i][j] ) );

I thought that vector '[]' would create an instance of 'point' (like
map '[]'), call copy constructor and assign newly created entry to
point( alpha[i], beta[i], Force[i][j] ) in AeroDOF.pt.

Evidently this isn't true?

<Ella>
 
Reply With Quote
 
 
 
 
jski
Guest
Posts: n/a
 
      12-17-2012
On Dec 17, 2:55*pm, Elzbieta Burnett <(E-Mail Removed)>
wrote:
> I have:
>
> struct point
> {
> * double alpha, beta;
> * double Force;
>
> * point( double a, double b, double f ) : alpha(a), beta(b), Force(f)
> {}
>
> };
>
> struct data
> {
> * int dof;
> * int XYZ;
> * double X, Y, Z;
> * int node;
> * vector<point> pt;
>
> };
>
> map<int, data> AeroDOF;
>
> I my initialization code I had:
>
> * * * * AeroDOF[ dof[j] ].pt.push_back( point( alpha[i], beta[i],
> Force[i][j] ) );
>
> I thought that vector '[]' would create an instance of 'point' (like
> map '[]'), call copy constructor and assign newly created entry to
> point( alpha[i], beta[i], Force[i][j] ) in AeroDOF.pt.
>
> Evidently this isn't true?
>
> <Ella>


Try:

struct data
{
int dof;
int XYZ;
double X, Y, Z;
int node;
vector<point *> pt;

};

AeroDOF[ dof[j] ].pt.push_back( new point( alpha[i], beta[i], Force[i]
[j] ) );


Not sure what the answer to your question though.

---John
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-17-2012
Elzbieta Burnett wrote:
> I have:
>
> struct point
> {
> double alpha, beta;
> double Force;
>
> point( double a, double b, double f ) : alpha(a), beta(b), Force(f)
> {}
> };
>
> struct data
> {
> int dof;
> int XYZ;
> double X, Y, Z;
> int node;
> vector<point> pt;
> };
>
> map<int, data> AeroDOF;
>
> I my initialization code I had:
>
> AeroDOF[ dof[j] ].pt.push_back( point( alpha[i], beta[i],
> Force[i][j] ) );
>
> I thought that vector '[]' would create an instance of 'point' (like
> map '[]'), call copy constructor and assign newly created entry to
> point( alpha[i], beta[i], Force[i][j] ) in AeroDOF.pt.
>
> Evidently this isn't true?


The evidence has led you the the correct conclusion! The [] operator on
std::vector does not allocate storage.

--
Ian Collins
 
Reply With Quote
 
Elzbieta Burnett
Guest
Posts: n/a
 
      12-17-2012
On Dec 17, 3:33*pm, Paavo Helde <(E-Mail Removed)> wrote:
> Elzbieta Burnett <(E-Mail Removed)> wrote in news:735599ee-
> (E-Mail Removed):
>
>
>
>
>
>
>
>
>
> > I have:

>
> > struct point
> > {
> > * double alpha, beta;
> > * double Force;

>
> > * point( double a, double b, double f ) : alpha(a), beta(b), Force(f)
> > {}
> > };

>
> > struct data
> > {
> > * int dof;
> > * int XYZ;
> > * double X, Y, Z;
> > * int node;
> > * vector<point> pt;
> > };

>
> > map<int, data> AeroDOF;

>
> > I my initialization code I had:

>
> > * * * * AeroDOF[ dof[j] ].pt.push_back( point( alpha[i], beta[i],
> > Force[i][j] ) );

>
> Your conclusion is right, but it does not follow from the above line. In
> this line [] is used a lot, but the only case where the container type is
> known it is a map (AeroDOF). All other variables where [] is applied to
> are undefined/undeclared (dof, alpha, beta, Force) in your example and so
> it is not clear if they are vectors, not to speak about how large they
> are and if they are accessed out of bounds.
>
>
>
> > I thought that vector '[]' would create an instance of 'point' (like
> > map '[]'), call copy constructor and assign newly created entry to
> > point( alpha[i], beta[i], Force[i][j] ) in AeroDOF.pt.

>
> On pt you are using push_back() which works more or less exactly as you
> describe. Your problem is probably elsewhere.
>
> Cheers
> Paavo


If I use solution proposed by John:

vector<point *> pt)
AeroDOF[ dof[j] ].pt.push_back( new point( alpha[i], beta[i], Force[i]
[j] ) );

this seems to work. Is there problem with this solution?

<Ella>
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      12-17-2012
On 12/17/2012 4:34 PM, Elzbieta Burnett wrote:
> On Dec 17, 3:33 pm, Paavo Helde <(E-Mail Removed)> wrote:
>> Elzbieta Burnett <(E-Mail Removed)> wrote in news:735599ee-
>> (E-Mail Removed):
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> I have:

>>
>>> struct point
>>> {
>>> double alpha, beta;
>>> double Force;

>>
>>> point( double a, double b, double f ) : alpha(a), beta(b), Force(f)
>>> {}
>>> };

>>
>>> struct data
>>> {
>>> int dof;
>>> int XYZ;
>>> double X, Y, Z;
>>> int node;
>>> vector<point> pt;
>>> };

>>
>>> map<int, data> AeroDOF;

>>
>>> I my initialization code I had:

>>
>>> AeroDOF[ dof[j] ].pt.push_back( point( alpha[i], beta[i],
>>> Force[i][j] ) );

>>
>> Your conclusion is right, but it does not follow from the above line. In
>> this line [] is used a lot, but the only case where the container type is
>> known it is a map (AeroDOF). All other variables where [] is applied to
>> are undefined/undeclared (dof, alpha, beta, Force) in your example and so
>> it is not clear if they are vectors, not to speak about how large they
>> are and if they are accessed out of bounds.
>>
>>
>>
>>> I thought that vector '[]' would create an instance of 'point' (like
>>> map '[]'), call copy constructor and assign newly created entry to
>>> point( alpha[i], beta[i], Force[i][j] ) in AeroDOF.pt.

>>
>> On pt you are using push_back() which works more or less exactly as you
>> describe. Your problem is probably elsewhere.
>>
>> Cheers
>> Paavo

>
> If I use solution proposed by John:
>
> vector<point *> pt)
> AeroDOF[ dof[j] ].pt.push_back( new point( alpha[i], beta[i], Force[i]
> [j] ) );
>
> this seems to work. Is there problem with this solution?


Only if you consider unnecessary use of pointers a problem. Try

vector<point> pt; // same as you had before
...
... pt.push_back(point(alpha[i]...

I.e. without the pointers and without the 'new'.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Elzbieta Burnett
Guest
Posts: n/a
 
      12-17-2012
On Dec 17, 4:47*pm, Paavo Helde <(E-Mail Removed)> wrote:
> Elzbieta Burnett <(E-Mail Removed)> wrote innews:(E-Mail Removed):
>
>
>
> > If I use solution proposed by John:

>
> > vector<point *> pt)
> > AeroDOF[ dof[j] ].pt.push_back( new point( alpha[i], beta[i], Force[i]
> > [j] ) );

>
> > this seems to work. *Is there problem with this solution?

>
> You have not told what problem you have, neither provided a sufficiently
> complete example demonstrating the problem. Right now I cannot see how
> using pointers instead of value objects could make anything better here,
> this would only make working with the data more cumbersome and possibly
> slower, in addition to creating a risk of memory leaks.
>
> Cheers
> Paavo


When iterating:

void AERODATA::Interpolate()
{
int i=0;
....
for( map<int, data>::iterator ii = AeroDOF.begin(); ii !=
AeroDOF.end(); ++ii)
{

A[i].Force = interpextrap( (ii->second).pt[0], //PT1
(ii->second).pt[3], //PT2
(ii->second).pt[1], //PT3
(ii->second).pt[4], //PT4
alphaNew,
betaNew );
i++;

}
....
}

I would set breakpoint on interpextrap(...) and examine alpha and beta
after many calls to AERODATA::Interpolate. These values would be
completely different from what is read from the file. When I used
pointers and allocated "points", this problem went away.

<Ella>
 
Reply With Quote
 
Elzbieta Burnett
Guest
Posts: n/a
 
      12-17-2012
On Dec 17, 5:02*pm, Elzbieta Burnett <(E-Mail Removed)>
wrote:
> On Dec 17, 4:47*pm, Paavo Helde <(E-Mail Removed)> wrote:
>
>
>
>
>
>
>
>
>
> > Elzbieta Burnett <(E-Mail Removed)> wrote innews:(E-Mail Removed):

>
> > > If I use solution proposed by John:

>
> > > vector<point *> pt)
> > > AeroDOF[ dof[j] ].pt.push_back( new point( alpha[i], beta[i], Force[i]
> > > [j] ) );

>
> > > this seems to work. *Is there problem with this solution?

>
> > You have not told what problem you have, neither provided a sufficiently
> > complete example demonstrating the problem. Right now I cannot see how
> > using pointers instead of value objects could make anything better here,
> > this would only make working with the data more cumbersome and possibly
> > slower, in addition to creating a risk of memory leaks.

>
> > Cheers
> > Paavo

>
> When iterating:
>
> void AERODATA::Interpolate()
> {
> * int i=0;
> ...
> * for( map<int, data>::iterator ii = AeroDOF.begin(); ii !=
> AeroDOF.end(); ++ii)
> * * {
>
> * * * A[i].Force = interpextrap( (ii->second).pt[0], *//PT1
> * * * * * * * * * * * * * * * * *(ii->second).pt[3], *//PT2
> * * * * * * * * * * * * * * * * *(ii->second).pt[1], *//PT3
> * * * * * * * * * * * * * * * * *(ii->second).pt[4], *//PT4
> * * * * * * * * * * * * * * * * *alphaNew,
> * * * * * * * * * * * * * * * * *betaNew );
> * * * i++;
>
> * * }
> ...
>
> }
>
> I would set breakpoint on interpextrap(...) and examine alpha and beta
> after many calls to AERODATA::Interpolate. These values would be
> completely different from what is read from the file. *When I used
> pointers and allocated "points", this problem went away.
>
> <Ella>


BTW, ONLY time values to AeroDOF[].pt[] are set is when reading from
data file - one time. They should never change.

<Ella>
 
Reply With Quote
 
Elzbieta Burnett
Guest
Posts: n/a
 
      12-17-2012
On Dec 17, 5:13*pm, Elzbieta Burnett <(E-Mail Removed)>
wrote:
> On Dec 17, 5:02*pm, Elzbieta Burnett <(E-Mail Removed)>
> wrote:
>
>
>
>
>
>
>
>
>
> > On Dec 17, 4:47*pm, Paavo Helde <(E-Mail Removed)> wrote:

>
> > > Elzbieta Burnett <(E-Mail Removed)> wrote innews:(E-Mail Removed):

>
> > > > If I use solution proposed by John:

>
> > > > vector<point *> pt)
> > > > AeroDOF[ dof[j] ].pt.push_back( new point( alpha[i], beta[i], Force[i]
> > > > [j] ) );

>
> > > > this seems to work. *Is there problem with this solution?

>
> > > You have not told what problem you have, neither provided a sufficiently
> > > complete example demonstrating the problem. Right now I cannot see how
> > > using pointers instead of value objects could make anything better here,
> > > this would only make working with the data more cumbersome and possibly
> > > slower, in addition to creating a risk of memory leaks.

>
> > > Cheers
> > > Paavo

>
> > When iterating:

>
> > void AERODATA::Interpolate()
> > {
> > * int i=0;
> > ...
> > * for( map<int, data>::iterator ii = AeroDOF.begin(); ii !=
> > AeroDOF.end(); ++ii)
> > * * {

>
> > * * * A[i].Force = interpextrap( (ii->second).pt[0], *//PT1
> > * * * * * * * * * * * * * * * * *(ii->second).pt[3], *//PT2
> > * * * * * * * * * * * * * * * * *(ii->second).pt[1], *//PT3
> > * * * * * * * * * * * * * * * * *(ii->second).pt[4], *//PT4
> > * * * * * * * * * * * * * * * * *alphaNew,
> > * * * * * * * * * * * * * * * * *betaNew );
> > * * * i++;

>
> > * * }
> > ...

>
> > }

>
> > I would set breakpoint on interpextrap(...) and examine alpha and beta
> > after many calls to AERODATA::Interpolate. These values would be
> > completely different from what is read from the file. *When I used
> > pointers and allocated "points", this problem went away.

>
> > <Ella>

>
> BTW, ONLY time values to AeroDOF[].pt[] are set is when reading from
> data file - one time. They should never change.
>
> <Ella>


I am using:

$ g++ --version
g++ (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)

<Ella>
 
Reply With Quote
 
Elzbieta Burnett
Guest
Posts: n/a
 
      12-17-2012
On Dec 17, 5:37*pm, Paavo Helde <(E-Mail Removed)> wrote:
> Elzbieta Burnett <(E-Mail Removed)> wrote innews:(E-Mail Removed):
>
>
>
>
>
>
>
>
>
> > On Dec 17, 4:47*pm, Paavo Helde <(E-Mail Removed)> wrote:
> >> Elzbieta Burnett <(E-Mail Removed)> wrote
> >> innews:da7c9ac3-565e-

> > (E-Mail Removed):

>
> >> > If I use solution proposed by John:

>
> >> > vector<point *> pt)
> >> > AeroDOF[ dof[j] ].pt.push_back( new point( alpha[i], beta[i],
> >> > Force[i] [j] ) );

>
> >> > this seems to work. *Is there problem with this solution?

>
> >> You have not told what problem you have, neither provided a
> >> sufficiently complete example demonstrating the problem. Right now I
> >> cannot see how using pointers instead of value objects could make
> >> anything better here, this would only make working with the data more
> >> cumbersome and possibly slower, in addition to creating a risk of
> >> memory leaks.

>
> >> Cheers
> >> Paavo

>
> > When iterating:

>
> > void AERODATA::Interpolate()
> > {
> > * int i=0;
> > ...
> > * for( map<int, data>::iterator ii = AeroDOF.begin(); ii
> > * !AeroDOF.end(); ++ii)
> > * * {

>
> > * * * A[i].Force = interpextrap( (ii->second).pt[0], *//PT1
> > * * * * * * * * * * * * * * * * *(ii->second).pt[3], *//PT2
> > * * * * * * * * * * * * * * * * *(ii->second).pt[1], *//PT3
> > * * * * * * * * * * * * * * * * *(ii->second).pt[4], *//PT4
> > * * * * * * * * * * * * * * * * *alphaNew,
> > * * * * * * * * * * * * * * * * *betaNew );
> > * * * i++;

>
> > * * }
> > ...
> > }

>
> > I would set breakpoint on interpextrap(...) and examine alpha and beta
> > after many calls to AERODATA::Interpolate. These values would be
> > completely different from what is read from the file. *When I used
> > pointers and allocated "points", this problem went away.

>
> These symptoms would be explained if you have a custom copy constructor
> for the point class and which does not do a proper copy (the push_back()
> operation may copy data around and would use this). Or maybe there is
> some other error. Why do you check data only "after many calls" and not
> immediately after reading in the file? Are you sure that the pt vector
> actually contains at least 5 elements in the above code?
>
> For catching unexpected data changes the data breakpoints are great, BTW.
>
> hth
> Paavo


I have conditional logic that end of interpextrap(...) to catch
problems. When I saw I had problem, I set conditional breakpoint in
GDB and examined these values. They should only be -6, 0, or 6. But
instead they were huge, ridiculous values.

<Ella>
 
Reply With Quote
 
Elzbieta Burnett
Guest
Posts: n/a
 
      12-18-2012
On Dec 17, 5:55*pm, Paavo Helde <(E-Mail Removed)> wrote:
> Elzbieta Burnett <(E-Mail Removed)> wrote innews:(E-Mail Removed):
>
> > I have conditional logic that end of interpextrap(...) to catch
> > problems. When I saw I had problem, I set conditional breakpoint in
> > GDB and examined these values. *They should only be -6, 0, or 6. *But
> > instead they were huge, ridiculous values.

>
> Sounds like you are reading uninitialized values. But note that gdb can
> also report wrong values, especially in optimized code, do not trust the
> gdb debugger 100%. Make sure to compile your code without optimization for
> best debugging.
>
> As you are on Linux you can also try to run the program with valgrind and
> see if it spots any memory errors.
>
> hth
> Paavo


Just to make sure I understand all that's been said;

Original code:

struct point
{
double alpha, beta;
double Force;

point(double a, double b, double f) : alpha(a), beta(b), Force(f) {}

};

struct data
{
int dof;
int XYZ;
double X, Y, Z;
int node;
vector<point> pt;

};

map<int, data> AeroDOF;

AeroDOF[dof[j]].pt.push_back(point( alpha[i], beta[i],Force[i][j]));

This should work?

"Point" in:

AeroDOF[dof[j]].pt.push_back(point( alpha[i], beta[i],Force[i][j]));

is local to initialization routine but STL class (vector) should
create instance of "point" and copy contents from local "point" to new
entry in AeroDOF[].pt[] ? Vector values in AeroDOF[].pt[] should not
go away after call to initialize.

<Ella>

 
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
Problem storing tvmet vector objects in an stl vector container alexjcollins@gmail.com C++ 2 09-08-2008 09:18 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector of STL maps versus Vector of objects amolpan@gmail.com C++ 2 07-26-2005 10:16 PM
Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*> CD C++ 2 10-05-2004 02:29 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments