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

segmentation fault in stl vector.put_back

P: 4
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
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
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

P: 4
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

Post your reply

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