By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

vector<vector<double> > & segmentation fault

P: n/a
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

May 27 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
utab wrote:
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);

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.
May 27 '06 #2

P: n/a

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,

May 27 '06 #3

P: n/a
utab wrote:
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,

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.
May 28 '06 #4

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


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

May 28 '06 #5

P: n/a
utab wrote:
Constructing a vector without a size does not initialise anything, it
might preallocate some memory, but that's it.

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.

--
Ian Collins.
May 28 '06 #6

P: n/a
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

May 28 '06 #7

P: n/a
utab wrote:
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;
http://www.parashift.com/c++-faq-lit....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

May 28 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.