The program is receiving SIGABRT signal.

J

jammie_linux

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
 
G

gpriv

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.
 
M

mlimber

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top