| | |
Help, Assertion Problem :((
![]() |
•
•
Join Date: Apr 2008
Posts: 19
Reputation:
Solved Threads: 0
i came into Assertion failure during running this program, and i really don't know why, hope you guys can help, thanks a lot ^^
The program is to copy a linked list from another, the Copy function may look complicated because i'm not experienced enough to simplify it.. Without the destructor, the program runs fine..
The program is to copy a linked list from another, the Copy function may look complicated because i'm not experienced enough to simplify it.. Without the destructor, the program runs fine..
#include <iostream.h>
struct node
{
int data ;
node * next ;
};
class list
{
private:
node *head ;
public:
list() ;
//~list() ;
node* Add(node *h , int d) ;
node* Copy(node *& res , node * copy) ;
void Input() ;
void Display() ;
node* GetHead() ;
void AssignHead(node *h) ;
};
list::list()
{
head = NULL ;
}
/*list::~list()
{
while(head)
{
node *current = head->next ;
delete head ;
head = current ;
}
}*/
node * list::GetHead()
{
return head ;
}
void list::AssignHead(node *h)
{
head = h ;
}
node * list::Add(node *h , int d)
{
if ( !h )
{
h = new node ;
h->data = d ;
h->next = NULL ;
}
else
h->next = Add(h->next , d) ;
return h ;
}
void list::Input()
{
int n ;
int d ;
cout << "How many intergers to input: " ;
cin >> n ;
for ( int i = 0 ; i < n ; ++i )
{
cout << "Interger #" << i+1 << ", please enter: " ;
cin >> d ;
head = Add(head,d) ;
}
}
void list::Display()
{
node * current = head ;
while (current != NULL)
{
cout << current->data << " " ;
current = current->next ;
}
}
node * list::Copy(node *& res , node * copy)
{
if (copy)
{
res = copy ;
Copy(res->next,copy->next) ;
}
return res ;
}
int main()
{
list integer , a ;
integer.Input() ;
integer.Display() ;
cout << endl ;
//a.Input() ;
//a.Display() ;
node *res = NULL ;
a.AssignHead(a.Copy(res , integer.GetHead())) ;
a.Display() ;
return 0 ;
}•
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
while(head) - why???
you need only:
you need only:
CPP Syntax (Toggle Plain Text)
list::~list() { node* current = head->next; delete head; head = current; }
The problem, while exhibiting itself when the destructor is called, is not actually in the destructor. Your Copy function makes a shallow copy - in your code object "a" points to the same memory as object "integer". When the first of those gets destroyed at program exit, the pointed to memory of head gets deleted. When the second one is passed to the destructor, it head still has that memory address, but there is no "next" member. So an invalid address is assigned to current, and then to head, and that fails when you try to access the next member of that nonexistent object.
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Apr 2008
Posts: 19
Reputation:
Solved Threads: 0
oh i under stood, thanks a lot, i made 2 pointers pointing to the same object at the same time, so redeleting is for sure, thanks a lot again.. Need to fix the Copy function 
@ ivailosp: oh i want the destructor to delete every element inside the linked list, if i take ur code, it may delete only the beginning :-?

@ ivailosp: oh i want the destructor to delete every element inside the linked list, if i take ur code, it may delete only the beginning :-?
![]() |
Other Threads in the C++ Forum
- Previous Thread: delay() or sleep()
- Next Thread: Yes/No to 1/0 and if else statement
| Thread Tools | Search this Thread |
addition api array base based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embed encryption error erroraftercompilation excel file forms fstream function functions game getline givemetehcodez gmail graph gui homework homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference rpg std::coutwstring string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






