473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

vector<vector<double> > & segmentation fault

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
7 12341
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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector...
2
by: franklini | last post by:
hello people i. can anybody help me, i dont know what is wrong with this class. it has something to do with the me trying to override the input output stream. if i dont override it, it works fine....
14
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
5
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.