Question:
c++ help with one compiler error in Sorted List program?
david
2010-11-23 17:21:21 UTC
Implement the template class SortedList. Write a simple driver that reads values from file float.dat, inserts them into the list, prints the length of the final list, and prints the items. Add code to your driver to test the remaining member functions. Delete 3.3, 10.0, and 200.0 and print the list to be sure they are gone.

My program was able to run with the correct output but a small white screen keeps poping up saying "Debug Assertion Failed". When I clicked RETRY, my compiler then tells me that "cis200prog3part2.exe has triggered a breakpoint". The breakpoint is pointing toward the last line in my SortedType destructor function which is " delete tempPtr; ".Basically there is a problem with my SortedType destructor function which I am not sure how to fix. Can anyone help me fix this? Thanks.

CODE:
#include
#include
using namespace std;
typedef float ItemType;
struct NodeType;
typedef NodeType * NodePtr;
class SortedType
{
public:
SortedType(); // Class constructor
~SortedType(); // Class destructor
bool IsThere(ItemType item) const;
int GetLength() const;
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
void Print();
int Length(){return length;};

private:
NodeType * listData;
int length;
NodeType * currentPos;
};

struct NodeType
{
ItemType info;
NodeType* next;
};


SortedType::SortedType() // Class constructor
{
length = 0;
listData = NULL;
}
bool SortedType::IsThere(ItemType item) const
{
NodeType *location = listData;

while(listData!=NULL)
{
if(location->info = item)
{
return true;
}
else
{
location = location->next;
}
}
return false;
}
int SortedType::GetLength() const
{
return length;
}

void SortedType::RetrieveItem(ItemType& item, bool& found)
{
bool moreToSearch;
NodeType* location;

location = listData;
found = false;
moreToSearch = (location != NULL);

while (moreToSearch && !found)
{

if (location->info > item)
{
location = location->next;
moreToSearch = (location != NULL);
}

else if (location->info < item)
{
found = true;
item = location->info;
}

else
moreToSearch = false;

}
}
void SortedType::InsertItem(ItemType item)
{
NodeType* newNode;
NodeType* predLoc;
NodeType* location;
bool moreToSearch;
location = listData;
predLoc = NULL;
moreToSearch = false;

while (location != NULL && !moreToSearch)
{
if (location->info{
predLoc = location;
location = location->next;
}
else
moreToSearch = true;
}

newNode = new NodeType;
newNode->info = item;
if (predLoc == NULL)
{
newNode->next = listData;
listData = newNode;
}
else
{
newNode->next = location;
predLoc->next = newNode;
}
length++;
}


void SortedType::DeleteItem(ItemType item)
{
NodeType* tempLocation ;
NodeType* location = listData ;
if ( item == location->info)
{ tempLocation = location;
listData= listData->next ;
}
else {
while ( location->next->info != item )
location = location->next ;
tempLocation = location->next ;
location->next = location->next->next ;
}
delete tempLocation ;
length--;
}
void SortedType::ResetList()
{
currentPos = NULL;
}
void SortedType::Print()
{
NodeType * temp = listData;
while(temp != NULL)
{
cout << temp->info << endl;
temp = temp->next;
}
}



SortedType::~SortedType() // Class destructor
{
NodeType* tempPtr;

while (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
}
int main()
{
float num;
SortedType intList;

ifstream inFile;
inFile.open("float1.txt");
while(inFile >> num)
{
intList.InsertItem(num);
}
int a = intList.GetLength();
cout << "The List contains " << a << " items:" << endl;
intList.Print();
intList.ResetList();
intList.DeleteItem(3.3);
intList.DeleteItem(10.0);
intList.DeleteItem(2.0);
SortedType a2(intList);
intList.Print();


}
Three answers:
Ratchetr
2010-11-23 18:30:03 UTC
Nasty, nasty C++'ism.



Things start to go wrong here:

SortedType a2(intList);



SortedType doesn't have a constructor that takes a SortedType as a parameter (copy constructor). So the compiler generates one for you. It does a shallow copy of everything in intList into a2.



So now listData in intList and a2 both point to the same list. The pointer was copied. The list wasn't.



Then, before you can exit main, C++ is going to call the destructor for intList and a2. They are local class variables declared on the stack, so the destructor needs to be run to unwind the function.



So: SortedType::~SortedType() gets called twice. Once for intList, again for a2. And in both cases listData points to the same linked list.



The first call frees all the nodes in the linked list. In a Debug build with Microsoft VS, not only does it free the memory, it fills the memory, with a value of 0xfeeefeee.



Then the C'tor gets called a second time. But now the next pointer in your linked list nodes contain 0xfeeefeee. And that's where you fault. feeefeee isn't a valid pointer.



If you are going to use a copy constructor, and your class contains pointer variables, you need to implement the copy C'Tor yourself. The one the compiler generates isn't going to work.



Thanks for reminding me why I love C#. This crud doesn't happen ;-)



PS:

DeleteItem fails miserably if the item isn't in the list. You should guard against that.
The Phlebob
2010-11-23 17:29:45 UTC
About the only two things that can go wrong with a delete of a pointer are that the pointer is NULL (which you're preventing with your code) or that it doesn't point to a valid block of memory. That could happen, even with valid data, if the memory locations immediately prior to the block, which often contain header information about the block, are wiped out by something.



Try printing the pointer and see if its value gives you a clue.



Good luck. Pointer problems can be ghastly to debug.
?
2016-10-17 15:58:13 UTC
If it makes you sense to any extent further useful approximately it, I copied and pasted your code over to my Solaris gadget and compiled it with G++. i did no longer attempt working it, in spite of the fact that it quite did no longer supply me any compiler messages. possibly you could see in case you could receive a abode windows version of the Gnu G++ compiler, and use that rather of Microsoft? Microsoft isn't particularly properly-conventional for sticking to any standards they hadn't invented, which anybody knows motives each and every style of heartburn (at the same time alongside with your potential business company, i'm going to wager). good luck! EDIT: in uncomplicated terms for kicks, i attempted working it. properly, the report it needed to study would not exist, of direction, the Solaris platform no longer even having a "C:" power -- so it revealed, "It does no longer open!", then center-dumped with a Segmentation Fault. i do no longer know regardless of if it is significant to you, in spite of the fact that that's possibly no longer meant to center-sell off.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...