473,473 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

copy algorithm:segmentation fault error appears

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

using std::cout;
using std::vector;
using std::string;
using std::endl;
using std::copy;
using std::back_inserter;

int main(){

vector<string> str;
vector<string>::iterator iterb = str.begin(),iter=str.begin();
vector<string>::iterator itere = iterb+11;
vector<string>::size_type i = 0;
vector<string> dest;

while(iter!=itere){

*(iter)="string";

iter++;
}

copy(iterb,itere,back_inserter(dest));

while(i!=dest.size()){

cout << dest[i];
i++;
}

return 0;

}

Why?

Feb 18 '06 #1
1 2108
utab wrote:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>

using std::cout;
using std::vector;
using std::string;
using std::endl;
using std::copy;
using std::back_inserter;

int main(){

vector<string> str;
vector<string>::iterator iterb = str.begin(),iter=str.begin();
vector<string>::iterator itere = iterb+11;
vector<string>::size_type i = 0;
vector<string> dest;

while(iter!=itere){

*(iter)="string";

iter++;
}
OK, itere points to the 11th element of str, which is an empty vector.
It has no 11th element, nor a first.

Use a for loop counting between 0 and 11, and use use push_back within it.

After that, initialise itere with str.end().
copy(iterb,itere,back_inserter(dest));

while(i!=dest.size()){

cout << dest[i];
i++;
}
Instead you can use copy (as above), and an ostream_iterator.
return 0;

}

Why?


Assigning to elements in the vector that have not been allocated.

Techniques to fix that:
1) Don't declare variables until you can properly initialise them (see
itere, which should be initialised as str.end())
2) Don't create random offsets of iterators if you don't need to. (the 11)

Read up on push_back and ostream_iterator.

Post the code you end up with here again, and we'll have another look.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Patrick Dunnigan | last post by:
Hi, I am attempting a bulk copy from a c program into SQL Server 2000 using DBLib in freeTDS 0.63 RC11 (gcc 3.4.3, RH 9). I am getting an error message that I cannot find any documentation on. ...
2
by: juli jul | last post by:
Hello, I am trying to overwrite a file in this way: File.Copy(this.file_name,this.file_name,true); The problem is that I get this error: An unhandled exception of type 'System.IO.IOException'...
1
by: POnfri | last post by:
Hi, I have a problem in a peace of code were i'm doing a file copy using File.Copy. The Source is local and the target is a remote machine. Example: File.Copy(C:\temp\hi.txt,...
4
by: Bricklen | last post by:
Hi, I'm not sure if this is the correct group for this question, but I'll post it hoping that it is. I'm loading several ~15 million row files into a table using the COPY command. Apparently one...
5
by: Mark | last post by:
I get the error: System.IO.FileNotFoundException: Could not find file "C:\mydir\myfile.xls" Using the sub: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
0
by: Frank Rizzo | last post by:
Hello, I am getting the following error when I deploy my web services to IIS: Server Error in '/FUSIONSERVICES' Application Cannot create/shadow copy 'MyApp.DataLayer' when that file already...
5
by: Saabster | last post by:
Hi all, It's always the simple stuff that trips you up I guess. Here is the issue I'm dealing with. I have a folder with 1400 excel files that I need to move to another location. Since each...
0
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hello, mister I have an application web asp.net 2.0 + vs 2005 and VS 2005 Web Application Project. Its appears this error in execution: "Cannot Create/Shadow Copy '<projectname>' when that...
1
by: jshunter | last post by:
I've got a weird one here. I'm running a DTS package on SQL Server 2005. It copies a bunch of stored procedures. I renamed them on the originating server and ran the DTS again. The came over...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
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,...
1
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: 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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.