vector<vector<double> > & segmentation fault

U

utab

Dear all,

I tried sth like this, compiles but segmentation fault error. In my
reasoning field_values holds a vector<double> but when I tried, I
understood that it is not the case :).

#include <iostream>
#include <vector>
using namespace std;

int main(){

vector<vector<double> > field_values;
vector<vector<double> >::size_type SIZE=3;
vector<vector<double> >::size_type sz;
double d=1.3;
for(sz=0;sz!=SIZE;++sz)
field_values[sz].push_back(d);

cout << (field_values[0])[2];
return 0;

}

So any comments,

Regards and thx
 
I

Ian Collins

utab said:
Dear all,

I tried sth like this, compiles but segmentation fault error. In my
reasoning field_values holds a vector<double> but when I tried, I
understood that it is not the case :).

#include <iostream>
#include <vector>
using namespace std;

int main(){

vector<vector<double> > field_values;
vector<vector<double> >::size_type SIZE=3;
vector<vector<double> >::size_type sz;
double d=1.3;
for(sz=0;sz!=SIZE;++sz)
field_values[sz].push_back(d);

You should use resize or construct field_values with a known size,
otherwise there isn't a vector to push_back to.

Ian
 
U

utab

I> You should use resize or construct field_values with a known size,
otherwise there isn't a vector to push_back to.

Ian
Thx,

But still did not understand, could you please give an example why this
is not possible?
Regards,
 
I

Ian Collins

utab said:
I> You should use resize or construct field_values with a known size,


Thx,

But still did not understand, could you please give an example why this
is not possible?
Regards,
Consider

std::vector<Something> vec;

vec[0].doSomthing();

Which is pretty much what you have, with Something being a
vector<double> and doSomthing() being push_back().

Constructing a vector without a size does not initialise anything, it
might preallocate some memory, but that's it.

If you construct a vector with a size, size elements are default
constructed, giving you a vector of size default objects to use.

Same with resize (but not reserve).

You could have written:

for(sz=0;sz!=SIZE;++sz)
{
field_values.push_back(vector<double>());
field_values[sz].push_back(d);
}

Or

vector<vector<double> > field_values;
vector<vector<double> >::size_type SIZE=3;
vector<vector<double> >::size_type sz(SIZE);
 
U

utab

Is not this the same, vector<T> so T is a vector<double>. When writing
as vector<T> you do not have to give a size(maybe my example confused
you a bit, I do not know the size in advance that is why I tried to use
a nested structure, just as a try to see how it works or not)

for(sz=0;sz!=SIZE;++sz)
{
field_values.push_back(vector<double>()); You have to create a vector
to push, logical
field_values[sz].push_back(d); Still did not get why only this is
not possible?

}

Regards
 
I

Ian Collins

utab said:
Is not this the same, vector<T> so T is a vector<double>. When writing
as vector<T> you do not have to give a size(maybe my example confused
you a bit, I do not know the size in advance that is why I tried to use
a nested structure, just as a try to see how it works or not)

for(sz=0;sz!=SIZE;++sz)
{
field_values.push_back(vector<double>()); You have to create a vector
to push, logical
field_values[sz].push_back(d); Still did not get why only this is
not possible?
Because field_values[sz] isn't a vector, it's at best a block of
uninitialised memory, at worst, nothing. That's why I added the
push_back(vector<double>()).

Remember

vector<T> vec;

Does not initialise anything except initialise the internals of the
vector, while

vector<T> vec(10);

Default initialises a vector of 10 Ts.
 
U

utab

Thx for the explanations, my head is like a football now. I will go and
get a sleep and see the STL guide tomorrow for a further discussion.

Regards and thx
 
J

Jonathan Mcdougall

utab said:
Dear all,

I tried sth like this, compiles but segmentation fault error. In my
reasoning field_values holds a vector<double> but when I tried, I
understood that it is not the case :).

#include <iostream>
#include <vector>
using namespace std;
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

int main(){
vector<vector<double> > field_values;
vector<vector<double> >::size_type SIZE=3;
vector<vector<double> >::size_type sz;
double d=1.3;
for(sz=0;sz!=SIZE;++sz)
field_values[sz].push_back(d);


field_values is empty, why do you think you can access an element in
it? You must add a vector to field_values, and then add doubles to that
vector:

for(sz=0;sz!=SIZE;++sz)
{
vector<double> v;
v.push_back(d);

field_values.push_back(v);
}


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top