2 Replies - 2390 Views - Last Post: 23 April 2010 - 12:19 PM Rate Topic: -----

#1 gilzero   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-February 10

linked list bus error

Posted 22 April 2010 - 11:42 PM

I am trying to get my linked list to work... anyone see anything wrong with this code?

This is what my declarations look like up top...
class CAR{
public:
	char *number;
	KIND kind;
	bool loaded;
	char *destination;
};

class NODE{
public:
	CAR car;
	NODE *link;
};

class StringOfCars{
public: 
	int count;
	NODE *head;
};

StringOfCars *initializeEmptyString();
void add(StringOfCars *, CAR *);
void remove(StringOfCars *);
void printAll(StringOfCars *);

char *setUpCar(char *);
void getInput(CAR *);
CAR *input();
void printCar(CAR *);
void cleanCar(CAR *);



I am trying to debug this function...
void add(StringOfCars *stringOfCars, CAR *car){
	NODE *pNew;
	NODE *pPre;
	NODE *pWalker;
	
	pPre=NULL;
	pNew=new NODE();
	pWalker=stringOfCars->head;
	
	if(pNew){
		pNew->car=*car;

		printCar(&pNew->car);
                //the program gets a "Bus Error" here
		while(pWalker->link){ //get to the end of the list
			pPre=pWalker;
			pWalker=pWalker->link;
		}
		
		pPre->link=pNew;
		pNew->link=NULL;
	}
	
	return;
}


This post has been edited by gilzero: 22 April 2010 - 11:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: linked list bus error

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: linked list bus error

Posted 23 April 2010 - 05:05 AM

Your CAR contains pointers, but you've not defined a copy constructor which would properly handle the pointers, so this
pNew->car=*car;

results in undefined behavior.

Consider making the NODE hold a CAR * instead, or define a copy constructor that will be proper copies of the pointers held within the CAR object. Note, this also assumes you've properly allocated and populated the memory to which those pointers are pointed.
Was This Post Helpful? 0
  • +
  • -

#3 gilzero   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-February 10

Re: linked list bus error

Posted 23 April 2010 - 12:19 PM

I changed my CAR class in NODE to a pointer, but that did not change anything. I also adjusted my add() function, that also did not change anything... I'm stumped.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define FLUSH while( getchar()!='\n') 

enum KIND{box, tank, flat};
const int MAX_SIZE=100;

class CAR{
public:
	char *number;
	KIND kind;
	bool loaded;
	char *destination;
};

class NODE{
public:
	CAR *car;
	NODE *link;
};

class StringOfCars{
public: 
	int count;
	NODE *head;
};

StringOfCars *initializeEmptyString();
void add(StringOfCars *stringOfCars, CAR *car, NODE *pCur);
void remove(StringOfCars *stringOfCars);
void printAll(StringOfCars *stringOfCars);

char *setUpCar(char *tempString);
void getInput(CAR *car);
void input(CAR *car);
void printCar(CAR *car);
void cleanCar(CAR *car);

int main () {
	
	int numCars;
	StringOfCars *stringOfCars;
	CAR car;
	NODE *pCur;
	
	pCur=new NODE(); 
	
	printf("How many cars would you like to add: ");
	scanf("%d", &numCars);
	FLUSH;
	
	stringOfCars=initializeEmptyString();
	pCur=stringOfCars->head;
	
	do{
		input(&car);
		add(stringOfCars, &car, pCur);
		numCars--;
	}while(numCars);
		   
	printAll(stringOfCars);
	remove(stringOfCars);
    
	return 0;
}

StringOfCars *initializeEmptyString(){
	StringOfCars *stringOfCars;
	stringOfCars = new StringOfCars();
	if(stringOfCars){
		stringOfCars->count=0;
		stringOfCars->head=NULL;
	}
	return stringOfCars;
}

void add(StringOfCars *stringOfCars, CAR *car, NODE *pCur){
	NODE *pNew;
	pNew=new NODE();
	
	if(pNew){
		pNew->car=car;
		stringOfCars->count++;
		
		printCar(pNew->car);
		printf("\n");
		
		pCur->link=pNew; //giving error
		pNew->link=NULL;
	}
	
	return;
}

void remove(StringOfCars *stringOfCars){
	NODE *pWalker;
	NODE *pPre;
	
	pWalker=stringOfCars->head;
	while(pWalker->link){
		pPre=pWalker;
		pWalker=pWalker->link;
	}
	cleanCar(pWalker->car);
	pPre->link=NULL;
		   
	return;
}

void printAll(StringOfCars *stringOfCars){
	NODE *pWalker;
	pWalker=stringOfCars->head;
	while(pWalker){
		printCar(pWalker->car);
	}
	return;
}

//Dynamically allocated space to store variables in CAR struct
char *setUpCar( char *tempString){
	char *string;
	int stringSize;
	
	stringSize = strlen( tempString ) +1;
	//string=(char *) calloc(stringSize, sizeof(char));
	string=new char[stringSize];
	if( string == NULL ){
		printf("ERROR, not enough memory!!!\n");
		exit(103);
	}
	
	strcpy(string, tempString);
	
	return string;
}

void getInput(CAR *car){
	
	char inputNumber[MAX_SIZE]={0};
	char inputKind[MAX_SIZE]={0};
	char inputFull[MAX_SIZE]={0};
	char inputCity[MAX_SIZE]={0};
	
	//Prompt user for railroad car information for struct
	printf("Enter the railroad car number: ");
	fgets(inputNumber,MAX_SIZE,stdin); //read input from stdin
	*(inputNumber + strlen(inputNumber) - 1 ) = '\0'; //eliminate \n
	fflush(stdin); //flush buffer
	car->number=setUpCar(inputNumber);
	
	do{
		printf("Enter the kind (box/tank/flat): ");
		scanf("%s", inputKind);
		FLUSH;
		//check for valid input
		if(strcmp("box", inputKind)==0){
			car->kind=(KIND)box;
		}else 
			if(strcmp("tank", inputKind)==0){
				car->kind=(KIND)tank;
			}else if(strcmp("flat", inputKind)==0)
				car->kind=(KIND)flat;
	}while(strcmp("box", inputKind)!=0 && strcmp("tank", inputKind)!=0 && strcmp("flat", inputKind)!=0);
	
	do{
		//if(strcmp("true",inputFull)!=0 && strcmp("false", inputFull)!=0){
		printf("Type \"true\" if the railroad car is loaded, \"false\" otherwise: ");
		//	}
		scanf("%s", inputFull);
		FLUSH;
	}while(strcmp("true",inputFull)!=0 && strcmp("false", inputFull)!=0);
	if(strcmp("true", inputFull)==0){
		car->loaded=true;
	}else
		car->loaded=false;
	
	printf("Enter the destination city: ");
	fgets(inputCity,MAX_SIZE,stdin); //Read input from stdin
	*(inputCity + strlen(inputCity) - 1 ) = '\0'; //eliminate \n
	//fflush(stdin); //flush buffer
	car->destination=setUpCar(inputCity); //allocate space for car.destination
	printf("\n");
	
	return;
}

//CAR structure is created and returned.
//User is prompted for input about railroad car.
void input(CAR *car){
	
	getInput(car);

	return;
}

void printCar(CAR *car){
	//Print out the contents of the railroad car
	printf("Car number: %s.\n", car->number);
	
	if((KIND)box==car->kind)
		printf("Car kind: box.\n");
	else if((KIND)tank==car->kind)
		printf("Car kind: tank.\n");
	else 
		printf("Car kind: flat.\n");
	
	if(car->loaded)
		printf("Car is loaded? True.\n");
	else
		printf("Car is loaded? False.\n");
	
	printf("Car is headed to %s.\n", car->destination);

	return;
}

//Function output receives CAR struct and prints out the contents.
//Nothing is returned.
//Memory is freed.
void cleanCar(CAR *car){
	//free memory
	delete(car->number);
	delete(car->destination);
	delete(car);
	
	return;
}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1