473,382 Members | 1,380 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,382 software developers and data experts.

segmentation fault in stl vector.put_back

I get a segmentation fault error when I compile the following program.
The v2 vector contains ponters to float values in the array v.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>  
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. int main (int argc, char *argv[])
  7. { float a = 3, b = 2, c = 1;
  8.   float v[3];
  9.   v[0] = a;  v[1] = b;  v[2] = c;  
  10.   vector<float*> v2(3); 
  11.  // v2[0] = v+0;  v2[1] = v+1;   v2[2] = v+2;  // runs fine, but 
  12.   v2[0] = v+0;  v2[1] = v+1;   v2.push_back(v+2); // ****ERROR****  
  13.   cout << *(v2[0]) << "  " << *v2[1] << "  " << *v2[2] << endl;
  14. return 0;
  15. }
If I remove the comment slashes from the line above the errorous line the errorous line also runs fine, but it does not run as such. Odd? And
if I comment the push_back line the commented line runs fine.

Can't I use put_back in this way? I cannot see the mistake by myself, so I would apriciate very much any hints that would explain the error.

purso
Feb 19 '07 #1
2 2537
horace1
1,510 Expert 1GB
you create a vector with 3 elements and you set up [0] and [1] with the address of v+0 and v+1
Expand|Select|Wrap|Line Numbers
  1.   vector<float*> v2(3); 
  2.  // v2[0] = v+0;  v2[1] = v+1;   v2[2] = v+2;  // runs fine, but 
  3.   v2[0] = v+0;  
  4.   v2[1] = v+1;   
  5.  
you the call push back which ADDS an element of the end of v2 (it now has 4 elements), i.e. element v2[3] contains the address of v+2
Expand|Select|Wrap|Line Numbers
  1.   v2.push_back(v+2); // ****ERROR****  
  2.  
so when you try to print
Expand|Select|Wrap|Line Numbers
  1.   cout << *(v2[0]) << "  " << *v2[1] << "  " << *v2[2] << endl;
  2.  
v2[2] does no contain a valid address so you get a segmentation fault
try printing *v2[3] and it should work
Expand|Select|Wrap|Line Numbers
  1.   cout << *(v2[0]) << "  " << *v2[1] << "  " << *v2[3] << endl;
  2.  
Feb 19 '07 #2
Thank you, horace1.

Now, i understand what was the mistake. I assumed that push_back only adds items to a vector when it is needed. I thought that as i had only two members added into the vector, the third should go to the remaining empty slot. That was wrong.

The revised program runs fine.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>  
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6. int main (int argc, char *argv[])
  7. { float a = 3, b = 2, c = 1;
  8.   float v[3];
  9.   v[0] = a;  v[1] = b;  v[2] = c;  
  10.   vector<float*> v2(2);  // this is now (only) 2
  11.   v2[0] = v+0;  v2[1] = v+1;   v2.push_back(v+2); // runs fine!  
  12.   cout << *(v2[0]) << "  " << *v2[1] << "  " << *v2[2] << endl;
  13. return 0;
purso
Feb 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
3
by: mblome | last post by:
Hi everybody! I came across a very strange problem with stl vectors during developement of a mesh creation program. As the program is quite large I can only post small parts of it. Basically I...
1
by: sandwich_eater | last post by:
I get a segmentation fault in my program when calling a function "TestFn" that has been passed as a pointer into another function. The following excerpt should give enough information as to what I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
1
by: samuel.y.l.cheung | last post by:
Hi, I wrote a template to use copy() algorithm, called copyAll: template<class T> void copyAll(const T& src , T& dest ) { copy (src.begin(), src.end(), back_inserter(dest)); } but when I...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
1
by: jwlkr | last post by:
Hi, I am trying to sort a vector of a user defined type: a class which represents points in cartesian coordinates. The vector of points needs to be sorted according to the value of the...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
2
by: Steve | last post by:
I have segmentation fault when calling resize on an stl vector. I'm unsure if I'm doing something horribly wrong or if the stl is being dumb. The code breaks down like this: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.