Hi all,
I've built a Queue class which basically uses dynamic memory allocation to make an array grow with every new element inserted. It compiles and works perfectly under dev c++, but when I included the class in a VC++ 10 Express project and instantiated an object from it, the compiler threw the following run time error:

Access violation reading location 0xccccccc0

I'm using this class for an important school project. Is there ANY way at all how can I get this class working on msvc++?

Here's the code if anyone wants to take a look...

class MyQueue
 {
// the data type of the queue
#define Q_TYPE char		

   private:
	// extends the queue, used internally
	int extend(int newElts, bool abs = false, bool restore = true);

   public:
	// current number of elements
	unsigned int crtElts;
	// the pointer to the queue
	Q_TYPE *ptrQ;

	// default constructor
	MyQueue(int newElts = 1);
	// default destructor
	~MyQueue();

	// add new element at the back of the queue and adds empty element after it
	void insert(Q_TYPE newVal);
	// gets first element in the queue
	Q_TYPE remove(bool delElt = true);

 };

//********************************

MyQueue::MyQueue(int newElts)
 {
	this->extend(newElts, true, false);
 }

//********************************

MyQueue::~MyQueue()
 {
	delete[] ptrQ;
 }

//********************************

int MyQueue::extend(int newElts, bool abs, bool restore)
 {
	Q_TYPE *bckArray;
	int prvElts = crtElts;

	if(restore)
	 {
		bckArray = new Q_TYPE[prvElts];

		for(int i = 0; i < prvElts; ++i)
			bckArray[i] = ptrQ[i];
	 }

	if(abs)
		crtElts = newElts;
	else
		crtElts += newElts;

	if(crtElts < 1)
		crtElts = 1;

	delete[] ptrQ;
	ptrQ = new Q_TYPE[crtElts];

	if(restore)
	 {
		for(int i = 0; (i < prvElts) && (i < crtElts); ++i)
			ptrQ[i] = bckArray[i];
		delete[] bckArray;
	 }

	ptrQ[crtElts - 1]= (Q_TYPE) 0;

	return crtElts;
 }

//********************************

void MyQueue::insert(Q_TYPE newVal)
 {
	int nxtElt = this->extend(1);
	ptrQ[nxtElt - 2] = newVal;
 }

//********************************

Q_TYPE MyQueue::remove(bool delElt)
 {
	Q_TYPE lastElt = ptrQ[0];
	Q_TYPE *bckArray;

	if((crtElts > 1) && (delElt))
	 {
		bckArray = new Q_TYPE[crtElts - 1];

		for(int i = 1; i < crtElts; ++i)
			bckArray[i - 1] = ptrQ[i];
		delete[] ptrQ;

		--crtElts;
		ptrQ = new Q_TYPE[crtElts];
		for(int i = 0; i < crtElts; ++i)
			ptrQ[i] = bckArray[i];
		delete[] bckArray;

		ptrQ[crtElts - 1] = (Q_TYPE) 0;
	 }

    return lastElt;
 }

Help appreciated greatly...

Thanks,
Dean

Its crashing when extend() is called from the class constructor because you failed to initiailze class variables before calling that function. Line 65 is trying to delete an ininitialized pointer. That is bad regardless of what compiler you used.

Ah ok, now I understand... Tnks, it worked perfectly!

Why does deleting an uninitialized pointer throw an error?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.