Trouble with Array Lists And Using the Insert and Retrieve Functions Given (Segmentation Fault Core Dumped Error)

Since StackOverflow is useless let me ask you guys about this.


I am having difficulty implementing a few functions in my array list.

I want to use the predefined function "insert" to insert a value into the list. I then want to use the retrieve function to print out the values of the entire list.

However, when I test my function, the program exits with a segmentation core dump after the last value is inserted

Any help would be appreciated.

Header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** @file ListA.h */
#include <string>
using namespace std;
const int MAX_LIST = 0;
typedef string ListItemType;
class List
{

public:
   List();
   bool isEmpty() const;
   int getLength() const;
   void insert(int index, const ListItemType& newItem, bool& success);
   void retrieve(int index, ListItemType& dataItem, bool & success) const;
   void remove(int index, bool& success);
   List selectionSort(List selectList);
private:
    ListItemType items[];
    int size;
    int translate(int index) const;
};


Implementation:

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
/** @file ListA.cpp */
#include "ArrayList.h"  // header file
#include <iostream>
#include <fstream>
List::List() : size()
{
}
bool List::isEmpty() const
{
   return size == 0;
}
int List::getLength() const
{
   return size;
}
void List::insert(int index, const ListItemType& newItem, bool& success)
{
   success = (index >= 1) &&
             (index <= size + 1) &&
             (size < MAX_LIST);
   if (success)
   {
      for (int pos = size; pos >= index; --pos)

         items[translate(pos + 1)] = items[translate(pos)];
      items[translate(index)] = newItem;

      ++size;

   }

}
void List::remove(int index, bool& success)
{
   success = (index >= 1) && (index <= size);
   if (success)
   {
      for (int fromPosition = index + 1;
       fromPosition <= size;
       ++fromPosition)
         items[translate(fromPosition - 1)] = items[translate(fromPosition)];
      --size;  // decrease the size of the list by one
   }  // end if

}  // end remove

void List::retrieve(int index, ListItemType& dataItem,
bool& success) const
{
   success = (index >= 1) && (index <= size);
   if (success)
      dataItem = items[translate(index)];
}

int List::translate(int index) const
{
   return index - 1;
}

List List::selectionSort(List selectList)

{


return selectList;
}


 int main()
 {
 ListItemType insertType = "listItem1";
 ListItemType retrieveType = "listitem2";
 int numberofitems;
 cout << "Please enter the number of data items:" << endl;
 cin >> numberofitems;
 cout << endl;
 cout << "Please enter the data items, one per line:" << endl;
 int listofitems[numberofitems];
 List myArrayList;
 cout << myArrayList.getLength() << endl;
        if (myArrayList.isEmpty())  // tests before
    {
        cout << "This list is empty \n" << endl;
    }
    else
    {
        cout << "List is not empty! \n"<< endl;
    }
 bool mainsucc = true;
 for (int i = 0; i<numberofitems; i++)
 {
 cout << "Enter number " << i + 1 << " : " << endl;
 cin >> listofitems[i];
 }
 for (int i =0; i <numberofitems; i++){
 myArrayList.insert(listofitems[i],  insertType, mainsucc);}
 cout << "Size of the list is : " << myArrayList.getLength() << endl;
/*for (int i=0; i<mainarraylistsize; i++)
 {
 cout << myArrayList.retrieve(listofitems[i], retrieveType, mainsucc);
}*/
if (myArrayList.isEmpty())  // tests after
    {
        cout << "This list is empty \n" << endl;
    }
    else
    {
        cout << "List is not empty! \n"<< endl;
    }
 return 1;
 }
Last edited on
Bump
C++ doesn't have "Array Lists." I would imagine std::vector is what you're after.

ListItemType items[]; creates an array of 0 elements. Accessing any element of that array is illegal and will result in undefined behavior, including the possibility of a segmentation fault. You might want to disable the compiler extension that allows it to avoid this mistake in the future (it isn't legal C++.)

Last edited on
Topic archived. No new replies allowed.