By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,443 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,443 IT Pros & Developers. It's quick & easy.

The program is receiving SIGABRT signal.

P: n/a
Hi,
Can anybody please tell me that why the following code is getting the
SIGABRT signal when the statement "node = new Node();" in the
append function executes ?

#include <iostream>
#include <string>
#include <new>
using namespace std;

template <class T>
class LinkedList
{
public:
void display();
bool isempty();
void append();
// void prepend();
//void remove();

~LinkedList();
LinkedList();

private:
// template <class T>
class Node
{

public:
Node():data(0),next(0)
{
}

T data;
Node *next;
};

Node * head, *tail;
};

template <class T>
void LinkedList<T> :: display()
{
if(isempty() == true)
{
cout << "List is empty"<<endl;
return;
}

Node *tmp;
tmp = head;
while(tmp != NULL)
cout << tmp->data;
return ;
}
template <class T>
bool LinkedList<T> :: isempty()
{
return head == NULL;
}

template <class T>
void LinkedList<T> :: append()
{
Node * node;
node = new Node(); // The ERROR is in this line.
T data;
cout <<"Enter the data to be entered"<<endl;
cin >>data;
node->data = data;
node->next = NULL;
if(head == NULL) // list was empty.
{
head = tail = node;
return ;
}

else // list was NOT empty.
{
tail->next = node;
tail = tail->next;
}

return ;
}

template <class T>
LinkedList<T>::LinkedList(): head(0), tail(0)
{
cout << "LinkedList created!"<<endl;
}

template <class T>
LinkedList<T>:: ~LinkedList()
{
cout << "~LinkedList() is useless"<<endl;
}

void low_on_memory()
{
cout<<"System low on memory."<<endl;
return;
}
int main(void)
{
set_new_handler(low_on_memory);
int choice;
LinkedList<string> list;
while(true)
{
cout<<"1: Display\n2:Append\n3: Exit\n ";
cin>>choice;

switch(choice)
{
case 1:
list.display();
break;

case 2:
list.append();
break;

case 3:
exit(1);
break;

default:
cout<<"Invalid Choice\n";
break;

}
}

return 0;
}

__________________________________________________ ________________________

Also, I was just wondering that is there is any use of virtual
function/s in a project being made by a single person and which(project
) will not be used again, not even for adding some more features in it
or to improve the efficiency of the project? The reason behind this
question is that I've heard that virtual function/s are used to replace
the old code by a new one, so if the project will never be used again
then what's the use of using virtual functions ?

Thanks

Nov 4 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You can not initialize string with 0-pointer. I suggest leaving out
data(0) completely from Node constructor since you never know what the
template argument might be.

For the second part: virtual functions are not created to do team work,
but to separate implementation details from conceptually clean objects.
It is useful no matter if you work alone or in a team. I am not sure
I make it sound simple though.

G.

ja**********@yahoo.com wrote:
Hi,
Can anybody please tell me that why the following code is getting the
SIGABRT signal when the statement "node = new Node();" in the
append function executes ?

#include <iostream>
#include <string>
#include <new>
using namespace std;

template <class T>
class LinkedList
{
public:
void display();
bool isempty();
void append();
// void prepend();
//void remove();

~LinkedList();
LinkedList();

private:
// template <class T>
class Node
{

public:
Node():data(0),next(0)
{
}

T data;
Node *next;
};

Node * head, *tail;
};

template <class T>
void LinkedList<T> :: display()
{
if(isempty() == true)
{
cout << "List is empty"<<endl;
return;
}

Node *tmp;
tmp = head;
while(tmp != NULL)
cout << tmp->data;
return ;
}
template <class T>
bool LinkedList<T> :: isempty()
{
return head == NULL;
}

template <class T>
void LinkedList<T> :: append()
{
Node * node;
node = new Node(); // The ERROR is in this line.
T data;
cout <<"Enter the data to be entered"<<endl;
cin >>data;
node->data = data;
node->next = NULL;
if(head == NULL) // list was empty.
{
head = tail = node;
return ;
}

else // list was NOT empty.
{
tail->next = node;
tail = tail->next;
}

return ;
}

template <class T>
LinkedList<T>::LinkedList(): head(0), tail(0)
{
cout << "LinkedList created!"<<endl;
}

template <class T>
LinkedList<T>:: ~LinkedList()
{
cout << "~LinkedList() is useless"<<endl;
}

void low_on_memory()
{
cout<<"System low on memory."<<endl;
return;
}
int main(void)
{
set_new_handler(low_on_memory);
int choice;
LinkedList<string> list;
while(true)
{
cout<<"1: Display\n2:Append\n3: Exit\n ";
cin>>choice;

switch(choice)
{
case 1:
list.display();
break;

case 2:
list.append();
break;

case 3:
exit(1);
break;

default:
cout<<"Invalid Choice\n";
break;

}
}

return 0;
}

__________________________________________________ ________________________

Also, I was just wondering that is there is any use of virtual
function/s in a project being made by a single person and which(project
) will not be used again, not even for adding some more features in it
or to improve the efficiency of the project? The reason behind this
question is that I've heard that virtual function/s are used to replace
the old code by a new one, so if the project will never be used again
then what's the use of using virtual functions ?

Thanks


Nov 4 '05 #2

P: n/a

ja**********@yahoo.com wrote:
Hi,
Can anybody please tell me that why the following code is getting the
SIGABRT signal when the statement "node = new Node();" in the
append function executes ?

#include <iostream>
#include <string>
#include <new>
using namespace std;

template <class T>
class LinkedList
{
public:
void display();
bool isempty();
void append();
// void prepend();
//void remove();

~LinkedList();
LinkedList();

private:
// template <class T>
class Node
{

public:
Node():data(0),next(0)
Well, ignoring the other bugs, your problem lies here. You are
initializing a std::string with 0. To make it work, change that line:

Node() : next(0)
{
}

T data;
Node *next;
};

Node * head, *tail;
};

template <class T>
void LinkedList<T> :: display()
{
if(isempty() == true)
{
cout << "List is empty"<<endl;
return;
}

Node *tmp;
tmp = head;
while(tmp != NULL)
cout << tmp->data;
return ;
}
template <class T>
bool LinkedList<T> :: isempty()
{
return head == NULL;
}

template <class T>
void LinkedList<T> :: append()
{
Node * node;
node = new Node(); // The ERROR is in this line.
T data;
cout <<"Enter the data to be entered"<<endl;
cin >>data;
node->data = data;
node->next = NULL;
if(head == NULL) // list was empty.
{
head = tail = node;
return ;
}

else // list was NOT empty.
{
tail->next = node;
tail = tail->next;
}

return ;
}

template <class T>
LinkedList<T>::LinkedList(): head(0), tail(0)
{
cout << "LinkedList created!"<<endl;
}

template <class T>
LinkedList<T>:: ~LinkedList()
{
cout << "~LinkedList() is useless"<<endl;
}

void low_on_memory()
{
cout<<"System low on memory."<<endl;
return;
}
int main(void)
{
set_new_handler(low_on_memory);
int choice;
LinkedList<string> list;
while(true)
{
cout<<"1: Display\n2:Append\n3: Exit\n ";
cin>>choice;

switch(choice)
{
case 1:
list.display();
break;

case 2:
list.append();
break;

case 3:
exit(1);
break;

default:
cout<<"Invalid Choice\n";
break;

}
}

return 0;
}

__________________________________________________ ________________________

Also, I was just wondering that is there is any use of virtual
function/s in a project being made by a single person and which(project
) will not be used again, not even for adding some more features in it
or to improve the efficiency of the project? The reason behind this
question is that I've heard that virtual function/s are used to replace
the old code by a new one, so if the project will never be used again
then what's the use of using virtual functions ?

Thanks


Ease of maintenance is only one reason to use virtual functions.
Virtual functions are a general technique for implementing inheritance
hierarchies that can make programs easier to build, debug, and
understand. For instance, you can use virtual functions to treat a
bunch of different objects as an abstract class. The classic example
is:

struct Shape { virtual void Draw() = 0; };
struct Circle : Shape { void Draw() { /* Draw a circle here */ } };
struct Triangle : Shape { void Draw() { /* Draw a triangle here */ }
};
struct Square : Shape { void Draw() { /* Draw a square here */ } };

// Create a bunch of shapes
std::vector<Shape*> shapes;
shapes.push_back( new Circle );
shapes.push_back( new Square );
shapes.push_back( new Triangle );
shapes.push_back( new Square );

Now we can draw all the elements in our list without caring what types
they really are:

for( unsigned n = 0; n < shapes.size(); n++ )
{
shapes[n] -> Draw(); // Will draw circles, squares, and triangles
}

Check out the sections of the FAQ on inheritance and virtual functions:

http://www.parashift.com/c++-faq-lite/

A good book for learning such things is _Acclerated C++_ by Koenig and
Moo.

Cheers! --M

Nov 4 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.