Segmentation Fault (core dump)

I'm creating an ADT Bag and have to dynamically allocate size for arrays. Once the array becomes full I need to dynamically allocate more space for it, so it can never be full. However when my original array is full, I'm unable to add another item to it, as I get the Segmentation Fault (core dumped) error. Any insight onto why this is happening would be greatly appreciated.

Header File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   template <typename ItemType>
    13  class itemBag : public BagInterface<ItemType> {
    14   private:
    15    /** Maximum capacity of this bag. */
    16    int arraySize = 6;
    17
    18    ItemType* items;
    19    ItemType* receipts;
    20    /** Data storage. */
    21    //  items = new ItemType[arraySize];
    22    //  receipt = new ItemType[arraySize];
    23
    24    /** Number of items in this bag. */
    25    int itemCount = 0;
    26
    27    /** Maximum capacity of this bag. */
    28    int maxItems = arraySize;


Implementation File

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
 template <typename ItemType>
    30  int itemBag<ItemType>::add(const ItemType& newEntry) {
    31    //Checking to see if there is room in array
    32    bool hasRoomToAdd(itemCount < maxItems);
    33    int temp;
    34    //If true add item add 1 to item count
    35    /* if (hasRoomToAdd) {
    36      items[itemCount] = newEntry;
    37      receipts[itemCount] = itemCount;
    38      temp = itemCount;
    39      ++itemCount;
    40      }
    41    */
    42    if(!hasRoomToAdd){
    43      ItemType* oldItems = items;
    44      ItemType* oldReceipts = receipts;
    45      maxItems *= 2;
    46      items = new ItemType[maxItems];
    47      receipts = new ItemType[maxItems];
    48
    49      for (int i = 0; i < maxItems; ++i){
    50        items[i] = oldItems[i];
    51        receipts[i] = oldReceipts[i];
    52
    53        delete [] oldReceipts;
    54        delete [] oldItems;
    55      }
    56    }
    57
    58    items[itemCount] = newEntry;
    59    receipts[itemCount] = itemCount;
    60    temp = itemCount;
    61    ++itemCount;
    62
    63    return temp;
    64  }


Driver File
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 std::string items[] = {"one", "two", "bat", "four", "five", "one"};
    42    //Changes
    43
    44
    45    cout << "Add 6 items to the bag: "
    46         << endl;
    47
    48    for (int i(0); i < 6; ++i) {
    49     index = bag.add(items[i]);
    50     cout<<items[i]
    51         <<" "
    52         <<"was placed in the index point: "
    53         <<index
    54         <<endl;
    55     ++count;
    56    }
 
//GETTING SEGMENTATION FAULT HERE
 cout << "Try to add another entry: add(\"extra\") returns "
    73         << bag.add("glove")
    74         << "; should be index of where it was placed."
    75         << endl;
Line 21/49: You copy the new size while it should be itemCount.


line 25/53 and 26/54: Move this lines out of the loop (they are the reason for the crash).
Topic archived. No new replies allowed.