Article View: gnu.g++.bug
Article #166Re: garbett@utknp3.phys.utk.edu: segmentation fault
From: Gnanasekaran Swaminathan
Date: Tue, 04 Aug 1992 14:19
Date: Tue, 04 Aug 1992 14:19
61 lines
1823 bytes
1823 bytes
garbett@utknp3.phys.utk.edu (Garbett) wants to know why he
is getting segmentation fault in the following code:
:#include <iostream.h>
:
:class list
:{
: private:
: int num;
: list *next;
: public:
: // Constructors
: inline list() { next = NULL; num = 0; }
: inline list(int the_num) { next = NULL; num = the_num; }
: inline list(list *next_node) { next = next_node; num = 0; }
: list(int the_num, list *next_node)
: { next = next_node, num = the_num; }
:
: // Normal Usage
: inline void display_value() { cout << num; }
: inline list *get_next() { return next; }
:
: // Recursive function and culprit of core dump
: void print_all() ;
:};
:
:void list::print_all()
:{
: cout << num << "\n";
:
: if( next != NULL)
: next->print_all();
:}
:
:int main()
:{
: int i;
: list *top, *curr;
:
: for(i=0, top = NULL; i<10; i++)
: top = new list(i, top);
:
: curr = top;
:
: /* This works */
: do
: {
: curr->display_value();
: cout << "\n";
: curr = curr->get_next();
: } while (curr != NULL);
:
: cout << "\n" << "Testing Recursive Function\n";
:
: /* Get ready for a segmenatation fault */
: curr->print_all();
curr is 0 when you are call print_all(). Hence, you are getting
the segmentation fault.
:}
Technical Details
Message-ID:
<1992Aug4.141933.3245@murdoch.acc.Virginia.EDU>Path:
arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!virginia.edu!gs4tReferences:
<9208031445.AA05419@utknp3.phys.utk.edu>Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!virginia.edu!gs4t From: gs4t@virginia.edu (Gnanasekaran Swaminathan) Subject: Re: garbett@utknp3.phys.utk.edu: segmentation fault Message-ID: <1992Aug4.141933.3245@murdoch.acc.Virginia.EDU> Sender: gnulists@ai.mit.edu Reply-To: gs4t@virginia.edu (Gnanasekaran Swaminathan) Organization: University of Virginia References: <9208031445.AA05419@utknp3.phys.utk.edu> Distribution: gnu Date: Tue, 4 Aug 1992 14:19:33 GMT Approved: bug-g++@prep.ai.mit.edu Lines: 62