MyString Class Segmentation Fault

I am doing a program for creating a simple string class using c-strings and dynamically allocated memory. Somewhere along the line I have done something wrong and I am getting a segementation default when I run my driver program with my cpp file.
Here is my code:
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
#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 

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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//begin of .cpp file
#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);
	str[size]='\0';
}               

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[size] = '\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 += 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;
	return os;
}

istream& operator>> (istream& is, MyString& s)
{
	int maxsize = 0;
	int index = 0;
	char* buffer = new char[maxsize];	     
	
	while(isspace(is.peek()))	
		is.ignore();

	while((!isspace(is.peek())) && (!is.eof())) 
	{
		char* tempstring = new char[maxsize + 1];
		is >> buffer[index];

		for(int i = 0; i < maxsize; i++)
			tempstring[i] = buffer[i];

		index++;
		maxsize++;
		delete [] buffer;
		buffer = tempstring;
	}
	
	buffer[index] = '\0';
	delete [] s.str;	
	s.str = buffer;
	return is;
}

istream& getline (istream& is, MyString& s, char delim)
{
	int index = 0;
	int buffersize = 10;
	char* buffer = new char[buffersize];  

	do
	{
		if(!is.eof())
		{
			if(index < buffersize - 1)
			{	
				buffer[index] = is.get();
				index++;
			}
			else
			{
				buffersize++;
				char* temp = new char[buffersize];

				for(int i = 0; i < index; i++)
					temp[i] = buffer[i];
				
				temp[index] = is.get();
				index++;
				delete [] buffer;
				buffer = temp;
			}
		}			
	}while((is.peek() != delim) && (!is.eof()));	
	is.ignore();					
	
	buffersize = index + 1;
	buffer[index] = '\0';
	delete [] s.str;			
	s.str= buffer;
	s.size= buffersize;
	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 - 1)	
	{
		char* tempstring = new char[size + s.size - 1];
		
		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;
}
Last edited on
Well, one mistake seems to be in the copy constructor.

Instead of strlen(second.str + 1) I think you want strlen(second.str) + 1.

You've also forgot to update the size.
Last edited on
Topic archived. No new replies allowed.