Gast
2006-10-19, 16:32:15
Hi,
hier einmal wichtige Funktion derTextList Klasse:
TextList::TextList(void)
{
this->runner=new LineNode();
this->footer=new LineNode();
this->header=new LineNode();
this->header->appendNextNode(footer);
this->numofLines=0;
}
TextList::~TextList(void)
{
system("pause");
this->runner=header->getNext();
LineNode *tmp;
int num=0;
while(num!=this->numberofLines())
{
std::cout<<num<<std::endl;
system("pause");
tmp=this->runner->getNext();
delete runner;
this->runner=tmp;
num++;
}
if(this->footer!='\0')
{
delete this->footer;
}
if(this->header!='\0')
{
delete this->header;
}
if(tmp!='\0')
{
delete tmp;
}
}
void TextList::appendLine(String s)
{
LineNode *foo;
if(s.getLength()<=255)
{
this->setRunner(this->numberofLines());
foo=new LineNode(&s);
runner->appendNextNode(foo);
// foo->printLine();
foo->appendNextNode(footer);
this->numofLines++;
}
}
void TextList::setRunner(int i)
{
this->runner=this->header->getNext();
int run=0;
while(this->runner!=this->footer&&run!=i)
{
run++;
this->runner=this->runner->getNext();
}
}
LineNode:
LineNode::LineNode(void)
{
this->data='\0';
this->next='\0';
}
LineNode::~LineNode(void)
{ // wird solange aufgerufen bis ein Stackoverflow auftritt
if(this->next!='\0')
{
delete next;
}
if(this->data!='\0')
{
delete this->data;
}
}
LineNode::LineNode(String *dat)
{
this->data=dat;
this->data->print();
this->next='\0';
}
LineNode::LineNode(const LineNode &data)
{
std::cout<<"Copy constructor"<<std::endl;
}
LineNode * LineNode::getNext()
{
return this->next;
}
void LineNode::appendNextNode(LineNode *node)
{ this->next=node;
}
void LineNode::printLine()
{
this->data->print();
}
void LineNode::appendNextNode(LineNode *node)
{ this->next=node;
}
String Class
String::String(void)
{
this->data='\0';
}
String::~String(void)
{
system("pause");
std::cout<<"Gehe in den String Destructor"<<std::endl;
if(this->data!='\0')
{
delete [] this->data;
}
}
String::String(char *content)
{
int count=0;
while(content[count]!='\0')
{
count++;
}
count++;
this->data=new char[count];
memcpy(data,content,count);
}
String::String(const String & s)
{
int sum=0;
while(s.data[sum]!='\0')
{
sum++;
}
this->data=new char[sum];
memcpy(this->data,s.data,sum);
std::cout<<"Leaving Copy Constructir"<<std::endl;
//s=new String(s.data);
}
int String::getLength()
{
int sum=0;
while(this->data[sum]!='\0')
{
sum++;
}
return sum;
}
void String::print()
{
std::cout<<this->data<<std::endl;
}
Geteste wurde der Code mit folgenden Aufrufen:
TextList *s=new TextList();
s->appendLine("HALLO");
s->appendLine("WELT");
system("pause");
delete s; // Hier beginnt das Übel^^
Wie kann es überhaupt zu sowas kommen?
hier einmal wichtige Funktion derTextList Klasse:
TextList::TextList(void)
{
this->runner=new LineNode();
this->footer=new LineNode();
this->header=new LineNode();
this->header->appendNextNode(footer);
this->numofLines=0;
}
TextList::~TextList(void)
{
system("pause");
this->runner=header->getNext();
LineNode *tmp;
int num=0;
while(num!=this->numberofLines())
{
std::cout<<num<<std::endl;
system("pause");
tmp=this->runner->getNext();
delete runner;
this->runner=tmp;
num++;
}
if(this->footer!='\0')
{
delete this->footer;
}
if(this->header!='\0')
{
delete this->header;
}
if(tmp!='\0')
{
delete tmp;
}
}
void TextList::appendLine(String s)
{
LineNode *foo;
if(s.getLength()<=255)
{
this->setRunner(this->numberofLines());
foo=new LineNode(&s);
runner->appendNextNode(foo);
// foo->printLine();
foo->appendNextNode(footer);
this->numofLines++;
}
}
void TextList::setRunner(int i)
{
this->runner=this->header->getNext();
int run=0;
while(this->runner!=this->footer&&run!=i)
{
run++;
this->runner=this->runner->getNext();
}
}
LineNode:
LineNode::LineNode(void)
{
this->data='\0';
this->next='\0';
}
LineNode::~LineNode(void)
{ // wird solange aufgerufen bis ein Stackoverflow auftritt
if(this->next!='\0')
{
delete next;
}
if(this->data!='\0')
{
delete this->data;
}
}
LineNode::LineNode(String *dat)
{
this->data=dat;
this->data->print();
this->next='\0';
}
LineNode::LineNode(const LineNode &data)
{
std::cout<<"Copy constructor"<<std::endl;
}
LineNode * LineNode::getNext()
{
return this->next;
}
void LineNode::appendNextNode(LineNode *node)
{ this->next=node;
}
void LineNode::printLine()
{
this->data->print();
}
void LineNode::appendNextNode(LineNode *node)
{ this->next=node;
}
String Class
String::String(void)
{
this->data='\0';
}
String::~String(void)
{
system("pause");
std::cout<<"Gehe in den String Destructor"<<std::endl;
if(this->data!='\0')
{
delete [] this->data;
}
}
String::String(char *content)
{
int count=0;
while(content[count]!='\0')
{
count++;
}
count++;
this->data=new char[count];
memcpy(data,content,count);
}
String::String(const String & s)
{
int sum=0;
while(s.data[sum]!='\0')
{
sum++;
}
this->data=new char[sum];
memcpy(this->data,s.data,sum);
std::cout<<"Leaving Copy Constructir"<<std::endl;
//s=new String(s.data);
}
int String::getLength()
{
int sum=0;
while(this->data[sum]!='\0')
{
sum++;
}
return sum;
}
void String::print()
{
std::cout<<this->data<<std::endl;
}
Geteste wurde der Code mit folgenden Aufrufen:
TextList *s=new TextList();
s->appendLine("HALLO");
s->appendLine("WELT");
system("pause");
delete s; // Hier beginnt das Übel^^
Wie kann es überhaupt zu sowas kommen?