473,670 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector<vector<d ouble> > & 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<d ouble> > field_values;
vector<vector<d ouble> >::size_type SIZE=3;
vector<vector<d ouble> >::size_type sz;
double d=1.3;
for(sz=0;sz!=SI ZE;++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 12367
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<d ouble> > field_values;
vector<vector<d ouble> >::size_type SIZE=3;
vector<vector<d ouble> >::size_type sz;
double d=1.3;
for(sz=0;sz!=SI ZE;++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<Som ething> 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!=SI ZE;++sz)
{
field_values.pu sh_back(vector< double>());
field_values[sz].push_back(d);
}

Or

vector<vector<d ouble> > field_values;
vector<vector<d ouble> >::size_type SIZE=3;
vector<vector<d ouble> >::size_type sz(SIZE);

--
Ian Collins.
May 28 '06 #4
Constructin g 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!=SI ZE;++sz)
{
field_values.pu sh_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:
Constructi ng 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!=SI ZE;++sz)
{
field_values.pu sh_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(vecto r<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<d ouble> > field_values;
vector<vector<d ouble> >::size_type SIZE=3;
vector<vector<d ouble> >::size_type sz;
double d=1.3;
for(sz=0;sz!=SI ZE;++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!=SI ZE;++sz)
{
vector<double> v;
v.push_back(d);

field_values.pu sh_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
2260
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 foo containing vectors of the size 3. I then want to compare a vector<double> of size 3 (coord) with foo to find a sequence of elements in foo that equals coord. However, when I try this it returns when just one of the element of coord equals one...
2
2457
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. i would forget overriding it but i have to do it because its a coursework. here is a simple version of the class #include <iostream> #include <string> #include <vector>
14
2659
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
4009
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 large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the vector<vector<double>dynamically, and that will kill my execution time. So, I want to reserve space first, using, of...
5
6046
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 use it, but std::sort doesn't seem to find it. I have copied a very simple example below. Everything compiles just fine when the line with std::sort function is commented out, but with that line included a whole slew of errors are given like: ...
0
8468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8814
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8660
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7415
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4209
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2799
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.