Help with self made vector class

Hello all, first time posting...

So my professor assigned us the task of making our own vector class, and I'm having an issue with the member function reserve() or possibly with my constructor that passes two arguments. The error I get is...

Unhandled exception at 0x775815de in IntVector2.exe: 0xC0000005: Access violation writing location 0xcccccccc.

The code is as follows, I'm new to this and any help or advice would be appreciated. I did search the forums for similar problems, and the internet, but couldn't find anything. I'm using Visual C++ 2008. I've also commented out a couple of member functions in the header file that I havn't started working on yet.

If I forgot to include anything please let me know, thanks again!


IntVector.h 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

class IntVector
{
	public:
		//typedef T * iterator;
		//typedef T value_type;
		//constructors
		IntVector(); 
		IntVector(const IntVector & v); //copy constructor
		IntVector & operator=(const IntVector & v); //
		IntVector(unsigned int, int);
		~IntVector();
		//member functions
		void clear();
		unsigned int size() const;
		bool empty() const;
		unsigned int capacity() const;
		void reserve(unsigned int);
		void resize(unsigned int);
		void resize(unsigned int, int);
		int front() const;
		int back() const;
		void pop_back();
		void push_back(int);
		//iterator begin() const;
		//iterator end() const;
		//operator
		int & operator[](unsigned int);


	private:
		unsigned int my_capacity; //int that holds the number of items that can be stored in the buffer
		unsigned int my_size; // int that holds the number of items that are actually in the vector
		int * buffer; //buffer is a pointer to a type T[Template]
};



IntVector.cpp 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

IntVector::IntVector(unsigned int n, int x)
{
	my_capacity = n;
	my_size = n;
	for (unsigned int i = 0; i < my_size - 1; i++)
		buffer[i] = x;
}


void IntVector::reserve(unsigned int n)
{
	if (buffer == 0)
	{
		my_size = 0;
		my_capacity = 0;
	}
	if (my_capacity >= n) //returns if the current capacity is already larger than the one being asked for
		return;

	int * oldbuffer = buffer; //creates a pointer type int called oldbuffer that is a copy of the current buffer

	buffer = new int[n];

	if (buffer == 0)
		return; //returns if the new buffer is equal to 0

	for (unsigned int i = 0; i < my_size; i++) //loop that copies the old values in the buffer to the new buffer
		buffer[i] = oldbuffer[i];

	delete [] oldbuffer; //deletes the memory allocation of the old buffer

	my_capacity = n; //sets the capacity size to the new value

}
Access violation writing location
usually means you have a pointer pointing somewhere where you don't belong.

buffer[i] = oldbuffer[i] You sure this does what you think?
You don't seem to be allocating any memory for the buffer in your constructor.

So then
1
2
for (unsigned int i = 0; i < my_size - 1; i++)
    buffer[i] = x;

here you'd be writing stuff to some memory location that you shouldn't be writing to....
Thank you both for the reply, I will see about reworking my constructor to allocate memory for the buffer and then let you know what happens, appreciate the advice.
Topic archived. No new replies allowed.