I am getting a segmentation fault, why?

Why am I getting a segmentation when declaring two constructors with different parameters, but the same string of arguments? If I were to declare only one constructor the program will be delightful to compile and execute my results, however declaring more then one constructor a segmentation fault occurs. I deleted the pointer string variables in the destructor. I understand only one destructor is allowed per class with no arguments thus a void keyword inside as well.

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
#include <iostream>
#include <string>

class Names{
	std::string *firstName;
	std::string *middleName;
	std::string *lastName;
public:
    Names(std::string,std::string,std::string);
    Names(std::string,std::string);
    Names(std::string);
    ~Names();
    
    std::string returnFirst( void );
    std::string returnMiddle( void );
    std::string returnLast( void );
    
    void showFML( void );
    void showFL( void );
    void showF( void );
};

int main(){
	Names n("John","Yo","Doe");
	Names n2("Carrey","Coe");
	n.showFML();
	n2.showFL();
}

Names::Names(std::string fn0,std::string mn0,std::string ln0){
	firstName = new std::string;
	middleName = new std::string;
	lastName = new std::string;
	
	*firstName = fn0;
	*middleName = mn0;
	*lastName = ln0;
}

Names::Names(std::string fn1,std::string ln1){
    *firstName = fn1;
    *lastName = ln1;
}
    
Names::Names(std::string fn2){
	*firstName = fn2;
}

Names::~Names(){
	delete firstName;
	delete middleName;
	delete lastName;
}
    
std::string Names::returnFirst(){
	return *firstName;
}

std::string Names::returnMiddle(){
	return *middleName;
}
    
std::string Names::returnLast(){
	return *lastName;
}
    
void Names::showFML(){
	std::cout<<returnFirst()<<' '<<returnMiddle()<<' '<<returnLast();
}

void Names::showFL(){
	std::cout<<returnFirst()<<' '<<returnLast();
}

void Names::showF(){
	std::cout<<returnFirst();
}
You are overusing pointers and dynamic memory, and therefore asking for trouble. Which you get.

In C++ there is a rule named "Rule of three": if you write a custom destructor, then you also need to write a custom copy constructor and copy assignment operator=.

As it is now, you wrote "regular" constructors and the destructor, but the copy constructor and copy assignment operator= are generated automatically... and they do a shallow copy instead of a deep copy. This will bite you in a little bit.

The immediate problem is caused by your constructors. You forget to allocate memory in two of them. It's not enough that you do allocate memory in the first one.

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
Names::Names(std::string fn0,std::string mn0,std::string ln0){
	firstName = new std::string;
	middleName = new std::string;
	lastName = new std::string;
	
	*firstName = fn0;
	*middleName = mn0;
	*lastName = ln0;
}

Names::Names(std::string fn1,std::string ln1){
	firstName = new std::string;
	middleName = new std::string;
	lastName = new std::string;
	
    *firstName = fn1;
    *lastName = ln1;
}
    
Names::Names(std::string fn2){
	firstName = new std::string;
	middleName = new std::string;
	lastName = new std::string;
	
	*firstName = fn2;
}
Ha, now I see why! I will keep that Rule of 3 in mind, the whole time I was trying to do different memory allocations in the three constructors in a different way. Instead of allocating all three in each constructor I would just allocate 2 in the constructor that took two arguments and one allocation in the constructor that took 1 argument. I am doing c++ on my android phone so pardon my miss spellings, kinda rough just coding on tiny keyboard touch screen. TY catfish666
Last edited on
I will keep that Rule of 3 in mind

No. Don't just "keep it in mind". Apply it now.
Otherwise your program can explode whenever you copy Names objects.

http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// copy constructor (incomplete)
Names::Names(const Names &n)
{
    // allocate memory
    firstName = new std::string;

    // copy contents of n into this class
    *firstName = n.*firstName;
}

// copy assignment operator=
Names & Names::operator = (const Names &n)
{
    // don't allocate memory! this isn't a constructor

    // check for self-assignment
    if (this != &n)
    {
        // copy contents
    }

    return *this;
}


I am doing c++ on my android phone so pardon my miss spellings, kinda rough just coding on tiny keyboard touch screen.

Got a little S&M thing going on, have we?
Whats an S&M? it can get annoying programming on an android, I am going to have to buy a little keyboard for the phone. But as long as I can do c++ it will be worth it. While I am at work, in thw bathroom, waiting for someone, or before going to bed, I will acknowledge a lot.
I was poking fun at how you make things harder for yourself by using a smart phone.

You should consider using a regular computer for programming.

Nowadays, many people use a desktop PC with dual display: on one monitor they edit the code, and on the other they browse the documentation... while you do it all on a little phone... I hope the little keyboard will help you.
Oh I see, but I actually do all my programming on a computer, well actually on a laptop, but I only program on my mobile android if I am not near my laptop. Its cool because its more universal. If I cannot finish a project when developing an android app on my laptop because I have to go somewhere, I would just do it on my phone, etc. lol I can bring my laptop, but it is an expensive equipment (alienware), and where I am from you would definitely get robbed in a heart beat. Newark, NJ sucks.
> You are overusing pointers and dynamic memory, and therefore asking for trouble. Which you get.
1
2
3
4
5
6
7
8
9
10
11
12
13
class Names{
	std::string firstName; //note: no pointers
	std::string middleName;
	std::string lastName;
//...
};

Names::Names(std::string fn0,std::string mn0,std::string ln0):
   firstName(fn0), //initialization list
   middleName(mn0),
   lastName(ln0)
{}
//similar for the others constructors 
No need to code a destructor, a copy constructor, or an assignment operator.
Topic archived. No new replies allowed.