473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

The program is receiving SIGABRT signal.

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
2 6270
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Joseph Suprenant | last post by:
Hello all, I have a C++ program, it does some calculations on things and then prints out a file in the format in which GNUPLOT can use. So my question is how would i call GNUPLOT from my C++...
0
by: szehau | last post by:
Hi all, I have a program written in C with embeded SQL. Following are the configuration: DB2/LINUX 8.1.5 Thread model: posix gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) My problems...
3
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/...
21
by: kimimaro | last post by:
Is there anymore methods in exiting your program using pure C language other than return 0?
26
by: steve | last post by:
Well I've been working all morning and have finally found the source of my "bus error (signal 10)" errors. The source is odd. The error occurs in any function where I make the function call: ...
3
by: wongjoekmeu | last post by:
Hiya all, I have a strange problem. I have compiled a program with g++ compiler. It seems to compile fine. I can even compile it with debug (-g) option. However at the very beginning when I run...
10
by: wong_powah | last post by:
I want to find out where (which line) my C program core dump. How to do that? Is there a web site describing the procedure? One approach is to use stack trace of the mdb debugger, but I does not...
2
by: wnbill | last post by:
hie I am running a batch developed in C on unix and its core-dumping. Upon opening the core with gdb its giving me Program terminated with signal 10, Bus Error. ...
0
by: Marcin Krol | last post by:
Hello everyone, And now for something completely different: signal handling sometimes works, sometimes it doesn't. When I embed following code, it works: count = 0
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.