473,545 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

copy algorithm:segme ntation 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_inser ter;

int main(){

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

while(iter!=ite re){

*(iter)="string ";

iter++;
}

copy(iterb,iter e,back_inserter (dest));

while(i!=dest.s ize()){

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

return 0;

}

Why?

Feb 18 '06 #1
1 2111
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_inser ter;

int main(){

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

while(iter!=ite re){

*(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,iter e,back_inserter (dest));

while(i!=dest.s ize()){

cout << dest[i];
i++;
}
Instead you can use copy (as above), and an ostream_iterato r.
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_iterato r.

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
6365
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. The server is sending back the following: "Received invalid row length 2 from bcp client. Minimum row size is 4." I know the row is longer 2...
2
2054
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' occurred in mscorlib.dll Additional information: The process cannot access the file "C:\Documents and Settings\My Documents\abc.xml" because it is...
1
3884
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, \\myremote\c$\temp\hi.txt, true); This API is run from a web service that exposes it. The problem is as follows: 1) I request a page (On the web service machine)...
4
1622
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 of the rows, about 6 million in, has an invalid entry. This is causing the COPY command to fail, so my question is this: Is there any way to skip...
5
1732
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 System.EventArgs) Handles Button1.Click Dim Path As String = "C:\mydir\" Dim File As String = "myfile.xls"
0
1474
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 exists. I can't figure out what this is and google mostly returns people asking the same question. Thanks.
5
3251
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 file has to go in to a particular target folder I decided that writing a program to move the files is they way to go. I've got all but this last...
0
2800
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 file arleady exists" error.
1
2395
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 with the old name and code! I deleted the DTS and built it from scratch, and the same thing happened. I ran SELECT * FROM sys.objects where type =...
0
7473
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7408
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7661
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7433
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5340
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3458
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1891
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1020
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.