//PARAMETERIZED CONSTRUCTOR
//sequence(int initial_capacity = DEFAULT_CAPACITY)
//Postcondition: The sequence has been initialized as an empty sequence.
//The insert/attach functions will work efficiently (without allocating
//new memory) until this capacity is reached.
sequence::sequence(int initial_capacity)
{
data = new value_type[initial_capacity];
capacity = initial_capacity;
used = 0;
}
I've been coming up with this error for my attach function:
Unhandled exception at 0x00e34008 in Sequence2.exe: 0xC0000005:
Access violation writing location 0x66dcadd8.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// void attach(const value_type& entry)
// Postcondition: A new copy of entry has been inserted in the sequence after
// the current item. If there was no current item, then the new entry has
// been attached to the end of the sequence. In either case, the newly
// inserted item is now the current item of the sequence.
void sequence::attach(const value_type& entry)
{
for(int i = used; i < capacity; i++)
{
data[i] = data[i+1];
used++;
data[current_index] = entry;
}
}
and this error for my insert function:
Unhandled exception at 0x01053f5a in Sequence2.exe: 0xC0000005:
Access violation writing location 0x6679add8.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// void insert(const value_type& entry)
// Postcondition: A new copy of entry has been inserted in the sequence
// before the current item. If there was no current item, then the new entry
// has been inserted at the front of the sequence. In either case, the newly
// inserted item is now the current item of the sequence.
void sequence::insert(const value_type& entry)
{
for(int i = used; i < capacity; i++)
{
data[i] = data[i+1];
used++;
}
data[current_index] = entry;
}
Maybe I just have it typed completely wrong or I'm over-thinking/under-thinking these two functions. Advice is appreciated.
That it would be more clear when i == capacity - 1 that is i < capacity then i + 1 is equal to capacity. However valid indexes for data are 0 - capacity - 1.
The insert function inserts a value into the sequence before the current index of the array. The attach function is suppose to insert it after the current index of the array.
for(int i = used; i < capacity; i++)
{
data[i] = data[i+1];
used++;
}
When i is equal to capacity - 1 you are trying to access the element with index i + 1 that is equal to capacity. You have no such an element with index equal to capacity in the array.