C++ Segmentation Fault?
-
This program is supposed to allow the user to enter 5 names and then add one, delete one, and try to delete one that's not on the list. I can add one, but keep getting "segmentation fault" after trying to delete one. Any suggestions? #include<iostream> #include<string> using namespace std; void read5names(string* names, int size); void displayNames(string* names, int size); string* addEntry(string* dynamicArray, int &size, string newEntry); string* deleteEntry(string* dynamicArray, int &size, string entryToDelete); int findName(string* dynamicArray, int size, string entryToDelete); int main() { //declarations string* names; string newname; int size = 5; int i; names = new string[5]; //read 5 names cout << "Enter 5 names, one per line\n"; read5names(names, size); cout << "\n===== ORIGINAL LIST =====\n"; displayNames(names, size); //add a name and resize cout << "\nEnter a new name to add\n"; cin >> newname; names = addEntry(names, size, newname); cout << "\n===== LONGER LIST =====\n"; displayNames(names, size); //remove a name and resize - attempt 1 cout << "\nEnter a name to delete\n"; cin >> newname; names = deleteEntry(names, size, newname); cout << "\n===== SHORTER LIST =====\n"; displayNames(names, size); //remove a name and resize - attempt 2 cout << "\nEnter a name to delete that is NOT in the list\n"; cin >> newname; names = deleteEntry(names, size, newname); cout << "\n===== FINAL LIST =====\n"; displayNames(names, size); return 0; } void displayNames(string* names, int size) { int i; for (i = 0; i < size; i++) cout << names[i] << endl; } void read5names(string* names, int size) { int i; for (i = 0; i < size; i++) cin >> names[i]; } string* addEntry(string* dynamicArray, int &size, string newEntry) { //declare new array one element larger string *newArr; newArr = new string[size + 1]; //copy names over into new array int i; for (i = 0; i < size; i++) newArr[i] = dynamicArray[i]; //add the new name newArr[size] = newEntry; //increase the size size++; //delete the old array delete[] dynamicArray; //return the pointer to the new array return newArr; } string* deleteEntry(string* dynamicArray, int &size, string entryToDelete) { //determine if entryToDelete is in the dynamicArray int location; //location of the match location = findName(dynamicArray, size, entryToDelete); if (location >= 0) //name found, now delete it { //declare new array one size smaller string *newArr; newArr = new string[size - 1]; //copy names over into new array int i, index; for (i = 0; i < size; i++) { if (i != location) { newArr[index] = dynamicArray[i]; index++; } } //decrement size size--; //delete the old array delete[] dynamicArray; //return the pointer to the new array return newArr; } else //name not found { { cout << entryToDelete << " not found!\n"; //return the original array pointer return dynamicArray; } } int findName(string* dynamicArray, int size, string entryToDelete) { int i; for (i = 0; i < size; i++) { if (dynamicArray[i] == entryToDelete) { return i; break; } } return -1; }
-
Answer:
The main problem I see is in both addEntry() and deleteEntry(). They both reallocate the array, but don't have a way to pass the pointer to the reallocated array BACK to the caller. You need to pass the pointer variable by reference, not by value. That means you need to have either a reference to a pointer to a string or a pointer to a pointer to a string. The first option is more "C++"-like, and the correct syntax for the delete function headers is: string* deleteEntry(string* &dynamicArray, int &size, string entryToDelete) { ... } That says that dynamicArray is a reference to a pointer variable. Now, when dynamicArray is reassigned, the callers pointer variable will be modified to point to the reallocated array. Make the same change to addEntry()..
bethann_... at Yahoo! Answers Visit the source
Related Q & A:
- How to artificially cause a page fault in linux kernel?Best solution by Stack Overflow
- How to learn C# (moving from C++ to C#?Best solution by stackoverflow.com
- What exactly is a fault bucket and what causes it?Best solution by Stack Overflow
- What is geographic segmentation?Best solution by Stack Overflow
- Is this the American tourist's fault or the Chinese person's fault?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.