Segmentation Fault. Need help

$ g++ Text.cpp Menu.cpp -o menu
$ ./menu
segmentation fault (core dumped)
I have no idea where I am trying to free pointer that has already been freed.

Text.h
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
#ifndef TEXT_H
#define TEXT_H
#include<iostream>



class Text{

	private:

		char* text;

	public:

		~Text();							// destructor

		Text();								// default constructor

		Text(const Text &);					// copy constructor /constructor overloading

		Text(const char*);					// conversion constructor / constructor overloading

		void set(const char*);				// setter

		void set(const Text &);				// overloading setter

		Text & operator= (const Text &);	// overloading = operator

		void append (const char*);			// appending to Text_store of an object

		void append (const Text &);			// overloading appending to add Text_store of object passed as parameter to the calling object

		int length() const;					// These are read only functions i.e. they can only read the private members and not modify them 

		bool isEmpty() const;				// If a function modifies private members it CANT BE CONST

		friend std::ostream& operator<<(std::ostream & , const Text &); //friend function for operator overloading		

};

#endif 


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

#include <iostream>

#include<cstring>

using namespace std;





// destructor

Text::~Text(){

	delete[] this->text;

}



// Default Constructor

Text:: Text(){

	this->text = new char;  				//c++ cant print null pointer

}



// copy constructor /constructor overloading

Text::Text(const Text &Text){

	this->text = new char[strlen(Text.text) + 1];		// obviously points to nothing so allocate dynamic memory allocation with same size as that of passed arg

	strcpy(this->text,Text.text);				// copy the text

}



// conversion constructor / constructor overloading

Text::Text(const char* text){

	this->text = new char[strlen(text) + 1];		// obviously points to nothing so allocate dynamic memory and copy

	strcpy(this->text,text);

}



// setter

void Text::set(const char* text){

	if (this->text == NULL){				// if NULL then allocate dynamic memory and copy

		this->text = new char[strlen(text) + 1];

		strcpy(this->text,text);

	}else{							// if not NULL then deallocate, allocate dynamic memory and copy

		delete[] this->text;

		this->text = new char[strlen(text) + 1];

		strcpy(this->text,text);

	}

}



// overloading setter

void Text::set(const Text &Text){

	delete[] this->text;					// deallocate, allocate, copy

	this->text = new char[strlen(Text.text) + 1];

	strcpy(this->text,Text.text);

}



// overloading = operator

Text & Text::operator=(const Text &Text){			// Note the Precedence of this operator is from RIGHT to LEFT

	set(Text);						// could make this faster by checking if both have the same string

	return *this; 						//A question....what if i return only this. thats not possible coz of const keyword.

								// Why cant we use &this as return. If an object calls it also passes itself as an argument(obviously by 									//reference). But then *this would be the calling object not its reference.

								// But when we return it is converted back to reference coz of Text & at return type.

}



// appending to Text_store of an object

void Text::append (const char* text){

	// Store this->text in temp and deallocate this->text

	char *temp = new char[strlen(this->text)];

	strcpy(temp,this->text);

	delete[] this->text;



	// Allocate memory for new string

	int len = strlen(temp) + strlen(text) + 1;

	this->text = new char[len];



	// Copy temp in this->text. Then concatinate text to this->text

	strcpy(this->text,temp);

	strcat(this->text,text);



	// deallocate temp

	delete [] temp;

}



// overloading appending to add Text_store of object passed as parameter to the calling object

void Text::append(const Text &Text){

	// Store this->text in temp and deallocate this->text

	char *temp = new char[strlen(this->text)];

	strcpy(temp,this->text);

	delete[] this->text;



	// Allocate memory for new string

	int len = strlen(temp) + strlen(Text.text) + 1;

	this->text = new char[len];



	// Copy temp in this->text. Then concatinate text to this->text

	strcpy(this->text,temp);

	strcat(this->text,Text.text);



	// deallocate temp

	delete [] temp;



}



// These are read only functions i.e. they can only read the private members and not modify them

int Text::length() const{

	return strlen(this->text);

}



// These are read only functions i.e. they can only read the private members and not modify them

bool Text::isEmpty() const{

	return (this->length()==0 ? true:false);

}



std::ostream& operator<<(std::ostream& os, const Text &Text){ 		//friend function for operator overloading

	if(Text.text==NULL){						//had to do this because c++ cant print null pointer

		os << "";

	}else{

		os << Text.text;

		return os;

	}

}

Menu.h
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
#ifndef MENU_H

#define MENU_H

#include "Text.h"

#include <iostream>





class Menu{

	private:

		Text* option_list;							// This is a stack!! Note there are push_back and pop_back functions. So it must be a stack

		int count;

		int max_list_size;

		Text top_prompt;

		Text bottom_prompt;

		Text top_text;

		Text bottom_text;					

	public:

		Menu();										// Default Constructor

		Menu(const Menu &);							// Copy Constructor

		~Menu();

		void double_capacity();						// Double max_list_size

		void push_back(char *);						// Create text obj assign char * and PUSH it to the STACK option_list

													// check OVERFLOW

		void push_back(const Text &);				// PUSH text obj to the STACK option_list

													// check OVERFLOW

		void insert(int, Text &);					// Insert at position by shifting

		Text remove(int);							// removes option from STACK option_list at index i

		const int size();							// Returns size of STACK option_list NOTE THIS IS NOT max_list_size

		const int capacity();						// Returns value of max_list_size

		void pop_back();							// POP from STACK max_list_size

													// Do check for UNDERFLOW

		Text get(int); 								// Return k th From stack option_list

		const Text toString();						// Read all in STACK option_list and concatinate them in char*, store in Text object and return it

		int read_option_number();					// Calls to string internally then reads and returns a vaiid option number

		

		//Operator Overloading

		Menu & operator= (const Menu &);			// overloading = operator

		friend std::ostream& operator<<(std::ostream & , Menu &); //friend function for operator overloading

		

		//I think these are explained in a wrong way in the assignment

		void set_top_prompt(const Text &);			

		void set_bottom_prompt(const Text &);

		void set_top_message(const Text &);

		void set_bottom_message(const Text &);

		

		void clear_top_prompt();					

		void clear_bottom_prompt();					

		void clear_top_message();					

		void clear_bottom_message();				

		const bool isEmpty();                       // Returns true if STACK is empty()

};



#endif  


Menu.cpp
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#include "Text.h" 
#include "Menu.h"
#include <iostream>
#include <cstring>
#include<stdexcept>
#include<cstdlib>
using namespace std;
Menu::Menu(){

	int count = 0;

	int max_list_size = 1;

	Text* option_list = new Text[max_list_size]; // This is a stack!! Note there are push_back and pop_back functions. So it must be a stack

	Text top_prompt = "";

	Text bottom_prompt = "";

	Text top_text = "";

	Text bottom_text = "";

}							

Menu::Menu(const Menu &otherMenu){

	this->count = otherMenu.count;

	this->max_list_size = otherMenu.max_list_size;

	this->option_list = new Text[otherMenu.count];

	for (int a=0; a<=count - 1; a++){

		this->option_list[a] = otherMenu.option_list[a];	

	}

	
	this->top_prompt = otherMenu.top_prompt;

	this->bottom_prompt = otherMenu.bottom_prompt;

	this->top_text = otherMenu.top_text;

	this->bottom_text = otherMenu.bottom_text;

}	





//Destructor						

Menu::~Menu(){

	delete [] this->option_list; 

}

void Menu::double_capacity(){

	this->max_list_size = this->max_list_size * 2;

}
					

void Menu::push_back(char *pOption){			

	// Check for overflow

	if (this->count == this->max_list_size){

		cout << "Cannot support more than "<< this->max_list_size << " options" << endl;

		return;

	}

	
	this->option_list[this->count].set(pOption);

	this->count = this->count + 1;

	return;	

}						

							

void Menu::push_back(const Text &option){ 	

	// check OVERFLOW

	if (this->count == this->max_list_size){

		cout << "Cannot support more than "<< this->max_list_size << " options" << endl;

		return;

	}

	

	this->option_list[this->count] = option;

	this->count = this->count + 1;

	return;

}			






void Menu::insert(int index, Text &option){

	//Check if index given is negative

	if(index<0){

		cout << "No negative indexing" << endl;

	}

	

	//Check if index given is valid

	if ((index - 1) > this->count){

		cout << "Index out of bounds" <<endl;

	}

	

	

	if(this->count == this->max_list_size){

		cout<< "Stack is full. Increase size to insert" << endl;

	}

	

	

	

	

	Text *new_option_list = new Text[this->max_list_size];

	

	int new_index = 0;



	for (int c = 0 ; c <= (this->max_list_size - 1); c++){		

		

		

		if (c == (index - 1)){

			new_option_list[c] = option;

			new_index = new_index + 1;

			continue;

		}

		

		// Copy from this->option_list to new_option_list

		new_option_list[new_index] = this->option_list[c];

		new_index = new_index+1;

	}

	

	//deallocate this->option_list

	delete [] this->option_list;

	

	//allocate memory to this->option_list

	this->option_list = new Text[this->max_list_size];

	

	//Now copy from new_option_list to new this->max_list_size

	for (int c = 0 ; c <= (this->count - 1) ; c++){

		this->option_list[c] =  new_option_list[c]; 

	}

	

	//increase count

	this->count = this->count + 1;



}





//No idea if NULL pointer can be returned

// removes option from STACK option_list at index i											

Text Menu::remove(int index){

	//Check if stack is empty

	if(this->isEmpty()){

		cout << "Nothing to remove" << endl;

		Text t;

		return t;

	}

	//Check if index given is negative

	if(index<0){

		cout << "No negative indexing" << endl;

		Text t;

		return t;

	}

	

	//Check if index given is valid

	if ((index - 1) > this->count){

		cout << "Index out of bounds" <<endl;

		Text t;

		return t;

	}

	

	

	//If index is not out of bounds

	Text temp = this->option_list[index-1];

	

	Text *new_option_list = new Text[this->max_list_size];

	

	int new_index = 0;

	

	for (int c = 0 ; c < (this->max_list_size - 1); c++){		// -1 because max_list_size if 4 then index is 0, 1, 2, 3

		

		// Skip this because we dont want to copy this to the new array

		if (c == (index - 1)){

			continue;

		}

		

		// Copy from this->option_list to new_option_list

		new_option_list[new_index] = this->option_list[c];

		new_index = new_index+1;

	}

	

	//deallocate this->option_list

	delete [] this->option_list;

	

	//allocate memory to this->option_list

	this->option_list = new Text[this->max_list_size];

	

	//Now copy from new_option_list to new this->max_list_size

	for (int c = 0 ; c <= (this->count - 1) ; c++){

		this->option_list[c] =  new_option_list[c]; 

	}

	

	//update this->count

	this->count = this->count - 1;

	

	return temp;

	

}	





// Returns size of STACK option_list NOTE THIS IS NOT max_list_size						

const int Menu::size(){

	return this->count;

}





// Returns value of max_list_size						

const int Menu::capacity(){

	return this->max_list_size;

}





// POP from STACK max_list_size						

void Menu::pop_back(){	

	if (this->isEmpty()){

		cout<< "There is nothing to remove" << endl;

	}else{

		this->remove(this->count - 1);

	}

}							





// Return k th From stack option_list											

Text Menu::get(int k){

	//if empty send message

	if(this->isEmpty()){

		cout<< "Empty stack"<< endl;

		Text t;

		return t;

	}else{

		return this->option_list[k-1];

	}

}



// Read all in STACK option_list and concatinate them in char*, store in Text object and return it

const Text Menu::toString(){

	// Create a Text object to store the string representation of Menu object. This is the object that we will return;

	Text toStr;

	


	//append top_message

	toStr.append(this->top_text);

	toStr.append("\n");

	

	//append top_prompt

	toStr.append(this->top_prompt);

	toStr.append("\n");



	//append all Text object in option_list array of Menu

	for (int c = 0; c <= (this->count - 1); c++){

				 

		char *conc = new char[4];

		conc[0] = '(';

		conc[1] = '0' + (c+1);						//Convert to char from ASCII

		conc[2] = ')';

		cout<<conc;

		

		//append conc and option_list[c] to toString Text object

		toStr.append(conc);

		toStr.append(this->option_list[c]);

		//append next line escape sequence

		toStr.append("\n");

	}
	

	toStr.append("?->");

	toStr.append(this->bottom_prompt);

	toStr.append("\n");

	
	toStr.append(this->bottom_text);
	return toStr;

}	



int Menu::read_option_number(){

	cout << this->toString();

	char *user_choice = new char[256];

	bool condition = true;

	int option;

	while (condition){

		cin.getline(user_choice,256);

		try{

			option = atoi(user_choice);

		}catch(invalid_argument &e){

			cout << "Invalid entry, must be a number" << endl;

		}catch(out_of_range &e){

			cout << "Wahahahahahahahah!!! art thou SERIOUS??" << endl;

		}

		
		if(option > this->count){

			cout << "invalid choice" << option << ". It must be within range [1," << 1 << "]" << endl;

		}else{

			condition = true;

		}

	}

}					


void Menu::set_top_prompt(const Text &t){

	this->top_prompt = t;

}


void Menu::set_bottom_prompt(const Text &t){

	this->bottom_prompt = t;

}


void Menu::set_top_message(const Text &m){

	this->top_text = m;

}


void Menu::set_bottom_message(const Text &m){

	this->bottom_text = m;

}

void Menu::clear_top_prompt(){

	this->top_prompt.set("");

}

void Menu::clear_bottom_prompt(){

	this->bottom_prompt.set("");

}					

void Menu::clear_top_message(){

	this->top_text.set("");

}					


void Menu::clear_bottom_message(){

	this->bottom_text.set("");	

}





const bool Menu::isEmpty(){                       
	if (this->count == 0){

		return true;

	}else{

		return false;

	}

}





//Operator Overloading

Menu & Menu::operator= (const Menu &menu){

	delete [] this->option_list;

	this->option_list = new Text[menu.max_list_size];



	for(int c=0; c<=menu.count - 1; c++){

		this->option_list[c] = menu.option_list[c];

	}

	

	//copy all other members

	this->count = menu.count;

	this->max_list_size = menu.max_list_size;

	this->top_prompt = menu.top_prompt;

	this->bottom_prompt = menu.bottom_prompt;

	this->bottom_text = menu.bottom_text;

	this->top_text = menu.top_text;

	

	return *this;

}



//need tostring for this.

std::ostream& operator<<(std::ostream &os , Menu &menu){

	os << menu.toString();

	return os;

}



int main(){

	Menu m;

	Text t = m.toString();

	//cout << t.isEmpty();

	//menu.set_bottom_message("abcd");

};

Topic archived. No new replies allowed.