If anyone can tell me why I\'m getting a bus error in my set method and help me
ID: 3642002 • Letter: I
Question
If anyone can tell me why I'm getting a bus error in my set method and help me fix my set, get, and copy methods I will rate Lifesaver! Included are 3 classes, MyString.h MyString.cpp and main.cpp.MyString.h:
//
// MyString.h
//
//
// CSCE 240 Assignment 6
//
#ifndef MyString_h
#define MyString_h
//class name
class MyString {
//public access
public:
//constructors
MyString();
MyString(char *word);
//destructors
~MyString();
//public methods
void Print();
int Length();
void Set(int index, char c);
char Get(int index);
void Copy(MyString &s);
private:
char *string;
};
#endif
MyString.cpp:
//
// MyString.cpp
//
//
//
// CSCE 240 Valafar
// Assignment 6
//
//libraries
#include <iostream>
#include "MyString.h"
using namespace std;
//default constructor
MyString::MyString(){
string = "Hello World";
}
//constructor that inititalizes the variable string to the parameter word
MyString::MyString(char *word){
string = word;
}
//deconstructor
MyString::~MyString(){
delete [] string;
}
//print method that is used to run through the char array and print out each individual character
void MyString::Print(){
cout << string << endl;
}
//length method that is used to determine the length of the char array
//returns the (int) length of the array
int MyString::Length(){
int Counter (0);
while (string[Counter] != '')
{
Counter++;
}
return (Counter);
}
//method used to set a specific character at a given index.
//parameters: the index in which to set, the actual character to be set
//uses boundary checks to insure index is within bounds
void MyString::Set(int index, char c){
if(index>=0 && index < Length()){
*(string+index)=c;
}
}
//method used in order to get a certain value at a given index, if the index is not within bounds the program exits and returns 1.
//parameters: the index in which to get the value
char MyString::Get(int index){
if(index>=0 && index < Length()){
return *(string+index);
}
else exit(1);
}
//Copy method that takes in an instance of MyString class and copys it to another instance of MyString
//parameter: the MyString instance that is being copied
void MyString::Copy(MyString &s){
}
main.cpp:
/*
* File: main.cpp
*
* Created on October 21, 2011, 8:56 PM
*/
#include <cstdlib>
#include <iostream>
#include "MyString.h"
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
MyString String1; //Test of default constructor. Use "Hello world"
MyString String2("This is a test"); //Test of alternate constructor
MyString String3("Something else");
cout << "*************Test of values*************" << endl;
String1.Print();
String2.Print();
String3.Print();
cout << "*************Test of Length*************" << endl;
cout << String1.Length() << " ";
cout << String2.Length() << " ";
cout << String3.Length() << endl;
cout << "*************Test of Set*************" << endl;
String1.Set(0, 'J');
String1.Print();
cout << "*************Test of Copy*************" << endl;
String1.Print();
cout << endl;
String3.Print();
cout << endl;
String3.Copy(String1); //String1 should be copied into String3
String1.Print();
cout << endl;
String3.Print();
cout << endl;
cout << "*************Test of Get*************" << endl;
for(int i=0; i < String1.Length()+1; i++) //The last character should exit the program
{
cout << String1.Get(i) << " ";
if(i==String1.Length()-1){
cout << endl;
}
}
return 0;
}
Explanation / Answer
You were never using "new" to allocate memory to hold the characters in the string, so when you attempted to modify the contents of the string with set, bad things happened.
I updated your MyString.h (to use pstring instead of string since it holds a pointer) and added the dynamic memory allocation to the functions in MyString.cpp.
The code now runs and executes your main.cpp without errors. I commented (most) of the changes in MyString.cpp.
Hope this helps!
MyString.h
//
// MyString.h
//
//
// CSCE 240 Assignment 6
//
#ifndef MyString_h
#define MyString_h
//class name
class MyString {
//public access
public:
//constructors
MyString();
MyString(char *word);
//destructors
~MyString();
//public methods
void Print();
int Length();
void Set(int index, char c);
char Get(int index);
void Copy(MyString &s);
private:
char *pstring;
};
#endif
MyString.cpp
//
// MyString.cpp
//
//
//
// CSCE 240 Valafar
// Assignment 6
//
//libraries
#include <iostream>
#include "MyString.h"
using namespace std;
//default constructor
MyString::MyString(){
pstring = new char[11]; // need to use new to allocate memory to hold the string
pstring[0]='H'; // there's probably an easier way to do this...
pstring[1]='e';
pstring[2]='l';
pstring[3]='l';
pstring[4]='o';
pstring[5]=' ';
pstring[6]='W';
pstring[7]='o';
pstring[8]='r';
pstring[9]='l';
pstring[10]='d';
pstring[11]='';
}
//constructor that inititalizes the variable string to the parameter word
MyString::MyString(char *word){
int Counter=0;
while (word[Counter] != '') // find size of word
{
Counter++;
}
Counter++; // increment Counter for the ending null character
pstring = new char[Counter]; // need to use new to allocate memory to hold the string
for (int i=0;i<Counter;i++)
{
pstring[i]=word[i]; // copy each char of word to pstring
}
}
//deconstructor
MyString::~MyString(){
delete [] pstring;
}
//print method that is used to run through the char array and print out each individual character
void MyString::Print(){
int Counter = 0;
while (pstring[Counter] != '')
{
cout << pstring[Counter++]; // should be doing output char by char
}
cout << endl;
}
//length method that is used to determine the length of the char array
//returns the (int) length of the array
int MyString::Length(){
int Counter (0);
while (pstring[Counter] != '')
{
Counter++;
}
return (Counter);
}
//method used to set a specific character at a given index.
//parameters: the index in which to set, the actual character to be set
//uses boundary checks to insure index is within bounds
void MyString::Set(int index, char c){
if(index>=0 && index < Length()){
*(pstring+index)=c;
}
}
//method used in order to get a certain value at a given index, if the index is not within bounds the program exits and returns 1.
//parameters: the index in which to get the value
char MyString::Get(int index){
if(index>=0 && index < Length()){
return *(pstring+index);
}
else exit(1);
}
//Copy method that takes in an instance of MyString class and copys it to another instance of MyString
//parameter: the MyString instance that is being copied
void MyString::Copy(MyString &s){
delete [] pstring; // delete current string
pstring = new char[s.Length()+1]; // allocate memory
for (int i=0; i<s.Length(); i++){
pstring[i] = s.Get(i); // copy each char from s to 'this'
}
pstring[s.Length()]='';
}