Insert and Attach Functions for a Sequence Array Assignment

This is my constructor that I'm using:
1
2
3
4
5
6
7
8
9
10
11
//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.
I do not understand what funcrion attach does. But in any case there is no element with index equal to capacity in array data.

1
2
3
4
5
6
7
8
9
10
void sequence::attach(const value_type& entry)
{
	for(int i = used; i < capacity; i++)
	{
		data[i] = data[i+1];
		used++;
		data[current_index] = entry;
	}

}

The same is valid and for the second function.
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.
Not sure if I understand what you're saying. Do you mean something like this:
 
for(int i = capacity-1; i < capacity;i++)
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.
Look here

1
2
3
4
5
	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.
Last edited on
Also it is not clear from your code what is current_index. At least I do not see that you would initialize such data member in the constructor.
1
2
3
4
5
value_type sequence::current() const
{
	is_item();
	return data[current_index];
}


1
2
3
4
5
void sequence::start()
{
	// Current index equals the 1st element on the array
	current_index = data[0];
}

The current_index starts at data[0] and as the user inserts/attaches an element to the array the current_index is suppose to adjust accordingly.
1) I do not see where current is defined. 2) it seems that this code current_index = data[0]; is simply a stupidy.
Last edited on
Oh I forgot to initialize current_index in my constructor as 0.
Last edited on
Topic archived. No new replies allowed.