Connecting Tech Pros Worldwide Forums | Help | Site Map

vector<vector<double> > & segmentation fault

utab
Guest
 
Posts: n/a
#1: May 28 '06
Dear all,

I tried sth like this, compiles but segmentation fault error. In my
reasoning field_values[i] 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


Ian Collins
Guest
 
Posts: n/a
#2: May 28 '06

re: vector<vector<double> > & segmentation fault


utab wrote:[color=blue]
> Dear all,
>
> I tried sth like this, compiles but segmentation fault error. In my
> reasoning field_values[i] 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);
>[/color]
You should use resize or construct field_values with a known size,
otherwise there isn't a vector to push_back to.

Ian

--
Ian Collins.
utab
Guest
 
Posts: n/a
#3: May 28 '06

re: vector<vector<double> > & segmentation fault



I> You should use resize or construct field_values with a known size,[color=blue]
> otherwise there isn't a vector to push_back to.
>
> Ian
>[/color]
Thx,

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

Ian Collins
Guest
 
Posts: n/a
#4: May 28 '06

re: vector<vector<double> > & segmentation fault


utab wrote:[color=blue]
> I> You should use resize or construct field_values with a known size,
>[color=green]
>>otherwise there isn't a vector to push_back to.
>>
>>Ian
>>[/color]
>
> Thx,
>
> But still did not understand, could you please give an example why this
> is not possible?
> Regards,
>[/color]
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);

--
Ian Collins.
utab
Guest
 
Posts: n/a
#5: May 28 '06

re: vector<vector<double> > & segmentation fault


[color=blue][color=green]
>>Constructing a vector without a size does not initialise anything, it
>>might preallocate some memory, but that's it.[/color][/color]

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

Ian Collins
Guest
 
Posts: n/a
#6: May 28 '06

re: vector<vector<double> > & segmentation fault


utab wrote:[color=blue][color=green][color=darkred]
>>>Constructing a vector without a size does not initialise anything, it
>>>might preallocate some memory, but that's it.[/color][/color]
>
>
> 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?
>[/color]
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.

--
Ian Collins.
utab
Guest
 
Posts: n/a
#7: May 28 '06

re: vector<vector<double> > & segmentation fault


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

Jonathan Mcdougall
Guest
 
Posts: n/a
#8: May 28 '06

re: vector<vector<double> > & segmentation fault


utab wrote:[color=blue]
> Dear all,
>
> I tried sth like this, compiles but segmentation fault error. In my
> reasoning field_values[i] holds a vector<double> but when I tried, I
> understood that it is not the case :-).
>
> #include <iostream>
> #include <vector>
> using namespace std;[/color]

http://www.parashift.com/c++-faq-lit....html#faq-27.5
[color=blue]
> 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);[/color]

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

Closed Thread