2 Replies - 265 Views - Last Post: 25 March 2012 - 07:07 AM Rate Topic: -----

#1 tcs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-March 12

Freeing memory in a Queue

Posted 25 March 2012 - 06:37 AM

Hi, I'm attempting to create a somewhat specific Queue Data Structure using a Linked List as the basis. I have used linked lists before to a limited extent, and though I always have some problems, I usually work them through.

However, with this one I just can't see where I'm going wrong. The code compiles fine, and it all works, unless I try to delete the top member of the queue. Originally, it was calling an Assert error, but after I removed the assert header (as I was not using it all that much), it changed to a Breakpoint, but couldn't point to a specific point in the code.

// queue.h
// This file declares a data structure for a queue
#pragma once

template <class Tx, class Ty, int Nx> struct Node
{
	Tx dataX[Nx];
	Ty dataY;
	Node *next;
};

template <class Tx, class Ty, int Nx> class Queue
{
	Node<Tx, Ty, Nx> *first, *last;
public:
	int size;
	Queue();
	~Queue();
	void add(Tx _dataX[Nx], Ty _dataY);
	void insert(Tx _dataX[Nx], Ty _dataY);
	void getX(Tx (&_dataX)[Nx]);
	void getY(Ty (&_dataY));
	void del();
};

template <class Tx, class Ty, int Nx> Queue<Tx, Ty, Nx>::Queue()
{
	first = NULL;
	last = NULL;
	size = 0;
}

template <class Tx, class Ty, int Nx> Queue<Tx, Ty, Nx>::~Queue()
{
	while(first != NULL) del();
}

template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::add(Tx _dataX[Nx], Ty _dataY)
{
	if(last == NULL)
	{
		first = new Node<Tx, Ty, Nx>;
		for(int i=0;i<Nx;i++) first->dataX[i] = _dataX[i];
		first->dataY = _dataY;
		last = first;
	}
	else
	{
		last->next = new Node<Tx, Ty, Nx>;
		last = last->next;
		for(int i=0;i<Nx;i++) last->dataX[i] = _dataX[i];
		last->dataY = _dataY;
	}
	size++;
}

template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::insert(Tx _dataX[Nx], Ty _dataY)
{
	if(first == NULL)
	{
		first = new Node<Tx, Ty, Nx>;
		for(int i=0;i<Nx;i++) first->dataX[i] = _dataX[i];
		first->dataY = _dataY;
	}
	else
	{
		Node<Tx, Ty, Nx> *tmp = new Node<Tx, Ty, Nx>;
		for(int i=0;i<Nx;i++) tmp->dataX[i] = _dataX[i];
		tmp->dataY = _dataY;
		tmp->next = first;
		first = tmp;
	}
	size++;
}

template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::getX(Tx (&_dataX)[Nx])
{
	for(int i=0;i<Nx;i++) _dataX[i] = first->dataX[i];
}

template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::getY(Ty (&_dataY))
{
	_dataY = first->dataY;
}

template <class Tx, class Ty, int Nx> void Queue<Tx, Ty, Nx>::del()
{
	if(first != NULL)
	{
		Node<Tx, Ty, Nx> *tmp = first;
		first = first->next;
		delete tmp;
		size--;
	}
}

I'm using VC++ EE 2008, on a Windows XP Operating System. Though, as far as I'm aware, this shouldn't matter with the code I'm writing.

It took me a while to nail down where the error was in the code, as (mentioned before) it did not give me a line to work with. Eventually I commented out the delete line in the del() function, and voila, it ran smoothly (save for the memory leak).

Can anyone help in some way? I'm still pretty new to C++, and I'm sure it could quite possibly be terribly coded.

Is This A Good Question/Topic? 0
  • +

Replies To: Freeing memory in a Queue

#2 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: Freeing memory in a Queue

Posted 25 March 2012 - 06:52 AM

You never initialize any of the next pointers to NULL. And size should probably be private.
Was This Post Helpful? 1
  • +
  • -

#3 tcs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-March 12

Re: Freeing memory in a Queue

Posted 25 March 2012 - 07:07 AM

Oh! That seems rather obvious now that you pointed it out! I got it to work with a few tweaks now, thanks for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1