Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   std::map<> problem (http://www.velocityreviews.com/forums/t955504-std-map-problem.html)

 jski 12-14-2012 11:10 PM

std::map<> problem

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

I'm using the following declarations:

struct point
{
double alpha, beta;
double Force;

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

struct node
{
int XYZ;
vector<point> pt;
};

map<int, node> AeroDOF2;

I'm looping thru a file to populate AeroDOF2:

for( i=1; i<=AlphaBetaBlocks; ++i)
for( j=1; j<=No_DOF; ++j )
{
AEROfile >> AeroDOF[j] >> alpha[i] >> beta[i] >> Force[i][j] >>
XYZ[j] >> X[j] >> Y[j] >> Z[j] >> Node[j];
AeroDOF2[ AeroDOF[j] ].XYZ = XYZ[j];
AeroDOF2[ AeroDOF[j] ].pt.push_back( point( alpha[i], beta[i],
Force[i][j] ) );
}

After successful looping for awhile, I get to j == 110 and die with a
SEG FAULT at:
AeroDOF2[ AeroDOF[j] ].XYZ = XYZ[j];
where:
AeroDOF[110] ==415 and XYZ[110] == 1 (according to GDB)

Is there a problem with my use of the STL map template?

---John

 jski 12-14-2012 11:42 PM

Re: std::map<> problem

On Dec 14, 6:10*pm, jski <john.chludzin...@gmail.com> wrote:
> First:
> \$ g++ --version
> g++ (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
>
> I'm using the following declarations:
>
> struct point
> {
> * double alpha, beta;
> * double Force;
>
> * point( double a, double b, double f ) : alpha(a), beta(b), Force(f)
> {}
>
> };
>
> struct node
> {
> * int XYZ;
> * vector<point> pt;
>
> };
>
> map<int, node> AeroDOF2;
>
> I'm looping thru a file to populate AeroDOF2:
>
> * for( i=1; i<=AlphaBetaBlocks; ++i)
> * * for( j=1; j<=No_DOF; ++j )
> * * * {
> * * * * AEROfile >> AeroDOF[j] >> alpha[i] >> beta[i] >> Force[i][j] >>
> XYZ[j] >> X[j] >> Y[j] >> Z[j] >> Node[j];
> * * * * AeroDOF2[ AeroDOF[j] ].XYZ = XYZ[j];
> * * * * AeroDOF2[ AeroDOF[j] ].pt.push_back( point( alpha[i], beta[i],
> Force[i][j] ) );
> * * * }
>
> After successful looping for awhile, I get to j == 110 and die with a
> SEG FAULT at:
> * * * * AeroDOF2[ AeroDOF[j] ].XYZ = XYZ[j];
> where:
> * * * * AeroDOF[110] ==415 and XYZ[110] == 1 (according to GDB)
>
> Is there a problem with my use of the STL map template?
>
> ---John

I replaced the loop with:

for( i=1; i<=AlphaBetaBlocks; ++i)
for( j=1; j<=No_DOF; ++j )
{
AEROfile >> AeroDOF >> alpha >> beta >> Force >> XYZ >> X >> Y >> Z
>> N;

//AEROfile >> AeroDOF[j] >> alpha[i] >> beta[i] >> Force[i][j] >>
XYZ[j] >> X[j] >> Y[j] >> Z[j] >> NNode[j];
AeroDOF2[ AeroDOF ].XYZ = XYZ;
AeroDOF2[ AeroDOF ].pt.push_back( point( alpha, beta, Force ) );
}

Just using local variables to store the data read from the file - NOT
the arrays and it appears to work. Not sure why?

---John

 Ian Collins 12-14-2012 11:45 PM

Re: std::map<> problem

jski wrote:
> First:
> \$ g++ --version
> g++ (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)
>
> I'm using the following declarations:
>
> struct point
> {
> double alpha, beta;
> double Force;
>
> point( double a, double b, double f ) : alpha(a), beta(b), Force(f)
> {}
> };
>
> struct node
> {
> int XYZ;
> vector<point> pt;
> };

General comment: Mixing naming styles (caps, mixed caps etc.) for the
same thing is a bad idea!

> map<int, node> AeroDOF2;
>
> I'm looping thru a file to populate AeroDOF2:
>
> for( i=1; i<=AlphaBetaBlocks; ++i)
> for( j=1; j<=No_DOF; ++j )
> {
> AEROfile >> AeroDOF[j] >> alpha[i] >> beta[i] >> Force[i][j] >>
> XYZ[j] >> X[j] >> Y[j] >> Z[j] >> Node[j];

Do you know if these arrays are in bounds?

> AeroDOF2[ AeroDOF[j] ].XYZ = XYZ[j];
> AeroDOF2[ AeroDOF[j] ].pt.push_back( point( alpha[i], beta[i],
> Force[i][j] ) );
> }
>
> After successful looping for awhile, I get to j == 110 and die with a
> SEG FAULT at:
> AeroDOF2[ AeroDOF[j] ].XYZ = XYZ[j];
> where:
> AeroDOF[110] ==415 and XYZ[110] == 1 (according to GDB)

Where does the fault occur?

Have you tried valgrind?

--
Ian Collins

 jski 12-15-2012 12:04 AM

Re: std::map<> problem

On Dec 14, 6:42*pm, jski <john.chludzin...@gmail.com> wrote:
> On Dec 14, 6:10*pm, jski <john.chludzin...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > First:
> > \$ g++ --version
> > g++ (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2)

>
> > I'm using the following declarations:

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

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

>
> > };

>
> > struct node
> > {
> > * int XYZ;
> > * vector<point> pt;

>
> > };

>
> > map<int, node> AeroDOF2;

>
> > I'm looping thru a file to populate AeroDOF2:

>
> > * for( i=1; i<=AlphaBetaBlocks; ++i)
> > * * for( j=1; j<=No_DOF; ++j )
> > * * * {
> > * * * * AEROfile >> AeroDOF[j] >> alpha[i] >> beta[i] >> Force[i][j] >>
> > XYZ[j] >> X[j] >> Y[j] >> Z[j] >> Node[j];
> > * * * * AeroDOF2[ AeroDOF[j] ].XYZ = XYZ[j];
> > * * * * AeroDOF2[ AeroDOF[j] ].pt.push_back( point( alpha[i], beta[i],
> > Force[i][j] ) );
> > * * * }

>
> > After successful looping for awhile, I get to j == 110 and die witha
> > SEG FAULT at:
> > * * * * AeroDOF2[ AeroDOF[j] ].XYZ = XYZ[j];
> > where:
> > * * * * AeroDOF[110] ==415 and XYZ[110] == 1 (accordingto GDB)

>
> > Is there a problem with my use of the STL map template?

>
> > ---John

>
> I replaced the loop with:
>
> * for( i=1; i<=AlphaBetaBlocks; ++i)
> * * for( j=1; j<=No_DOF; ++j )
> * * * {
> * * * * AEROfile >> AeroDOF >> alpha >> beta >> Force >> XYZ >> X>> Y >> Z>> N;
>
> * * * * //AEROfile >> AeroDOF[j] >> alpha[i] >> beta[i] >> Force[i][j] >>
> XYZ[j] >> X[j] >> Y[j] >> Z[j] >> NNode[j];
> * * * * AeroDOF2[ AeroDOF ].XYZ = XYZ;
> * * * * AeroDOF2[ AeroDOF ].pt.push_back( point( alpha, beta, Force ) );
> * * * }
>
> Just using local variables to store the data read from the file - NOT
> the arrays and it appears to work. *Not sure why?
>
> ---John

Found an array, AeroDOF, that was dimensioned with too small a value.

 All times are GMT. The time now is 09:12 AM.