473,418 Members | 2,049 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,418 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 2540
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: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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...
0
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...

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.