Hey Everyone,

I am trying to compile and run a program for my C++ class, and I am getting a Segmentation Fault as a runtime error. I looked at my code and my datafile to make sure that there was not an issue but I can not seem to find whats giving me the error. Below I have placed my code and the datafile I am using during runtime. I have to use the terminal in Ubuntu to compile/link/run, and this is the way my professor wants me to write the code.

Thanks in advance for any help!

Source Code:

//Assignment 7 DNJ--> CSCI 208

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;

//****************************************************************************************
/* Global Variables */

const int NAMESIZE=25;
const int FILENAME=51;
const int MAXLISTSIZE=200;

typedef char Name_t[NAMESIZE];
typedef char Filename_t[FILENAME];
typedef int ID_t;
typedef double Bal_t;
typedef fstream Infile_t;
typedef fstream Outfile_t;
typedef int Addr_t;

const Addr_t NIL=-1;
const Addr_t END=-2;

//****************************************************************************************
/* Class Declerations */

class cAccount
{

	private:

		Name_t fname;
		Name_t lname;
		ID_t aID;
		Bal_t aBal;

	public:

		cAccount();
		void Read (Infile_t &Infile);
		void Print (Outfile_t &Outfile);
		void Key_Input();
		void write2data(Infile_t &Infile);
		void Display();		 
		void write2list(Infile_t &Infile);
		 int compID2ID(cAccount NewAccount);
		 int Search4ID(); 

};// end cAccount

//****************************************************************************************
/* Struct Declerations */

struct ListEle_t
{

	cAccount Account;
	Addr_t Link;

};// end ListEle_t

//****************************************************************************************
/* Array Decleration */

typedef ListEle_t List_t[MAXLISTSIZE];

//****************************************************************************************
/* Class Function Implementations */

cAccount::cAccount()
{

	strcpy(fname,"");
	strcpy(lname,"");
	aID=0;
	aBal=0;

}// end cAccount::cAccount

void cAccount::Read(Infile_t &Infile)
{

	Infile>>aID>>lname>>fname>>aBal;

}// end cAccount::Read

void cAccount::Print(Outfile_t &Outfile)
{

	Outfile<<setw(10)<<"ACCOUNT-ID: "<<aID<<endl;
	Outfile<<setw(10)<<"ACCOUNT NAME: "<<lname<<","<<fname<<endl;
	Outfile<<setw(10)<<"ACCOUNT BALANCE:  $"<<aBal<<endl;
	Outfile<<"------------------------------------------------"<<endl;

}// end cAccount::Print

void cAccount::Key_Input()
{

	cout<<"Begining KeyBoard Input.........."<<endl<<endl;
	cout<<"Enter 5 digit Account-ID #:"<<endl;
	cin>>aID;
	cout<<"Enter the first name:"<<endl;
	cin>>fname;
	cout<<"Enter the last name:"<<endl;
	cin>>lname;
	cout<<"Enter the Account Balance:"<<endl;
	cin>>aBal;
	cout<<endl<<endl;

}// end cAccount::Key_Input();

void cAccount::write2data(Infile_t &Infile)
{

	Key_Input();
	Infile<<fname<<lname<<aID<<aBal<<endl;

}// end cAccount::write2data

void cAccount::write2list(Infile_t &Infile)
{

	Infile<<fname<<lname<<aID<<aBal<<endl;

}// end cAccount::write2list

void cAccount::Display()
{

	cout<<setw(10)<<"ACCOUNT-ID: "<<aID<<endl;
	cout<<setw(10)<<"ACCOUNT NAME: "<<lname<<","<<fname<<endl;
	cout<<setw(10)<<"ACCOUNT BALANCE:  $"<<aBal<<endl;
	cout<<"------------------------------------------------"<<endl;

}// end cAccount::Display


int cAccount::compID2ID(cAccount NewAccount)
{

	int count;
	count=0;
	
	if(this->aID > NewAccount.aID)
	{
		count=-1;

	}// end if

	else if(this->aID < NewAccount.aID)
	{
		count=1;

	}// end else if

	else
	{
		count=0;
	
	}//end else

	return count;

}// end cAccount::compID2ID

int cAccount::Search4ID()
{

	int i=0;

	if(aID==this->aID)
	{ i=1; }
	else
	{ i=0; }

	return i;

}// end cAccount::Search4ID 

//****************************************************************************************
/* Function Prototypes */

void Insert( List_t &List, Addr_t &Start, Addr_t Pred, Addr_t &Avail,cAccount NewAccount);
bool Search(List_t List, cAccount Account, Addr_t Start, Addr_t &Pred);
void Search4Pred(Addr_t &Pred,List_t List,Addr_t Start, cAccount Account);
void Add(Addr_t &Avail, List_t &List, Addr_t &Start, cAccount NewAccount);
void Load(List_t &List,Addr_t &Start,Addr_t &Avail);
void Display(List_t List, Addr_t Start, Addr_t Avail);
void Print(List_t List,Addr_t Start,Addr_t Avail);
void SaveList(List_t List, Addr_t Start, Addr_t Avail);
void Save(cAccount TempAccount, Infile_t &Infile);

//****************************************************************************************
/* MAIN */

int main()
{

	int COUNT=0;
	Addr_t Start;
	Start=NIL;
	List_t List;
	Addr_t Avail;
	cAccount Account;
	Infile_t Infile;
	
	Load(List,Start,Avail);
	Display(List,Start,Avail);
	Print(List,Start,Avail);
	SaveList(List,Start,Avail);


	return 0;
}// end main
//****************************************************************************************
/* Function Implementations */

void Insert( List_t &List, Addr_t &Start, Addr_t Pred, Addr_t &Avail,cAccount NewAccount)
{
	Addr_t Curr;
	Curr=Avail;
	Avail=Avail+1;
	List[Curr].Account=NewAccount;

	if(Pred!=NIL)
	{

		List[Curr].Link=List[Pred].Link;
		List[Pred].Link=Curr;
	}
	
	else
	{

		List[Curr].Link = Start;
		Start = Curr;
	}
};// end Insert

bool Search(List_t List, cAccount Account, Addr_t Start, Addr_t &Pred)
{

	Addr_t Curr;
	Pred=NIL;
	bool Found;
	bool Done;
	Done=false;
	Curr=Start;

	while((Done!=true)&&(Curr!=NIL))
	{

		if(List[Curr].Account.compID2ID(Account)>0)
		{
			Pred=Curr;
			Curr=List[Curr].Link;
		}
		else if(List[Curr].Account.compID2ID(Account)<0)
		{
			Done=true;
			Found=false;
		}
		else
		{
			Done=true;
			Found=true;
			Account=List[Curr].Account;
		}
	}//end while
	
	return Found;
};//end Search

void Search4Pred(Addr_t &Pred,List_t List,Addr_t Start, cAccount Account)
{
	bool Done=false;
	Addr_t Curr;
	Curr=Start;	
	Pred=NIL;

	while((Done!=true)&&(Curr!=NIL))
	{
		if(List[Curr].Account.compID2ID(Account)>0)
		{
			Pred=Curr;
			Curr=List[Pred].Link;
		}
		else
		{
			Done=true;
		}
	}//end while
}// end Search4Pred

void Add(Addr_t &Avail, List_t &List, Addr_t &Start, cAccount NewAccount)
{
	Addr_t Pred;
	Search4Pred(Pred,List,Start,NewAccount);
	Insert(List,Start,Pred,Avail,NewAccount);
}// end Add

void Load(List_t &List,Addr_t &Start,Addr_t &Avail)
{
	Filename_t Filename;
	Infile_t Infile;
	Addr_t Curr;
	Addr_t Pred;

	cout<<"Begining Load.........."<<endl<<endl;
	cout<<"Enter the name of the data file to be used:"<<endl;
	cin>>Filename;
	Infile.open(Filename,ios::in);
	cAccount NewAccount;
	NewAccount.Read(Infile);

	while(!Infile.eof())
	{
		Add(Avail,List,Start,NewAccount);
		NewAccount.Read(Infile);
	}//end while

	Infile.close();
	cout<<"Read Complete........."<<endl;
};//end load

void Display(List_t List, Addr_t Start, Addr_t Avail)
{
	Addr_t Curr;
	Curr=Start;
	Name_t choice;
	cout<<"Would you like to view the output file?"<<endl;
	cout<<"(YES or NO)"<<endl;
	cin>>choice;
	if(choice=="YES"||choice=="yes"||choice=="Yes")
	{
		cout<<setw(20)<<"ACCOUNT-DATA"<<endl<<endl;
		
		while(Curr!=NIL)
		{
			List[Curr].Account.Display();
			Curr=List[Curr].Link;
		}//end while
	}//end if
	
	else
	{
		cout<<"Very Well.........."<<endl<<endl;
	}
};// end Display

void Print(List_t List,Addr_t Start,Addr_t Avail)
{
	Addr_t Curr;
	Curr=Start;
	Filename_t Filename;
	Outfile_t Outfile;
	
	cout<<"Enter the name of the output file:"<<endl;
	cin>>Filename;
	Outfile.open(Filename,ios::out);
	Outfile<<setw(20)<<"ACCOUNT-DATA"<<endl<<endl;
	while(Curr!=NIL)
	{
		List[Curr].Account.Print(Outfile);
		Curr=List[Curr].Link;
	}//end while
	
	Outfile.close();
	cout<<"Write Sucessful.........."<<endl<<endl;
};// end Print

void SaveList(List_t List, Addr_t Start, Addr_t Avail)
{
	Addr_t Curr;
	Curr=Start;
	Filename_t Filename;
	Infile_t Infile;

	cout<<"Enter the name of the Datafile that you wish to create:"<<endl;
	cin>>Filename;
	Infile.open(Filename,ios::out);

	while(Curr!=NIL)
	{
		List[Curr].Account.write2list(Infile);
		Curr=List[Curr].Link;
	}//en while

	Infile.close();
}// end SaveList

void Save(cAccount TempAccount, Infile_t &Infile)
{
	Filename_t Filename;
	cout<<"Enter the name of the data file you whish to create/n";
	cout<<"(or QUIT to use an existing data file"<<endl;
	cin>>Filename;

	if (strcmp(Filename,"QUIT")!=0)
	{
		Infile.open(Filename,ios::out);
		Name_t choice;
		cout<<"Do you wish to continue (YES or NO): "<<endl;
		cin>>choice;
		
		while(strcmp(choice,"NO")!=0)
		{
			TempAccount.write2data(Infile);
			cout<<"Do you wish to continue (YES or NO):"<<endl;
			cin>>choice;
		}//end while
	}//end if
	
	Infile.close();
}//end Save
//****************************************************************************************

Datafile:

31951
Walker
John
1000.98
69537
Patton
George
876.07
28542
Colt
Samuel
1234.56
28325
Bond
James
6798.00
68128
Hamilton
Alex
9000.00
44206
Lincoln
Abe
00.01
13108
Armstrong
Louis
750.00
41727
Gotti
John
5500.60
21158
Lennon
John
6969.69
52019
McCarthy
Joseph
742.02
37823
Stewart
Jon
29.98
70109
Schrute
Dwight
00.00
74746
Bob
Billy
50.50
18211
Pitt
Brad
2056.89
19724
Blitzer
Wolf
3009.10
49926
Dillinger
John
862.43

Recommended Answers

All 13 Replies

The program is halting at this line:

void Insert( List_t &List, Addr_t &Start, Addr_t Pred, Addr_t &Avail,cAccount NewAccount)
{
   Addr_t Curr;
   Curr=Avail;
   Avail=Avail+1;
   List[Curr].Account=NewAccount;  ///breaks here

And is doing so on the very first record (John Walker)

You also have many warnings that you should attend to, such as at

if(choice=="YES"||choice=="yes"||choice=="Yes")

You cannot compare array based strings with the == operator, you must use one of the strcmp( ) functions.

You have several variables/parameters that don't get used.

The program is halting at this line:

void Insert( List_t &List, Addr_t &Start, Addr_t Pred, Addr_t &Avail,cAccount NewAccount)
{
   Addr_t Curr;
   Curr=Avail;
   Avail=Avail+1;
   List[Curr].Account=NewAccount;  ///breaks here

And is doing so on the very first record (John Walker)

You also have many warnings that you should attend to, such as at

if(choice=="YES"||choice=="yes"||choice=="Yes")

You cannot compare array based strings with the == operator, you must use one of the strcmp( ) functions.

You have several variables/parameters that don't get used.

Here is my new code, I got it to compile/run on my schools machines through PUTTY in windows, however when I try to run on my home Ubuntu, I get the Segmentation Fault. Is it perhaps an error with the library files? I will keep working on it, but I just want to know why I can not get it to run on my machine....

//*************************************************************************************************					
                            /* Assignment 7 DNJ--> CSCI 208 */
//*************************************************************************************************
    #include <iostream>   
    #include <iomanip>   
    #include <fstream>   
    #include <cstring>  
    using namespace std;
//*************************************************************************************************  
      					         /* Global Variables */    
//*************************************************************************************************
 
    const int NAMESIZE=25;
    const int FILENAME=51;
    const int MAXLISTSIZE=200;       
  
    typedef char Name_t[NAMESIZE];
    typedef char Filename_t[FILENAME];
    typedef int ID_t;
    typedef double Bal_t;
    typedef fstream Infile_t;
    typedef fstream Outfile_t;
    typedef int Addr_t;       
 
    const Addr_t NIL=-1;
    const Addr_t END=-2;    
//*************************************************************************************************
      				         /* Class Declerations */   
//*************************************************************************************************

    class cAccount
    {
		private:
			
			Name_t fname;
      		Name_t lname;
      		ID_t aID;
      		Bal_t aBal;       

      	public:       

      		cAccount();                       //used
      		void Read (Infile_t &Infile);     //used
      		void Print (Outfile_t &Outfile);  //used
      		void Display();                   //used
      	        int compID2ID(cAccount NewAccount); //used      		   

      };// end cAccount       
//*************************************************************************************************
      				          /* Struct Declerations */
//*************************************************************************************************
 
      struct ListEle_t
      {
		  cAccount Account;
      	  Addr_t Link;       

      };// end ListEle_t     
//*************************************************************************************************
				                /* Array Decleration */ 
//*************************************************************************************************

      typedef ListEle_t List_t[MAXLISTSIZE];       

//*************************************************************************************************
      			           /* Class Function Implementations */       
//*************************************************************************************************
      cAccount::cAccount()
      {
		  strcpy(fname,"");
      	  strcpy(lname,"");                                                //used
      	  aID=0;
      	  aBal=0;  
	  
	  }// end cAccount::cAccount       
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- 
      void cAccount::Read(Infile_t &Infile)
	  {	Infile>>aID>>lname>>fname>>aBal;}// end cAccount::Read              //used
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void cAccount::Print(Outfile_t &Outfile)  
      {
		  Outfile<<setw(10)<<"ACCOUNT-ID: "<<aID<<endl; 
      	  Outfile<<setw(10)<<"ACCOUNT NAME: "<<lname<<","<<fname<<endl;      //used
      	  Outfile<<setw(10)<<"ACCOUNT BALANCE: $"<<aBal<<endl; 
      	  Outfile<<"------------------------------------------------"<<endl;  
  
	  }// end cAccount::Print           
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void cAccount::Display()
      {
		  cout<<setw(10)<<"ACCOUNT-ID: "<<aID<<endl;
      	  cout<<setw(10)<<"ACCOUNT NAME: "<<lname<<","<<fname<<endl;        //used
      	  cout<<setw(10)<<"ACCOUNT BALANCE: $"<<aBal<<endl;
      	  cout<<"------------------------------------------------"<<endl;       
 
      }// end cAccount::Display       
 //_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      int cAccount::compID2ID(cAccount NewAccount)
      {
		  int count;
      	  count=0;       

      	  if(this->aID > NewAccount.aID)
      	  { count=-1;}// end if     

		  else if(this->aID < NewAccount.aID)        //used
      	  { count=1; }// end else if       

		  else
      	  { count=0;}//end else       

      	  return count;

      }// end cAccount::compID2ID    
//*************************************************************************************************
      				                   /* Function Prototypes */
//*************************************************************************************************
 
      void Insert( List_t &List, Addr_t &Start, Addr_t Pred, Addr_t &Avail,cAccount NewAccount); //used
      void Search4Pred(Addr_t &Pred,List_t List,Addr_t Start, cAccount Account);  //used
      void Add(Addr_t &Avail, List_t &List, Addr_t &Start, cAccount NewAccount);  //used
      void Load(List_t &List,Addr_t &Start,Addr_t &Avail);  //used
      void Display(List_t List, Addr_t Start, Addr_t Avail);//used
      void Print(List_t List,Addr_t Start,Addr_t Avail);    //used
      
//*************************************************************************************************
      					                       /* MAIN */       
//*************************************************************************************************

      int main()
      {
		  int COUNT=0; 
      	  Addr_t Start; 
      	  Start=NIL; 
      	  List_t List; 
      	  Addr_t Avail;
      	  cAccount Account;
      	  Infile_t Infile;

          /* FUNCTION CALLS */

      	  Load(List,Start,Avail);
      	  Display(List,Start,Avail);
      	  Print(List,Start,Avail);
      	  
      	  return 0;

      }// end main 
//************************************************************************************************* 
      				             /* Function Implementations */   
//*************************************************************************************************
      void Insert( List_t &List, Addr_t &Start, Addr_t Pred, Addr_t &Avail,cAccount NewAccount)
      {
		  Addr_t Curr;
      	  Curr=Avail;
      	  Avail=Avail+1;
      	  List[Curr].Account=NewAccount;       
 
      	  if(Pred!=NIL)
      	  {
			  List[Curr].Link=List[Pred].Link;        //used
      		  List[Pred].Link=Curr;
      	  }    
	      else
      	  {
      		  List[Curr].Link = Start;
      		  Start = Curr;
      	  }

      };// end Insert       
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Search4Pred(Addr_t &Pred,List_t List,Addr_t Start, cAccount Account)
      {
		  bool Done=false; 
      	  Addr_t Curr;
      	  Curr=Start;
      	  Pred=NIL;       

     	  while((Done!=true)&&(Curr!=NIL))
      	  {
			  if(List[Curr].Account.compID2ID(Account)>0)   //used
      		  {
				  Pred=Curr;
      			  Curr=List[Pred].Link;
      		  }
      		  else
      		  { Done=true;}
      	  }//end while
      }// end Search4Pred       
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Add(Addr_t &Avail, List_t &List, Addr_t &Start, cAccount NewAccount)
      {
		  Addr_t Pred;

      	  Search4Pred(Pred,List,Start,NewAccount);                //used
      	  Insert(List,Start,Pred,Avail,NewAccount);

	  }// end Add       
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Load(List_t &List,Addr_t &Start,Addr_t &Avail)
      {
		  Filename_t Filename;
      	  Infile_t Infile;
      	  Addr_t Curr; 
      	  Addr_t Pred; 
		  cout<<"Program Starting.........."<<endl;
      	  cout<<"Begining Load.........."<<endl<<endl;
      	  cout<<"Enter the name of the data file to be used:"<<endl;   //used
      	  cin>>Filename;
      	  Infile.open(Filename,ios::in);
      	  cAccount NewAccount; 
      	  NewAccount.Read(Infile);
      	  while(!Infile.eof())
      	  {
			  Add(Avail,List,Start,NewAccount);
      		  NewAccount.Read(Infile);

      	  }//end while
 
      	  Infile.close();
      	  cout<<"Read Complete........."<<endl;
		  cout<<"Load Complete........."<<endl;

      };//end load
 //_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Display(List_t List, Addr_t Start, Addr_t Avail)
      {
		  Addr_t Curr;
      	  Curr=Start;
      	  Name_t choice;
      	  cout<<"Would you like to view the output file?"<<endl;
      	  cout<<"(YES or NO)"<<endl;
      	  cin>>choice;

      	  if(strcmp(choice,"YES")==0||strcmp(choice,"Yes")==0||strcmp(choice,"yes")==0) 
      	  {
			  cout<<setw(20)<<"ACCOUNT-DATA"<<endl<<endl;       

      		  while(Curr!=NIL)                                              //used
      		  {
				  List[Curr].Account.Display();
      			  Curr=List[Curr].Link;

      		  }//end while
			  cout<<endl;
			  cout<<"Moving to next process.........."<<endl<<endl;

      	  }//end if
      	  else
      	  {
			  cout<<"Very Well.........."<<endl;
		      cout<<"Moving to next process.........."<<endl<<endl;
      	  }

      };// end Display
//_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
      void Print(List_t List,Addr_t Start,Addr_t Avail)
      {
		  Addr_t Curr;
      	  Curr=Start; 
      	  Filename_t Filename;
      	  Outfile_t Outfile;
      	  cout<<"Enter the name of the output file: "<<endl;
      	  cin>>Filename; 
      	  Outfile.open(Filename,ios::out);
      	  Outfile<<setw(20)<<"ACCOUNT-DATA"<<endl<<endl;

      	  while(Curr!=NIL)
      	  {
			  List[Curr].Account.Print(Outfile);
      		  Curr=List[Curr].Link;

      	  }//end while       

      	  Outfile.close();
      	  cout<<"Write Sucessful.........."<<endl<<endl;

      };// end Print
//*************************************************************************************************
										/* END OF SOURCE CODE */
//*************************************************************************************************

Telling us the program aborts with Seg Fault doesn't tell us much - just that you've encountered a memory error.

You need to track down the error. I pointed out where the error manifests itself, but that's not the place the cause. You need to start at the line I pointed out and backtrack all the variables involved. Use of a debugger is really needed here, so you can, while the code is executing, follow the path back to the problem source.

I'll tell you this - it's a variable that's not initialized. You look at it a while more.

First, compile with as many warnings as you can.
The compiler will spot things like uninitialised variables, which is a common mistake. Fortunately for you, this seems not to be the case, but still there are issues.

$ g++ -W -Wall -ansi -pedantic -O2 foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:132: warning: unused variable ‘COUNT’
foo.cpp: At global scope:
foo.cpp:170: error: extra ‘;’
foo.cpp: In function ‘void Load(ListEle_t (&)[200], Addr_t&, Addr_t&)’:
foo.cpp:204: warning: unused variable ‘Curr’
foo.cpp:205: warning: unused variable ‘Pred’
foo.cpp: At global scope:
foo.cpp:224: error: extra ‘;’
foo.cpp:226: warning: unused parameter ‘Avail’
foo.cpp:255: error: extra ‘;’
foo.cpp:257: warning: unused parameter ‘Avail’
foo.cpp:278: error: extra ‘;’

Second, Use the debugger (Luke)

$ g++ -g foo.cpp
$ gdb ./a.out 
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) run
Starting program: /home/forum/work/a.out 
Program Starting..........
Begining Load..........

Enter the name of the data file to be used:
test.txt
Read Complete.........
Load Complete.........
*** stack smashing detected ***: /home/forum/work/a.out terminated
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x48)[0xb7e58da8]
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x0)[0xb7e58d60]
/home/forum/work/a.out[0x80499bf]
[0x656e6f]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 327142     /home/forum/work/a.out
0804a000-0804b000 r--p 00002000 08:01 327142     /home/forum/work/a.out
0804b000-0804c000 rw-p 00003000 08:01 327142     /home/forum/work/a.out
08e6f000-08e90000 rw-p 08e6f000 00:00 0          [heap]
b7d5a000-b7d5b000 rw-p b7d5a000 00:00 0 
b7d5b000-b7eb7000 r-xp 00000000 08:01 261651     /lib/tls/i686/cmov/libc-2.9.so
b7eb7000-b7eb8000 ---p 0015c000 08:01 261651     /lib/tls/i686/cmov/libc-2.9.so
b7eb8000-b7eba000 r--p 0015c000 08:01 261651     /lib/tls/i686/cmov/libc-2.9.so
b7eba000-b7ebb000 rw-p 0015e000 08:01 261651     /lib/tls/i686/cmov/libc-2.9.so
b7ebb000-b7ebe000 rw-p b7ebb000 00:00 0 
b7ebe000-b7ecb000 r-xp 00000000 08:01 245345     /lib/libgcc_s.so.1
b7ecb000-b7ecc000 r--p 0000c000 08:01 245345     /lib/libgcc_s.so.1
b7ecc000-b7ecd000 rw-p 0000d000 08:01 245345     /lib/libgcc_s.so.1
b7ecd000-b7ece000 rw-p b7ecd000 00:00 0 
b7ece000-b7ef2000 r-xp 00000000 08:01 261685     /lib/tls/i686/cmov/libm-2.9.so
b7ef2000-b7ef3000 r--p 00023000 08:01 261685     /lib/tls/i686/cmov/libm-2.9.so
b7ef3000-b7ef4000 rw-p 00024000 08:01 261685     /lib/tls/i686/cmov/libm-2.9.so
b7ef4000-b7fd8000 r-xp 00000000 08:01 36238      /usr/lib/libstdc++.so.6.0.10
b7fd8000-b7fdc000 r--p 000e3000 08:01 36238      /usr/lib/libstdc++.so.6.0.10
b7fdc000-b7fdd000 rw-p 000e7000 08:01 36238      /usr/lib/libstdc++.so.6.0.10
b7fdd000-b7fe3000 rw-p b7fdd000 00:00 0 
b7ff3000-b7ff7000 rw-p b7ff3000 00:00 0 
b7ff7000-b7ff8000 r-xp b7ff7000 00:00 0          [vdso]
b7ff8000-b8014000 r-xp 00000000 08:01 245294     /lib/ld-2.9.so
b8014000-b8015000 r--p 0001b000 08:01 245294     /lib/ld-2.9.so
b8015000-b8016000 rw-p 0001c000 08:01 245294     /lib/ld-2.9.so
bfc21000-bfc36000 rw-p bffeb000 00:00 0          [stack]

Program received signal SIGABRT, Aborted.
0xb7ff7430 in __kernel_vsyscall ()
(gdb) bt
#0  0xb7ff7430 in __kernel_vsyscall ()
#1  0xb7d866d0 in raise () from /lib/tls/i686/cmov/libc.so.6
#2  0xb7d88098 in abort () from /lib/tls/i686/cmov/libc.so.6
#3  0xb7dc424d in ?? () from /lib/tls/i686/cmov/libc.so.6
#4  0xb7e58da8 in __fortify_fail () from /lib/tls/i686/cmov/libc.so.6
#5  0xb7e58d60 in __stack_chk_fail () from /lib/tls/i686/cmov/libc.so.6
#6  0x080499bf in Load (List=@0xbfc309a0, Start=@0xbfc33ec4, Avail=@0xbfc33ec0) at foo.cpp:224
#7  0x00656e6f in ?? ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb)

Now go read up on gdb to find other useful commands, say
- setting breakpoints
- examining variables.

> Is it perhaps an error with the library files?
No, 99.99% of the time, it will be your code.
http://www.catb.org/~esr/faqs/smart-questions.html#id382249

You should generally regard "success" on the first system as including a fair amount of "luck".

I must just be missing a library file on my pc if I can run it at school and not at home....

I must just be missing a library file on my pc if I can run it at school and not at home....

No, if it compiled at home, you have everything needed.

Don't blame the tools, blame the carpenter.

Did it in fact run correctly at school? Or did it just run and crash, and not give you a clear signal that it crashed?

You've been given a lot of help and information - use it.

No, if it compiled at home, you have everything needed.

Don't blame the tools, blame the carpenter.

Did it in fact run correctly at school? Or did it just run and crash, and not give you a clear signal that it crashed?

You've been given a lot of help and information - use it.

No at school it runs perfectly, no warnings in the compile/link and no anomalies in the run time. All the functions that are called do exactly what they are supposed to which is why I think there is something wrong with my PC. I also had my peers trace through the code and they came to the same conclusion... if it works on Ubuntu at school why does it not work on Ubuntu at home.

You can see from Salem's backtrace that the program dumped in the function load() : #6 0x080499bf in Load (List=@0xbfc309a0, Start=@0xbfc33ec4, Avail=@0xbfc33ec0) at foo.cpp:224 So that narrows it down a bit. One thing that I notice is that you never check if the file is successfully opened before reading from it.

Your typedefs look a bit suspicious too.

Please forget missing library files, as a matter of fact, what do you actually mean by missing library files?

The only way that it seems to run succesfully (that is, without crashing), is to have it read an empty input file.

[EDIT]
What compiler are you using ..

- at school:
- at home:

> No at school it runs perfectly, no warnings in the compile/link and no anomalies in the run time.
That's just pure dumb luck.

I look at your main() and I see a whole host of uninitialised variables.

The fact that it runs at school just means you got 'good' crap in those variables.
At home, you get a different set of crap values, and a different outcome.

Start by making sure EVERYTHING is initialised.

got it solved, it will work when I set Avail=0; in main

thanks for the help and im sorry if i seemed frustrated, i am very much a newbie to c++

got it solved, it will work when I set Avail=0; in main

thanks for the help and im sorry if i seemed frustrated, i am very much a newbie to c++

Yep, that's the one I referred to before. As Salem said, you should initialize ALL variables, so you start from a known state. In the case of Avail, it has to be 0 when the program starts.

Did you track down how many times that bad value was passed from function to function to function....?

Yep, that's the one I referred to before. As Salem said, you should initialize ALL variables, so you start from a known state. In the case of Avail, it has to be 0 when the program starts.

Did you track down how many times that bad value was passed from function to function to function....?

well in main load called add, which called insert and search4pred so from its deceleration in main, at least 3 times, well for each singular data item in the file

makes my head spin!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.