Simple String Class Segmentation Fault

I am doing a simple string class program using dynamic allocation and c-strings.
I can get everything to compile, but I get a segmentation fault.
the program starts
s1 = asdf
segemtation Fault
When I try to debug it shows:
Program received signal SIGSEGV, Segmentation fault.
__strcat_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296
296 movdqu (%rsi), %xmm1
(gdb) where
#0 __strcat_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:296
#1 0x00000000004011d6 in MyString::operator+=(MyString const&) ()
#2 0x00000000004012f3 in operator>>(std::istream&, MyString&) ()
#3 0x0000000000401f2a in main ()
Header File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#ifndef MYSTRING_H
#define MYSTRING_H
#include <iostream>
using namespace std;

class MyString
{
  friend ostream& operator<< (ostream& , const MyString& );
  friend istream& operator>> (istream& , MyString& );
  friend istream& getline (istream& , MyString& , char delim = '\n');

  friend MyString operator+ (const MyString& , const MyString& );

  friend bool operator< (const MyString& , const MyString& );
  friend bool operator> (const MyString& , const MyString& );
  friend bool operator<=(const MyString& , const MyString& );
  friend bool operator>=(const MyString& , const MyString& );
  friend bool operator==(const MyString& , const MyString& );
  friend bool operator!=(const MyString& , const MyString& );

public:
  MyString();				// empty string
  MyString(const char* );		// conversion from c-string
  MyString(int );			// conversion from int
  ~MyString();				// destructor
  MyString(const MyString& );		// copy constructor
  MyString& operator=(const MyString& );   // assignment operator

  MyString& operator+=(const MyString& );  // concatenation/assignment

  // bracket operators to access char positions
  char& operator[] (unsigned int index);
  const char& operator[] (unsigned int index) const;

  // insert s into the string at position "index"
  MyString& insert(unsigned int index, const MyString& s);

  // find index of the first occurrence of s inside the string
  //  return the index, or -1 if not found
  int indexOf(const MyString& s) const;
  
  int getLength() const;		// return string length
  const char* getCString() const;	// return c-string equiv

  MyString substring(unsigned int , unsigned int ) const;
  MyString substring(unsigned int ) const;

private:

char * str;
int size;


};
#endif


CPP File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <iostream>
#include <cstring>
#include <cctype>
#include <iomanip>
#include "mystring.h"

MyString::MyString()
{
	size = 0;
	str = new char[size+1];
	str = NULL;
}                           

MyString::MyString(const char* s)
{
	
	size = strlen(s);
	str = new char[size+1];
	strcpy(str,s);
}               

MyString::MyString(int convert)
{
	int con = convert;
	int counter = 0;

	do	
	{
		con = con / 10;
		counter++;
	}while(con != 0);
	
	size = counter + 1;
	str = new char[size];
 	
	do	
	{
		str[counter - 1] = char((convert % 10) + 48);
		convert = convert / 10;
		counter--;
	}while(counter >= 1);
	str= '\0';
}                       

MyString::~MyString()
{
	delete [] str;
}                          

MyString::MyString(const MyString& second)
{
	
	size = second.size;
	str = new char[size+1];
	strcpy(str,second.str);

}

MyString& MyString::operator=(const MyString& second )
{
        
	if (this != &second)
        {
                delete []str;
		size = strlen(second.str);
                str = new char[size+1];
                strcpy(str,second.str);
		str[size]='\0';
        }
	
                return *this;
}

MyString& MyString::operator+=(const MyString& first)
{
   	size = size + first.size;
   	char  *temp = str;
   	str = new char[size+1];
   	strcpy(str,temp);
   	strcat(str,first.str);
   	delete  [] temp;
   	return  *this;
}

ostream& operator<< (ostream& os, const MyString& s)
{
	  
	os <<s.str;
	return os;
}

istream& operator>> (istream& is, MyString& s)
{

    	char c;

	while (is && isspace(is.peek()))
		is.ignore();

	s = ""; // Set the target to the empty string.
	while (is && !isspace(is.peek()))
	{
		is >> c;
		s += c;
	}

    return is;
}


istream& getline (istream& is, MyString& s, char delim)
{
	MyString String;
	char ch;
	is.get(ch);
	while (ch!= delim && !is.eof()) //check for end of file
	{
		String+=ch;
		is.get(ch);
	}
	s=String;
	return is;		
}

MyString operator+ (const MyString& first, const MyString& second)
{
	MyString s = first;
        s += second;
   	return  s; 
}

char& MyString::operator[] (unsigned int index)
{
	//assert aborts program with error message
	if(index>=0 && index < strlen(str))
	{
		cout << "Not Valid";
		
	}
	return str[index];
}

const char& MyString::operator[] (unsigned int index) const
{
	if(index>=0 && index < strlen(str))
	{
		cout << "Not Valid";
		
	}
	return str[index];
}

MyString& MyString::insert(unsigned int index, const MyString& s)
{
	if(index <= size)	
	{
		char* tempstring = new char[size + s.size];
		
		for(int i = 0; i < index; i++)
			tempstring[i] = str[i];

		for(int i = 0; i < s.size - 1; i++)
			tempstring[i + index] = s.str[i];

		for(int i = index + s.size - 1; i < size + s.size -2; i++)
			tempstring[i] = str[i - s.size +1];

		tempstring[size + s.size -2] = '\0';
		delete [] str;
		str = tempstring;
		size = size + s.size - 1;
	}
	else				
	{
		char* tempstring = new char[size + s.size - 1];

		for(int i = 0; i < size - 1; i++)
			tempstring[i] = str[i];

		for(int i = 0; i < s.size - 1; i++)
			tempstring[size - 1 + i] = s.str[i];

		tempstring[size + s.size - 2] = '\0';
		delete [] str;
		str = tempstring;
		size = size + s.size - 1;
	}
		return *this;  
}


int MyString::indexOf(const MyString& s) const
{
	if(size < s.size)	
		return -1;
	else if(size == s.size)	
	{
		int i = 0;

		while((str[i] == s.str[i]) && (str[i] != '\0'))
			i++;

		if(str[i] == '\0')
			return 0;
		else
			return -1;
	}
	else					
	{
		int index = 0;

		while(index < (size - s.size + 1))
		{	
			int j = 0;
			int k = 0;
			while((s[0] != str[index]) && (index < (size - s.size + 1)))
				index++;
		
			if(index == size - s.size + 1)
				return -1;
			else
			{
				k = index;
				while((s[j] == str[k]) && (s[j] != '\0'))
				{
					k++;
					j++;
				}

				if(s[j] == '\0')
					return index;
				else
					index++;
			}
		}
	}	
}

int MyString::getLength() const
{
	return size;
}

const char* MyString::getCString() const
{
	return str;
}

MyString MyString::substring(unsigned int index, unsigned int sz ) const
{
	MyString sub = substring(index);
	
	if(sub.size - 1 <= sz)
	{
		return sub;
	}
	else
	{
		char* tempstring = new char[sz + 1];
		
		for(int i = 0; i < sz; i++)
		{
			tempstring[i] = sub.str[i];
		}
		
		tempstring[sz] = '\0';
		delete [] sub.str;
		sub.str = tempstring;
		sub.size = sz + 1;
		return sub;
	}
}

MyString MyString::substring(unsigned int index) const
{
	MyString sub;
	int tempsize = size - index;
	
	if(tempsize <= 1)
	{
		sub.str = '\0';
		sub.size = 1;
	}
	else
	{
		char* tempstring = new char[tempsize];
		
		for(int i = 0; i < tempsize - 1; i++)
		{
			tempstring[i] = str[i + index];
		}
		
		tempstring[tempsize - 1] = '\0';	
		delete [] sub.str;
		sub.str = tempstring;
		sub.size = tempsize;
	}
	return sub;
}


bool operator< (const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)<0;
}

bool operator> (const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)>0;
}

bool operator<=(const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)<=0;
}

bool operator>=(const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)>=0;
}

bool operator==(const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)==0;
}

bool operator!=(const MyString& first, const MyString& second)
{
	return strcmp(first.str,second.str)!=0;
}


My driver Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
using namespace std;

#include "mystring.h"

int main()
{
   MyString s1;
   MyString s2("Hello, World");
   MyString s3 = "Welcome to Florida, have a nice day";
   MyString s4 = 12345;

   cout << "s1 = " << s1 << '\n';
   cout << "s2 = " << s2 << '\n';
   cout << "s3 = " << s3 << '\n';
   cout << "s4 = " << s4 << '\n';
   cout << '\n';

   cout << "Making the calls:\n"
        << "\tcin >> s1\n"
        << "\tgetline(cin, s2, ',')\n"
        << "\tgetline(cin, s3)\n";
   cout << "Enter some sentences: ";
  
   cin >> s1;
   getline(cin,s2,',');
   getline(cin,s3);

   cout << "\nNew string values:\n";

   cout << "s1 = " << s1 << '\n';
   cout << "s2 = " << s2 << '\n';
   cout << "s3 = " << s3 << '\n';
   cout << "---------------------------\n";

// ----------------------------------

   s1 = "Dog";
   s2 = "Food";
   MyString result = s1 + s2;
   cout << "result = " << result << '\n';

   s1 += s2;
   cout << "s1 = " << s1 << endl;

   const MyString s5 = "The concatenation of the catapult is a catamaran";
   cout << "s5 = " << s5 << endl;
   cout << "s5.indexOf(\"cat\") returns " << s5.indexOf("cat") << '\n';
   cout << "s5.indexOf(\"dog\") returns " << s5.indexOf("dog") << '\n';

   cout << "s5.getLength() = " << s5.getLength() << '\n';

   cout << "s5[4] = " << s5[4] << '\n';
   cout << "s5[10] = " << s5[10] << '\n';
   cout << "s5[15] = " << s5[15] << '\n';
   cout << "s5[52] = ascii " << static_cast<int>(s5[52]) << '\n';

   cout << "s5.substring(10,16) = " << s5.substring(10,16) << '\n';
   cout << "s5.substring(23) = " << s5.substring(23) << '\n';

   cout << "-----------------------------\n";

   MyString words = "Greetings, Earthling";

   cout << "words = " << words << '\n';
   cout << "words.getLength() = " << words.getLength() << '\n';
   words[0] = 'K';
   words[4] = 'p';
   words[16] = 'z';

   cout << "words = " << words << '\n';

   words[25] = 'Q';
   cout << "words = " << words << '\n';

   words.insert(11, "Insane ");
   cout << "words = " << words << '\n';

   cout << "-----------------------------\n";

   MyString x = "apple", y = "apply";
   cout << "x = " << x << '\n';
   cout << "y = " << y << '\n';

   if (x < y)		cout << "x < y is true\n";
   if (x > y)		cout << "x > y is true\n";
   if (x <= y)		cout << "x <= y is true\n";
   if (x >= y)		cout << "x >= y is true\n";
   if (x == y)		cout << "x == y is true\n";
   if (x != y)		cout << "x != y is true\n";



}
Topic archived. No new replies allowed.