| View previous topic :: View next topic |
| Author |
Message |
hannibal200480@yahoo.fr Guest
|
Posted: Tue Jul 19, 2005 6:49 pm Post subject: vector |
|
|
Hi everyone,
Here about what it is.
vector<vector pVector0;
pVector0 is a vector of vector of 3D points.
pVector0[i] is a vector of 3D points.
pVector0[i][j] is a 3D point.
I wanted to add a point pt3D to the m vector in pVector0, I have
write these two instructions which belong to a C++ program.
vector< vector pVector0;
pVector0[m].push_back(pt3D);
I have as error message: Segmentation fault (core dumped)
Could anyone give me suggestions?
Thanks!
hannibal
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Tutone Guest
|
Posted: Wed Jul 20, 2005 9:41 am Post subject: Re: vector |
|
|
[email]hannibal200480 (AT) yahoo (DOT) fr[/email] wrote:
| Quote: | vector<vector pVector0;
pVector0 is a vector of vector of 3D points.
pVector0[i] is a vector of 3D points.
pVector0[i][j] is a 3D point.
I wanted to add a point pt3D to the m vector in pVector0
|
What m vector? Right now, pVector0 is empty - it does not contain any
vectors.
| Quote: | , I have
write these two instructions which belong to a C++ program.
vector< vector pVector0;
|
This creates an empty vector of empty vectors. BTW, I assume you meant
"Point3D" rather than "Vector3d."
| Quote: | pVector0[m].push_back(pt3D);
|
You're now executing push_back on the vector at position m of pVector0.
But pVector 0 is empty - so no matter what value m is, this is
undefined behavior.
| Quote: | I have as error message: Segmentation fault (core dumped)
|
Undefined behavior includes, among other things, segmentation faults.
| Quote: | Could anyone give me suggestions?
|
// create a vector<vector with m elements:
std::vector< std::vector p3VecVec(m);
// add a Point3d to the m'th element:
p3VecVec[m].push_back(pt3D);
Best regards,
Tom
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dan McLeran Guest
|
Posted: Wed Jul 20, 2005 9:42 am Post subject: Re: vector |
|
|
Most likely, you're accessing an element that does not exist. Try
replacing:
pVector0[m].push_back(pt3D);
with
pVector0.at(m).push_back(pt3D);
the .at member function will throw an exception if you're out of bounds.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alex Beluga Guest
|
Posted: Wed Jul 20, 2005 9:43 am Post subject: Re: vector |
|
|
Greetings.
Maybe you need to push vector<Vector3D> first, because what are you
calling -- unexisting matter?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Wed Jul 20, 2005 9:46 am Post subject: Re: vector |
|
|
[email]hannibal200480 (AT) yahoo (DOT) fr[/email] wrote:
Hi Hannibal,
| Quote: | Here about what it is.
vector<vector pVector0;
pVector0 is a vector of vector of 3D points.
|
is an empty vector
| Quote: | pVector0[i] is a vector of 3D points.
|
for every i, there is no vector, because pVector0 is empty.
| Quote: | pVector0[i][j] is a 3D point.
|
so there is no point, see above.
| Quote: | I wanted to add a point pt3D to the m vector in pVector0, I have
write these two instructions which belong to a C++ program.
vector< vector pVector0;
pVector0[m].push_back(pt3D);
I have as error message: Segmentation fault (core dumped)
|
try pVector0.at(m).push_back(pt3D);
very likely m is out bound of the vector and thus causing your seg
fault. In case that m is an index not within the vector, at(m) will
throw an exception. STLPort have a vector implementation that can be
configured to find errors of that kind.
best regards
Torsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Wed Jul 20, 2005 9:49 am Post subject: Re: vector |
|
|
[email]hannibal200480 (AT) yahoo (DOT) fr[/email] wrote:
| Quote: | Hi everyone,
Here about what it is.
vector<vector pVector0;
pVector0 is a vector of vector of 3D points.
pVector0[i] is a vector of 3D points.
pVector0[i][j] is a 3D point.
|
Without knowing anything about i and j's values, last two statements
are not necessarily true. The program should use asserts to test its
assumptions:
assert( i >= 0 and i < pVector0.size());
assert( j >= 0 and j < pVector0[i].size());
| Quote: | I wanted to add a point pt3D to the m vector in pVector0, I have
write these two instructions which belong to a C++ program.
vector< vector pVector0;
pVector0[m].push_back(pt3D);
|
likewise with this operation:
vector< vector pVector0;
assert( m >= 0 and m < pVector0.size());
pVector0[m].push_back(pt3D);
The assert conditions communicate that values provided for accessing
the vectors must fall within the range of valid indexes for the vector
on which it operates.
An alternate approach for this operation would be to accept an index of
any value and check its validity before using it. The function could
return an error to the client for out of range index values:
const long indexOutOfRangeErr = -1001;
vector< vector pVector0;
if (not( m >= 0 and m < pVector0.size()))
return indexOutOfRangeErr;
pVector0[m].push_back(pt3D);
Whether to use an assert or to test for the out-of-range condition
really depends on the program's interfaces and structure, and division
of responsibility. The specific choice is not all that important, what
is much more important is to decide on one or the other. If a caller
has only a vague assumption of what constitutes valid input to a
function, then there is a segmentation fault that is waiting to happen.
| Quote: | I have as error message: Segmentation fault (core dumped)
Could anyone give me suggestions?
Thanks!
hannibal
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Wed Jul 20, 2005 9:52 am Post subject: Re: vector |
|
|
[email]hannibal200480 (AT) yahoo (DOT) fr[/email] wrote:
| Quote: | I wanted to add a point pt3D to the m vector in pVector0, I have
write these two instructions which belong to a C++ program.
|
To add a point to a vector, you first have to have that vector:
| Quote: | vector< vector pVector0;
|
How many vectors of Vector3D does pVector0 have?
| Quote: | pVector0[m].push_back(pt3D);
|
Would the following work?
vector<int> a;
a[2000] = 0;
HTH,
Carlos
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
gmramaswamy@gmail.com Guest
|
Posted: Wed Jul 20, 2005 9:53 am Post subject: Re: vector |
|
|
i guess the problem is with this line
| Quote: | pVector0[m].push_back(pt3D);
In this case pVector0[m] will return another vector object in location |
'm' and push_back is called on that object. But since there is no
vector object in that location it dumps core. Vector, unlike arrays,
doesnt have any default size unless specified during creation of it.
the example below, does similar to what u do and it works.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< vector v1;
vector<int> v2;
vector<int> v3;
v2.push_back(10);
v2.push_back(20);
v2.push_back(30);
v3.push_back(100);
v3.push_back(200);
v3.push_back(300);
v1.push_back(v2);
v1.push_back(v3);
v1[0].push_back(40);
printf("some value v1[0][3]: %dn", v1[0][3]);
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Catalin Pitis Guest
|
Posted: Wed Jul 20, 2005 9:54 am Post subject: Re: vector |
|
|
[email]hannibal200480 (AT) yahoo (DOT) fr[/email] wrote:
| Quote: | Hi everyone,
Here about what it is.
vector<vector pVector0;
pVector0 is a vector of vector of 3D points.
pVector0[i] is a vector of 3D points.
pVector0[i][j] is a 3D point.
I wanted to add a point pt3D to the m vector in pVector0, I have
write these two instructions which belong to a C++ program.
vector< vector pVector0;
pVector0[m].push_back(pt3D);
I have as error message: Segmentation fault (core dumped)
Could anyone give me suggestions?
|
Looking at those two lines of code you gave, you initialized en empty
vector and you tried to set the m-th element of the vector, which leads
to writing into an unallocated memory, which leads to memory fault.
You should dimension your vector first...
Catalin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Jul 20, 2005 9:57 am Post subject: Re: vector |
|
|
[email]hannibal200480 (AT) yahoo (DOT) fr[/email] wrote:
| Quote: | vector< vector pVector0;
pVector0[m].push_back(pt3D);
I have as error message: Segmentation fault (core dumped)
|
The size of 'pVector0' is zero, so you can't access its first element.
std::vector only resizes explicitly, not implicitly by accessing an
element.
Other than that, you seriously need to learn to use a debugger to step
through a program - that way you could have told us that it fails in the
call to push_back() because it is called with a null 'this' pointer.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
zura.khetsuriani@gmail.co Guest
|
Posted: Wed Jul 20, 2005 2:05 pm Post subject: Re: vector |
|
|
pVector[m] ???
first allocate.
i.e.
vector< vector pVector0(m + 1);
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
titancipher Guest
|
Posted: Wed Jul 20, 2005 2:06 pm Post subject: Re: vector |
|
|
It could be one of two things. The naming of pVector0 seems to
indicate it is a pointer, in which case, you will have to check it has
value, and if so, then dereference it first: (*pVector). Or, it could
be that your Point3D allocates memory and does not have a copy
constructor. You should debug and find out where it is crashing.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
hongseok.yoon@gmail.com Guest
|
Posted: Wed Jul 20, 2005 2:10 pm Post subject: Re: vector |
|
|
You accessed invalid memory block.
There's no data such position *m* in pVector0.
You should do push_back() first.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Wei Guest
|
Posted: Wed Jul 20, 2005 2:17 pm Post subject: Re: vector |
|
|
| Quote: | vector< vector pVector0;
pVector0[m].push_back(pt3D);
|
Access vector elements with [] is unchecked. If pVector0.size() < (m +
1), there will be a seg dump, I think.
Wei
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Jul 20, 2005 2:18 pm Post subject: Re: vector |
|
|
[email]hannibal200480 (AT) yahoo (DOT) fr[/email] wrote:
| Quote: | Here about what it is.
vector<vector pVector0;
pVector0 is a vector of vector of 3D points.
|
More exactly, pVector0 is a vector of 0 vectors of 3D points.
| Quote: | pVector0[i] is a vector of 3D points.
|
Supposing it exists.
| Quote: | pVector0[i][j] is a 3D point.
|
Again, supposing it exists.
| Quote: | I wanted to add a point pt3D to the m vector in pVector0, I
have write these two instructions which belong to a C++
program.
vector< vector pVector0;
pVector0[m].push_back(pt3D);
|
Not sufficient. With just these two instructions, pVector0[m]
doesn't exist. To add a point, you'd have to do something like:
if ( pVector0.size() <= m ) {
pVector0.resize( m + 1 ) ;
}
pVector0[ m ].push_back( pt3D ) ;
| Quote: | I have as error message: Segmentation fault (core dumped)
|
Because you're accessing objects which don't exist.
If you know how many vectors of 3D points you need, you can
declare the vector:
vector< vector< Point3D > > pVector0( maxM ) ;
to begin with.
--
James Kanze GABI Software
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|