Copy on Write program

Hi,
I am trying to make copy on write program, but I get a Segmentation fault after my first result (String kiri has : 1 pointer(s).)

It is mandatory for my program to have a class(StringHolder) that will keep a pointer to a string.
and I am supposed to create my StringHolder objects like this : a("kiri").
If there is a b("kiri"), it has to redirect the pointer to the first string in order to save memory.
I have done it a little differently than I should but I don't get the segmentation fault.

Thanks in advance!!!


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

#include <iostream>
#include <cstring>
#include <string>
#include <map>
//changes

//A class that keeps count of how many pointers point to each string
class U_Ptr
{


public:

U_Ptr() {}

/*U_Ptr(std::string* p) : ip(p)
{
	pointer_count[ip] = 1;
}*/

~U_Ptr()
{
//	pointer_count.clear();
}

std::string* AddString(std::string* a_Data)
{
	std::string* m_pStr;
	std::string m_Str;
	std::string m_Data;
	m_Data = *a_Data;
//	Map_Type::iterator iter = pointer_count.find(a_Data); //psakse me vash to periexomeno tou pointer kai oxi ton pointer (isws proswrinh metavlhth gia na krathsei to periexomeno)
	if (pointer_count.empty())
	{
		pointer_count.insert(std::pair<std::string*, int>(a_Data, 1));
		return a_Data;
	}
	else
	{

		for (Map_Type::iterator iter = pointer_count.begin(); iter != pointer_count.end(); ++iter)
		{
			m_Data = *a_Data;
			m_pStr = iter->first;
			m_Str = *m_pStr;
			if (m_Data.compare(m_Str) == 0)
			{
				a_Data = iter->first;
				++iter->second;
			}
			else
				pointer_count.insert(std::pair<std::string*, int>(a_Data, 1));
		}

	}

}

void DeleteString(std::string* a_Data)
{
	Map_Type::iterator iter = pointer_count.find(a_Data);
	if (iter->second == 1)
	{
		pointer_count.erase(iter);
	}
	else
	{
		--iter->second;
	}
}
void PrintString(std::string* a_Data)
{
	Map_Type::iterator iter = pointer_count.find(a_Data);
    std::cout << "String " << *iter->first << " has : " << iter->second << " pointer(s)." << std::endl;
}

private:
friend class StringHolder;
typedef std::map<std::string*, int> Map_Type;
Map_Type pointer_count; //A map that counts how many pointers to each std::string exist

};//class U_Ptr

//A class that keeps a pointer to a std::string object
class StringHolder
{
public:
	StringHolder(std::string* a_Data, U_Ptr& a_ptr)
	{
//		ptr = new U_Ptr(a_Data);//creates a pointer to a U_Ptr object
		m_Data = a_Data;
		m_ptr = &a_ptr;
		m_Data = (*m_ptr).AddString(m_Data);
	}

	void operator+=(std::string rhs)
	{
		std::string temp = (*m_Data);
	    temp.append(rhs);
		(*m_Data) = temp;
		(*m_ptr).AddString(m_Data);
//	    std::cout << *m_Data << std::endl;
	}

	~StringHolder()
	{
		(*m_ptr).DeleteString(m_Data);

	}
	void Print()
	{
		(*m_ptr).PrintString(m_Data);
	}

private:
	U_Ptr* m_ptr;
	std::string* m_Data;

};//class StringHolder


int main()
{
	U_Ptr ptr;

	std::string test = "kiri";
	std::string test2 = "Oleg";
	std::string test3 = "kiri";
	std::string* p_test = &test;
	std::string* p_test2 = &test2;
	std::string* p_test3 = &test3;
	StringHolder a(p_test, ptr);
	a.Print();
	StringHolder b(p_test, ptr);
	b.Print();
	b += "Luc";
	b.Print();
	StringHolder c(p_test2, ptr);
	c.Print();
	StringHolder d(p_test3, ptr);
	d.Print();
	return 0;
}
Please don't make duplicate threads.

http://www.cplusplus.com/forum/beginner/81321/
Yes i am sorry i will delete the second.
Topic archived. No new replies allowed.