Segmentation error help

Hello, I am working on a project for my C++ class and i am almost finished but i keep getting a segmentation error. I know what segmentation error means but i don't know what is causing it here. Any help would be appreciated and please try to explain it simply. The program reads in 5 words from the user, adds a word, then deletes a word all with strings and pointers.

here's my code...

i think the error occurs in line 97 because i added location... but how do i delete the slot dynamicArray[location] ?
------------------------------------------------------------------------

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
// This program implements a dynamic array of strings
// Problem 6, p521-522
// Spring Semester 2012

#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";
  getline(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";
  getline(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";
  getline(cin, newname);
  names = deleteEntry(names, size, newname);
  cout << "\n===== FINAL LIST    =====\n";
  displayNames(names, size);
}

void displayNames(string* names, int size)
{
   for(int i=0; i<size; i++)
   {
      cout << *(names+i) << endl;
   }

}

void read5names(string* names, int size)
{
   for(int i=0; i<size; i++)
   {
      getline(cin, names[i]);
   }

}

string* addEntry(string* dynamicArray, int &size, string newEntry)
{
   // declare new array one element larger
   string* onelarger;
   onelarger = new string[size+1];
   // copy names over into new array
   for(int i=0; i<(size); i++)
   {
      onelarger[i] = *(dynamicArray+i);
   }
   // add the new name
   onelarger[5] = newEntry;
   // increase the size
   size = size+1;
   // delete the old array
   delete []dynamicArray;
   // return the pointer to the new array
   return onelarger;
}

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.
      delete (dynamicArray+location);
     // declare new array one size smaller
      string* onesmaller;
      onesmaller = new string[size-1];
     // copy names over into new array
      for(int i=0; i<(size-1); i++)
      {
         onesmaller[i] = *(dynamicArray+i);
      }
     // decrement size
      size = size-1;
     // return the pointer to teh new array
      return onesmaller;
   }
   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 location = -1;
   for(int i=0; i< size; i++)
   {
      if(*(dynamicArray+i) == entryToDelete)
      {
         location=i;
      }
   }

   return location;

}

Last edited on
Indeed, line 97 is a problem. You may only use delete on an address you obtain with new, and then it must be the corresponding delete[] for new[]'d memory.

The correct way to delete your entry is to allocate the smaller array, copy all but the entry you want to delete to the smaller array. delete[] the old array. return the new array.

Just fyi, you can write *(dynamicArray+i) as dynamicArray[i].
Thank you so much! I finished it thanks to your help. It turns out i didn't even need line 97 in the first place. lol.

Thank you!
Topic archived. No new replies allowed.