Dynamic memory segmentation fault

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
#include <iostream>
#include <string>
#include <new>
#include <cstdlib>
using namespace std;
int main(){
int i = 1;
do{
		string * p;
	p = new (nothrow) string[i];
	cout << '\t' << '\t' << "Choose an option below";
	cout << endl << "Enter new name - A";
	cout << endl << "Browse names - B";
	cout << endl << "Delete a name - C";
	cout << endl << endl << "Enter choice -> ";
		string choice;
		getline(cin, choice);
			if (choice == "a" || choice == "A"){
			int n=i;
			if (p == 0)
			cout << "Error: memory could not be allocated.";
				else {
				for(n=i;n==i;i++){
				cout << "Enter name -> ";
				getline(cin, p[i]);
				i++;
	}
cout << p[n];
cin.get();
system("clear");
for (n=0; n<i; n++)
cout << p[n] << ", ";
cin.get();
}
}
}while(1==1);
cin.get();
}



Please help me understand why I am receiving this error.
Take a careful look at lines 23 and 25. Your array is of size i, and what you're going at line 25 is accessing its ith element.

EDIT: That's lines 24 and 26 in Disch's reposted code.

-Albatross
Last edited on
Reposted with proper indenting:

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
#include <iostream>
#include <string>
#include <new>
#include <cstdlib>
using namespace std;

int main(){
	int i = 1;
	do{
		string * p;
		p = new (nothrow) string[i];
		cout << '\t' << '\t' << "Choose an option below";
		cout << endl << "Enter new name - A";
		cout << endl << "Browse names - B";
		cout << endl << "Delete a name - C";
		cout << endl << endl << "Enter choice -> ";
		string choice;
		getline(cin, choice);
		if (choice == "a" || choice == "A"){
			int n=i;
			if (p == 0)
				cout << "Error: memory could not be allocated.";
			else {
				for(n=i;n==i;i++){
					cout << "Enter name -> ";
					getline(cin, p[i]);
					i++;
				}
				cout << p[n];
				cin.get();
				system("clear");
				for (n=0; n<i; n++)
					cout << p[n] << ", ";
				cin.get();
			}
		}
	}while(1==1);
	cin.get();
}


EDIT:

Your problem is here:

getline(cin, p[i]);

p[i] is out of bounds of your array. Your array can only go from p[0] to p[i-1]. p[i] is passed the end of the array, therefore you're trying to modify a string that hasn't been constructed, and are corrupting the heap and doing all sorts of other nasty stuff that is causing the segfault.

You seem to be doing a lot of questionable things here. Why are you creating an array of strings if i is always 1? Why not just use a single string?

What is the point of this loop: for(n=i;n==i;i++){ ?

If I were you, I'd get rid of that loop, get rid of the new[] line, and just use a normal string:

 
string p;  // no need for new[] 
Last edited on
Disch,

I is not always 1. It is initialized as 1 before the loop begins. And, at the end of the loop, i++. So when you are entering another name, i=2. so on and so forth.
But I do thank you. i-1 solved my problem (:
Topic archived. No new replies allowed.