• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
KittensWithSpikes

Double ended link list help

5 posts in this topic

Hi I'm currently working on a double ended link list and currently testing it as I implement it.

 

There are three  parts of code I'm Fuzzy on

 

First is the copy constructor and second is the assignment operator. the third is the implementation of the destroy  member function(deleting the list)

 

Currently this is the implantation I have for the assignment operator.

 

LinkedList& LinkedList::operator=(const LinkedList& rhs)
{
	if(this == &rhs)
		return *this;

	this->Destroy();

	this->mFirst = rhs.mFirst;
	this->mLast = rhs.mLast;


	return *this;
}

 

As for the copy constructor, I am not sure how to implement it .

 

then the destroy member function

 

void LinkedList::Destroy() 
{
	LinkedNode* current = nullptr;	
	if(!IsEmpty())
	{
		current =  mLast->previous;
		while(current != nullptr)
		{
			delete mLast;
			current->next = nullptr;
			mLast = current;
			current = mLast->previous;
		}
		delete mFirst;
		mLast = nullptr;
		mFirst = nullptr;
	}
}

Here is my Main function

 

int main()
{
	LinkedList list;
	list.InsertFirst(5);
	list.InsertFirst(24);
	list.InsertFirst(32);

	LinkedList list2;

	list2.InsertFirst(1);
	list2.InsertFirst(2);
	list2.InsertFirst(3);
	list2.InsertFirst(4);
	list2.InsertFirst(5);


	LinkedNode* current;

	current =  list.GetFirst();

	while (current != nullptr)
	{
		cout << current->data << endl;
		
		current = current->next;
	}

	cout << endl;

	list = list2;

	current =  list.GetFirst();

	while (current != nullptr)
	{
		cout << current->data << endl;
		
		current = current->next;
	}
	cout << endl;
}

 

now when executing this I get an error: debug assertion failed expression _block_type_is_valid(pHead->nBlockUse)

 

I'm unsure what this is error is I tried debugging for a while but not sure where things are going wrong. I believe it has something to do with deleting invalid memory.

 

i know it has something to do with the destroy  member function and copy assignment operator, as if i remove list = list2 from the main function everything works fine.

 

 

I appreciate any input/help on this

 

 

 

 

 

0

Share this post


Link to post
Share on other sites

You need to do a "deep copy" in your assignment operator. If we ignore exception safety for the moment, the thing to do is to iterate through the objects in the "rhs" list, and insert them into the list being assigned to.

 

What you end up with your current implementation is two lists that point to the same set of nodes. When one of these lists is destroyed, the other one is corrupted because it's pointers point at de-allocated memory (or worse, previously de-allocated memory that is now being re-used elsewhere).

 

I'd probably implement destroy as a simple forward loop, starting at the head and deleting each node:

[code]   

LinkedNode* current = mFirst;
while(current)
{
    LinkedNode *temp = current;
    current = current->next;
    delete temp;
}
mFirst = nullptr;
mLast = nullptr;
[/code]

Your code might be correct, but I'm finding it much harder to reason about.

1

Share this post


Link to post
Share on other sites

I wanted to make a suggestion that might make things a bit easier. Have you tried just using a vector with a wrapper class to allow for easy traversing? Bjarne Stroustrup in his C++11 keynote speech mentioned that due to caching, vectors are actually much faster than linked lists, and this would be very pertinent for game development. As the video of his speech was over an hour long, check out this post: http://bulldozer00.com/2012/02/09/vectors-and-lists/ . It includes Bjarne's slides with graphs of benchmarks.

0

Share this post


Link to post
Share on other sites

rip-off thanks for pointing me in the right direction, 

 

OK so after of bit of trial and error I have come up with this solution.

 

I wanted to know if this is valid? I mean it works with no errors. but then I am not sure if I am leaking memory or doing something im not supposed to.

 

thanks again for the help guys

 

LinkedList& LinkedList::operator=(const LinkedList& rhs)
{
	if(this == &rhs)
		return *this;

	this->Destroy();
	
	LinkedNode* rhsNode = nullptr;
	LinkedNode* node = nullptr;
	LinkedNode* temp =  nullptr;

	rhsNode = rhs.mFirst;

	while(rhsNode != nullptr)
	{
		node = new LinkedNode;
		if(IsEmpty())
		{
			mFirst = node;
			mLast =  node;
			node->data = rhsNode->data;

			mFirst->previous =  nullptr;
			mLast->next =  nullptr;
		}
		else
		{

			temp = mLast;
			mLast = node;
			temp-> next =  mLast;
			mLast->previous = temp;
			mLast->data = rhsNode->data;
		}

		rhsNode =rhsNode->next;
	}

	mLast->next = nullptr;

	rhsNode =nullptr;
	node = nullptr;
	temp = nullptr;

	return *this;
}

Scottlowe, thanks for your interesting link. The reason I am building my own double ended linked list is to use this as an exercise. I don't plan to use these data structure projects in any project but to learn. 

0

Share this post


Link to post
Share on other sites
You might find it easier to reason about your programs if you broke them into smaller chunks. For example, let's say you had two other functions: PushBack(), which adds a new node to the end of the linked list with the given data and PopBack() which removes the node at the end of the linked list. These are pretty useful functions to have for a linked list in and of themselves, but you could use them to implement things like copying another linked list (by calling PushBack() for the data you see in the other list) and clearing your linked list (by repeatedly calling PopBack() while the list isn't empty).
2

Share this post


Link to post
Share on other sites

Thanks SiCrane, I have not implemented any kind of remove functions yet, only insertions. Although I do see how the remove functions could make my Destroy Function more clean and simple. I really appreciate the help you guys have given me ;).

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0