segmentation fault

C

Chris

I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
It compiles fine, then when I run it it simply prints "segmentation
fault".

int main() {
vector<vector<string> > yo;
yo[0][0] = "hello";
cout << yo[0][0];

return 0; }

1. What's causing this?

I'm using the object "vector<vector<string> >" so I can split a string
into lines, then split the lines into words so that
"hello there\nTommy Boy"
becomes
[ ["hello", "there"], ["Tommy", "Boy"] ]

2. Should I do this a different way?

Thanks for any help!
 
I

Ian Collins

Chris said:
I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
It compiles fine, then when I run it it simply prints "segmentation
fault".

int main() {
vector<vector<string> > yo;
yo[0][0] = "hello";
cout << yo[0][0];

return 0; }

1. What's causing this?
Surely this must be an FAQ by now, if not, look back though the past
week or so of postings. You can't use operator [] on an empty vector.
 
J

Jim Langston

Chris said:
I'm compiling the following code with GCC 4.1.0 (on fedora 6 I think).
It compiles fine, then when I run it it simply prints "segmentation
fault".

int main() {
vector<vector<string> > yo;
yo[0][0] = "hello";
cout << yo[0][0];

return 0; }

1. What's causing this?

I'm using the object "vector<vector<string> >" so I can split a string
into lines, then split the lines into words so that
"hello there\nTommy Boy"
becomes
[ ["hello", "there"], ["Tommy", "Boy"] ]

2. Should I do this a different way?

Thanks for any help!

Your yo vector is empty. You need to insert into it before you can use it.

std::vector mama;
mama.push_back( "hello" );
yo.push_back( mama );
 

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

Members online

Forum statistics

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

Latest Threads

Top