?
Solved

Unhandled exception at 0x00fd5bf9 in BuddyMemory.exe: 0xC0000005: Access violation reading location 0xcdcdcdd5.

Posted on 2011-03-18
9
Medium Priority
?
827 Views
Last Modified: 2012-05-11
Hello Everyone,

I am writing a buddy memory simulation program.  So, the user can request a size of memory from 2 - 256.(By powers of 2).  So I have created a linked list and for each node it will hold one of the powers of 2.  And within each node it has a vector which will hold the block number from get_rangeNumber function.  What I am having trouble with is when I try to initialize the vector I will get a run time error: Unhandled exception at 0x00fd5bf9 in BuddyMemory.exe: 0xC0000005: Access violation reading location 0xcdcdcdd5. The error is coming from LinkedList class

LinkedList:
 
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include<iostream>
#include<vector>
#include<string>

using namespace std;

template<class Object> class ListNode;
template<class Object>
ostream &operator<<(ostream& out, const vector<Object> &node_linked_list);



template<class Object>
class ListNode
{
	friend ostream &operator<<(ostream& out, const vector<Object> &node_linked_list);

public:
	
	ListNode(Object nodeValue)
	{
		value = nodeValue;
		next = NULL;
	}


	ListNode(Object nodeValue, Object startValue, Object endValue)
	{
		value = nodeValue;
		next = NULL;
		start_blockValue=startValue;
		end_blockValue=endValue;

		node_linked_list->resize(4);
		
		
	}

	

	void set_rangeNumber();
	Object get_rangeNumber();

	void displayList();

	vector<Object> *node_linked_list;

	Object value;
	ListNode<Object> *next;
	
private:
	 Object start_blockValue;
	 Object end_blockValue;

};

template<class Object>
ostream &operator<<(ostream& out, const vector<Object> &node_linked_list)
{
	for(int i =0; i != node_linked_list.size(); i++)
		out<<*node_linked_list[i];
	return out;
}

template<class Object>
void ListNode<Object>::displayList()
{
	if(node_linked_list->size() > 0)
	{
		for(int i = 0; i != node_linked_list->size(); i++)
			cout<<node_linked_list;
	}
}


template<class Object>
void ListNode<Object>::set_rangeNumber()
{
	start_blockValue += 32;
}

template<class Object>
Object ListNode<Object>::get_rangeNumber()
{
	return start_blockValue;
}


template<class Object>
class LinkedList
{

private:
	ListNode<Object> *head;
	 int total_free_memory;
	 int total_used_memory;

public:
	LinkedList()
	{
		head = NULL;
		total_free_memory = 0;
		total_used_memory = 0;
	}

	~LinkedList();
	void appendNode(Object);
	void appendNode(Object, int, int);

	void insertNode(Object);
	void show_List(string);
	void find_value(Object);
	void deleteNode(Object);
	
	void add_free_memory(Object);
	void add_used_memory(Object);
	void set_memory(Object);

	Object getTotal_free_memory();
	Object getTotal_used_memory();

};

template<class Object>
void LinkedList<Object>::add_free_memory(Object value)
{
	free_memory += value;
	used_memory -= value;
}

template<class Object>
void LinkedList<Object>::add_used_memory(Object value)
{
	used_memory += value;
	free_memory -= value;
}

template<class Object>
void LinkedList<Object>::set_memory(Object value)
{
	total_free_memory = value;
}

template<class Object>
Object LinkedList<Object>::getTotal_free_memory()
{
	return total_free_memory;
}

template<class Object>
Object LinkedList<Object>::getTotal_used_memory()
{
	return used_memory;
}


template<class Object>
void LinkedList<Object>::appendNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr;

	newNode = new ListNode<Object>(newValue);

	if(head == NULL)
		head = newNode;

	else
	{
		nodePtr = head;

		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::appendNode(Object newValue, int startValue, int endValue)
{
	ListNode<Object> *newNode, *nodePtr;
	
	//Allocate a new node and store the value
	newNode = new ListNode<Object>(newValue, startValue, endValue);

	//If there are no nodes in the list
	//make newNode the first node
	if(head == NULL)
		head = newNode;
	//Insert the node at the end of the list
	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Find the last node in the list
		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::show_List(string name)
{
	cout<<"*****"<<name<<"*****"<<endl;

	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		cout<<nodePtr->value<<": ";

		if(!nodePtr->node_linked_list->empty())
		{cout<<nodePtr->node_linked_list;}

		nodePtr = nodePtr->next;
		cout<<endl;
	}

	cout<<""<<endl;
}

template<class Object>
void LinkedList<Object>::find_value(Object memory)
{
	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		if(nodePtr->value>= memory && (nodePtr->next->value<memory || nodePtr->next->value == NULL ))
		{	
			nodePtr->set_rangeNumber();
			nodePtr->node_linked_list->push_back(nodePtr->get_rangeNumber());
			break;
		}		

	}

}

template<class Object>
void LinkedList<Object>::insertNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr, *previousNode = NULL;

	//Allocate a new node and store newValue
	newNode = new ListNode<Object>(newValue);

	//If there are no nodes in the  list
	//make newNode the first node

	if(head == NULL)
	{
		head = newNode;
		newNode->next = NULL;
	}

	else
	{
		//Initialize nodePtr to head of list and previousNode to NULL
		nodePtr = head;
		previousNode = NULL;

		//Skip all nodes whose value member is less than newValue
		while(nodePtr != NULL && nodePtr->value < newValue)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		//If the new node is the first in the list
		if(previousNode == NULL)
		{
			head = newNode;
			newNode->next=nodePtr;
		}

		else
		{
			previousNode->next=newNode;
			newNode->next=nodePtr;
		}
	}
}

template<class Object>
void LinkedList<Object>::deleteNode(Object searchValue)
{
	ListNode<Object> *nodePtr, *previousPtr;

	//If the list is empty do nothing
	if(head == NULL)
		return;

	//Determine if the first node is the value that we are looking for
	if(head->value == searchValue)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}

	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Skip all nodes whose value member is not equal to search value
		while(nodePtr != NULL && nodePtr->value != searchValue)
		{
			previousNode=nodePtr;
			nodePtr = nodePtr->next;
		}

		//If nodePtr is @ the end of the list
		//than link the previous node to the node after nodePtr
		if(nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

template<class Object>
LinkedList<Object>::~LinkedList()
{
	ListNode<Object> *nodePtr, *nextNode;

	nodePtr = head;
	while(nodePtr != NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;

	}
}

#endif

Open in new window


Buddy.h
 
#ifndef BUDDYMEMORY_H
#define BUDDYMEMORY_H

#include<iostream>

using namespace std;
#include"LinkedList.h"

class BuddyMemory
{
public:
	BuddyMemory();
	~BuddyMemory();
	
	void check_free_memory(int);

	void addTo_allocated_list();

	void addTo_free_list();

private:
	LinkedList<int> allocated_list;
	LinkedList<int> free_list;

};
#endif

Open in new window


Buddy.cpp
 
#include<iostream>

using namespace std;

#include"BuddyMemory.h"
#include"LinkedList.h"

BuddyMemory::BuddyMemory()
{
	//Create allocated_list
	allocated_list.appendNode(256);
	allocated_list.appendNode(128);
	allocated_list.appendNode(64);
	allocated_list.appendNode(32);
	allocated_list.appendNode(16);
	allocated_list.appendNode(8);
	allocated_list.appendNode(4);
	allocated_list.appendNode(2);

	//Create free_list
	free_list.appendNode(256, 0,32);
	free_list.appendNode(128, 33, 97);
	free_list.appendNode(64, 98, 226);
	free_list.appendNode(32, 227, 483);
	free_list.appendNode(16, 484, 996);
	free_list.appendNode(8, 997, 2021);
	free_list.appendNode(4, 2022, 4070);
	free_list.appendNode(2, 4076, 80167);
	
	free_list.set_memory(256);

	free_list.show_List("Free List");
	allocated_list.show_List("Allocated List");

}

BuddyMemory::~BuddyMemory()
{
	 allocated_list.~LinkedList();
	 free_list.~LinkedList();
}

void BuddyMemory::check_free_memory(int value)
{
	if(free_list.getTotal_free_memory()+ value <= 256)
	{
		free_list.find_value(value);
	}
	else
		cout<<"You Must Free Up Space"<<endl;	
}

Open in new window


Main.cpp
 
#include<iostream>
#include<vector>
#include<string>

using namespace std;

#include"BuddyMemory.h"

int main()
{
	cout<<"***********BUDDY MEMORY PROGRAM**********\n"<<endl;

	BuddyMemory start_program;

	int memory;
	int choice=1;


	do
	{
			
		cout<<"Enter in corresponding number"<<endl;
		cout<<"1. Allocate Memory"<<endl;
		cout<<"2. Delete Memory"<<endl;
		cout<<"3. Exit Program"<<endl;
	    
		cin>>choice;
		


	if(choice == 1)
	{
			cout<<"Enter In The Amount Of Memory You Would Like To Allocate\n"<<endl;

			cin>>memory;

			while((memory <= 0) || (memory > 256))
					{
							cout<<"Oops, Please Enter In An Amount Between '2 - 256' \n"<<endl;

							cin>>memory;

					}//End Of While
		
			
			start_program.check_free_memory(memory);
			
				
	}
	
	
		/*else if(choice==2)
		{
			cout<<"How Much Memory Would You Like To Delete"<<endl;
			cin>>memory;

				while((memory <= 0) || (memory > 256))
				{
					cout<<"Oops, Please Enter In An Amount Between '2 - 256' \n"<<endl;
					cin>>memory;
				}

				allocatedList.deleteMemory(memory);


		}*/
	
	}while(choice != 3);


	return 0;
}//End Of Main

Open in new window

0
Comment
Question by:brich744
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 

Expert Comment

by:dspkid
ID: 35170373
You need to create an instance for a point (vector<Object> *node_linked_list) before use it.

ListNode(Object nodeValue)
{
      value = nodeValue;
      next = NULL;
      node_linked_list = new vector<Object>; // maybe you need to add this line
}


ListNode(Object nodeValue, Object startValue, Object endValue)
{
      value = nodeValue;
      next = NULL;
      start_blockValue=startValue;
      end_blockValue=endValue;
      node_linked_list = new vector<Object>;  // maybe you need to add this line
      node_linked_list->resize(4);
}
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35170763
You can combine the new vector creation, and its sizing in one step.
For example:
   ListNode(Object nodeValue, Object startValue, Object endValue)
      : value(nodeValue), next(NULL),
      start_blockValue( startValue ), end_blockValue( endValue ), 
      node_linked_list( new vector<Object>(4) )
   {
   }

Open in new window

Now, node_linked_list is a vector having 4 elements, and each element is initialized by the Object's default constructor. I prefer using an initialization list in constructors wherever possible (and it is required for const members and base class initialization of the base class private members).

In general, you should intialize every member of your class during construction (even useful for debugging with known initial values). So, maybe you will find the following to be useful:
   ListNode(Object nodeValue)
      : value(nodeValue), next(NULL),
      start_blockValue( Object() ), end_blockValue( Object() ), 
      node_linked_list( new vector<Object> )
   {
   }

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35171122
Looks like the change to the ListNode(Object nodeValue) constructor also needs to be changed as dspkid mentioned; otherwise, this has a problem:
   buddy:     allocated_list.show_List("Allocated List");

When I applied the initialzer list to it, then no problem gettting past that line.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:brich744
ID: 35172505
Just a quick side note in show_list I am outputting the vector from list_node with the overloaded operator <<.  But, when I output the data the memory location outputs. But I have tried to take the derefence outbut it is just accessing another memory location.  
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include<iostream>
#include<vector>
#include<string>

using namespace std;

template<class Object> class ListNode;
template<class Object>
ostream &operator<<(ostream& out, const vector<Object> &node_linked_list);



template<class Object>
class ListNode
{
	friend ostream &operator<<(ostream& out, const vector<Object> &node_linked_list);

public:
	
	 ListNode(Object nodeValue)
      : value(nodeValue), next(NULL),
      start_blockValue( Object() ), end_blockValue( Object() ), 
      node_linked_list( new vector<Object> )
		{}


	 ListNode(Object nodeValue, Object startValue, Object endValue)
      : value(nodeValue), next(NULL),
      start_blockValue( startValue ), end_blockValue( endValue ), 
      node_linked_list( new vector<Object> )
		{}
	

	void set_rangeNumber();
	Object get_rangeNumber();

	vector<Object> *node_linked_list;

	Object value;
	ListNode<Object> *next;
	
private:
	 Object start_blockValue;
	 Object end_blockValue;

};

template<class Object>
ostream &operator<<(ostream& out, const vector<Object> &node_linked_list)
{
	for(int i =0; i != node_linked_list.size(); i++)
		out<<node_linked_list[i];
	return out;
}


template<class Object>
void ListNode<Object>::set_rangeNumber()
{
	start_blockValue += 32;
}

template<class Object>
Object ListNode<Object>::get_rangeNumber()
{
	return start_blockValue;
}

//End of ListNode--------------------------------------------------------

template<class Object>
class LinkedList
{

private:
	ListNode<Object> *head;
	 int total_free_memory;
	 int total_used_memory;

public:
	LinkedList()
	{
		head = NULL;
		total_free_memory = 0;
		total_used_memory = 0;
	}

	~LinkedList();
	void appendNode(Object);
	void appendNode(Object, int, int);

	void insertNode(Object);
	void show_List(string);
	void find_value(Object);
	void deleteNode(Object);
	
	void add_free_memory(Object);
	void add_used_memory(Object);
	void set_memory(Object);

	Object getTotal_free_memory();
	Object getTotal_used_memory();

};

template<class Object>
void LinkedList<Object>::add_free_memory(Object value)
{
	free_memory += value;
	used_memory -= value;
}

template<class Object>
void LinkedList<Object>::add_used_memory(Object value)
{
	used_memory += value;
	free_memory -= value;
}

template<class Object>
void LinkedList<Object>::set_memory(Object memory)
{
	total_free_memory = memory;

	ListNode<Object> *nodePtr;

	nodePtr = head;

	while(nodePtr!= NULL)
	{
		if(nodePtr->value == memory)
		{
			nodePtr->set_rangeNumber();
			nodePtr->node_linked_list->push_back(nodePtr->get_rangeNumber());
			break;
		}
	}

	
}

template<class Object>
Object LinkedList<Object>::getTotal_free_memory()
{
	return total_free_memory;
}

template<class Object>
Object LinkedList<Object>::getTotal_used_memory()
{
	return used_memory;
}


template<class Object>
void LinkedList<Object>::appendNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr;

	newNode = new ListNode<Object>(newValue);

	if(head == NULL)
		head = newNode;

	else
	{
		nodePtr = head;

		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::appendNode(Object newValue, int startValue, int endValue)
{
	ListNode<Object> *newNode, *nodePtr;
	
	//Allocate a new node and store the value
	newNode = new ListNode<Object>(newValue, startValue, endValue);

	//If there are no nodes in the list
	//make newNode the first node
	if(head == NULL)
		head = newNode;
	//Insert the node at the end of the list
	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Find the last node in the list
		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::show_List(string name)
{
	cout<<"*****"<<name<<"*****"<<endl;

	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		cout<<nodePtr->value<<": ";

			if(!nodePtr->node_linked_list->empty())
			{cout<<nodePtr->node_linked_list;}

		nodePtr = nodePtr->next;
		cout<<endl;
	}

	cout<<""<<endl;
}

template<class Object>
void LinkedList<Object>::find_value(Object memory)
{
	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		if(nodePtr->value>= memory && (nodePtr->next->value<memory || nodePtr->next->value == NULL ))
		{	
			nodePtr->set_rangeNumber();
			nodePtr->node_linked_list->push_back(nodePtr->get_rangeNumber());
			break;
		}		

	}

}

template<class Object>
void LinkedList<Object>::insertNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr, *previousNode = NULL;

	//Allocate a new node and store newValue
	newNode = new ListNode<Object>(newValue);

	//If there are no nodes in the  list
	//make newNode the first node

	if(head == NULL)
	{
		head = newNode;
		newNode->next = NULL;
	}

	else
	{
		//Initialize nodePtr to head of list and previousNode to NULL
		nodePtr = head;
		previousNode = NULL;

		//Skip all nodes whose value member is less than newValue
		while(nodePtr != NULL && nodePtr->value < newValue)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		//If the new node is the first in the list
		if(previousNode == NULL)
		{
			head = newNode;
			newNode->next=nodePtr;
		}

		else
		{
			previousNode->next=newNode;
			newNode->next=nodePtr;
		}
	}
}

template<class Object>
void LinkedList<Object>::deleteNode(Object searchValue)
{
	ListNode<Object> *nodePtr, *previousPtr;

	//If the list is empty do nothing
	if(head == NULL)
		return;

	//Determine if the first node is the value that we are looking for
	if(head->value == searchValue)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}

	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Skip all nodes whose value member is not equal to search value
		while(nodePtr != NULL && nodePtr->value != searchValue)
		{
			previousNode=nodePtr;
			nodePtr = nodePtr->next;
		}

		//If nodePtr is @ the end of the list
		//than link the previous node to the node after nodePtr
		if(nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

template<class Object>
LinkedList<Object>::~LinkedList()
{
	ListNode<Object> *nodePtr, *nextNode;

	nodePtr = head;
	while(nodePtr != NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;

	}
}

#endif

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35173142
The output of your program is
***********BUDDY MEMORY PROGRAM**********

*****Free List*****
256: 00346358
128:
64:
32:
16:
8:
4:
2:

*****Allocated List*****
256:
128:
64:
32:
16:
8:
4:
2:

Open in new window


Here is what the debug window shows for the freelist (with some of the next pointers expanded):
 freelist with some next pointer expandedCould you rewrite the above output to show what it should be?
0
 

Author Comment

by:brich744
ID: 35173831
The output should show the value that is being held within the address. For this example it should start out showing 32 instead of 00346358.

***********BUDDY MEMORY PROGRAM**********

*****Free List*****
256: 32
128:
64:
32:
16:
8:
4:
2:

*****Allocated List*****
256:
128:
64:
32:
16:
8:
4:
2:
0
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 35173867
Just to give you a 32, I dererenced the pointer and hard-coded for the first element:
if(!nodePtr->node_linked_list->empty())
      {
       cout<< (*nodePtr->node_linked_list)[0];
      }

Open in new window

0
 

Author Comment

by:brich744
ID: 35173886
Thanks alot, quick question I want to get better at programming and I am practicing as much as possible.  And, also I have picked up Scott Meyer 55 ways to improve your programming.(Great Book) Would you recommend me doing anything else to help better myself?
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35173937
>> Would you recommend me doing anything else to help better myself?
Obviously, what you are doing is proving effective - practicing and asking questions.

At work, a lead engineer read many books including Scott Meyer's books. Nevertheless, his UML (and resulting implementation) had to be totally revamped in some areas resulting in 20 engineers redoing their implementations. He had trouble accepting criticism and just kept producing design without a true consensus. I can assure you that he has improved from his mistakes. (Luckily, I was working 3 months on a requirement/design document), so I didn't suffer too much.)

So, a mistake isn't a bug or a design flaw in this context. It's making use of the practical applications of the language and tools available so that a team can work effectively together.

About tools - we used IBM's Rhapsody in C++ to draw diagrams and generate framework code from them.

If you can wipe your slate clean during Rhapsody's trial period, it would be valuable to learn how to use Rhapsody. The trial is for at least one month, maybe two. Try to find their tutorials and sample projects. When you generate code, look carefully at what it looks like. It is correct, but takes awhile to get used to their paradigm. It is necessary to develop this skill when trying to locate a bug. Run their state animations to see how your code works under real-time (but in slow simulation) environment.

Being on a team project at work is IMO the best way to learn practical application of C++. If not working, then join an open source project of your choice. Or create one from scratch.

I enjoy reading The C++ Programming Language: Special Edition by Bjarne Stroustrup (creator of C++)
 http://www.amazon.com/C-Programming-Language-Special/dp/0201700735/ref=sr_1_1?ie=UTF8&qid=1300587936&sr=8-1
which is a big step easier than reading the C++ Standard, which is painful to read. I'm sure you already know about the other online resources in C++ like Thinking In C++ (two volumes).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question