Segmentation Fault

I am trying to create an ADT String and when I compile my program it gets a Segmentation Fault while it tests my add function. Here is what I have. I am not sure why or where it is getting the segmentation fault.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Constructors

String::String(){

		s[0] = '\0';
}

String::String(const char rhs){
	
	s[0] = rhs;
	s[1] = 0;
}

String::String(const char rhs[]){

	int i=0;
	while ((rhs[i] != 0)&&(i<DEFAULT_STRING_CAPACITY)){
		s[i] = rhs[i];
		++i;
	}
	s[i]=0;
}


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
//Header File

#ifndef STRING_H
#define STRING_H
#include <iostream>
#include <cstdlib>
#include <cassert>

const int DEFAULT_STRING_CAPACITY = 256;

class String{
public:
	String ();               
	String (const char);                       
	String (const char[]);
	String operator+(const String&)const;
	bool operator == (const String&)const;
	bool operator < (const String&)const;
	bool operator > (const String&)const;
	bool operator <= (const String&)const;
	bool operator >=(const String&)const;
	bool operator !=(const String&)const;
	char operator[](const int)const;

	friend std::ostream& operator<< (std::ostream&, const String&);
	friend std::istream& operator>> (std::istream&, String&);

	int length()const;
	String substr(int, int)const;
	int findstr(const String&)const;
	int findchar(const char, const int)const;

private:
	char s[DEFAULT_STRING_CAPACITY];	
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//add function

String String::operator+(const String& rhs)const{

	String result;
	int i=0;
	while (s[i] != 0){
		result.s[i] = s[i];
		++i;
	}
	int offset = length();
	while (rhs.s[i] != 0){
		result.s[i+offset] = rhs.s[i];
		++i;
	}
	result.s[i+offset] = 0;
	return result;
}
are you sure you meant 0 and not '\0'?
Is there a difference? I thought that 0 could be used as a NULL character just like '\0'?
I suppose technically '\0' and 0 are the same character but it just looks odd.

Anyways why do you have lines 7-10 isn't that the same as result.s = s;

Your problem might be with the offset on line 13 or 16. You may be trying to access position 256 when it goes 0-255.
Oh yeah I guess you're right about lines 7-10. Beginners mistake.. So if I am going 1 position out of the array would I have it as result.s[i+offset-1]?
Last edited on
Or maybe in line 12 I could change it to while ((rhs.s[i] != 0)&&(i+offset < DEFAULT_STRING CAPACITY))
yeah you want to make sure it is less than the capacity otherwise you'd have to reallocate it.
Okay well I fixed my function but when I compile it with my makefile it still says theres a segmentation fault.. could it be something other than my operator+ function? I know this thread isn't about unix but here's the error message I get..

-bash-4.1$ make tests
./test_default_ctor
Done testing default constructor.
./test_c_str_ctor
Done testing constructors.
./test_add
make: *** [tests] Segmentation fault (core dumped)
What do you have in your main?

You could use that to see exactly what is wrong other than that I am not positive sorry.

A way to check if it is the operator+ would be something like
1
2
3
4
5
6
7
#include "String.h"

int main()
{
    String str1( "He" ) , str2( "llo" );
    str1 + str2;
}
In main I have this

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
#include "string.h"

int main(){
	{ 
		String s1, s2, result;
		assert(s1 =="\0");
		assert(s2 == "\0");
		assert(result == "\0");
	}

	{ 
		String s1, s2('a'), result;
		result = s1 + s2;
		assert(s1 == "\0");
		assert(s2 == "a");
		assert(result == "a");
	}

	{ 
		String s1 = 'a', s2 = 'b', result;
		result = s1 + s2;
		assert(s1 == "a");
		assert(s2 == "b");
		assert(result == "ab");
	}

	{ 
		String s1('a'), s2, result;
		result = s1 + s2;
		assert(s1 == "a");
		assert(s2 == "\0");
		assert(result == "a");
	}

	{ 
		String s1("Test."), s2("Test."), result;
		result = s1 + s2;
		assert(s1 == "Test.");
		assert(s2 == "Test.");
		assert(result == "Test.Test.");
	}

	{ 
		String s1("Test"), result;
		result = s1 + "Test";
		assert(s1 == "Test");
		assert(result == "TestTest");
	}

	{ 
		String s1("Test"), result;
		result = s1 + '.';
		assert(s1 == "Test");
		assert(result == "Test.");
	}

	std::cout<<"Done testing add."<<std::endl;
}


I have a .cpp file for every function I want to test and use assert to test them. So this should correctly check if the operator+ is working correctly but when I compile I get the segmentation fault.
Maybe your length function is off then
Wouldn't the length function just be?

1
2
3
4
5
6

int String::length()const{

	return length();
}
No you are just calling your function within itself. You're going to need something like

1
2
3
4
5
6
7
int count = 0;
for( int i = 0; i < MAX_CAPACITY; ++i )
{
    if( s[i] != '\0' ) ++count;
    else break;
}
return( count );
Wow I am stupid... Thank you so much for the help! That was my issue and everything compiles and runs now.
Topic archived. No new replies allowed.