3 Replies - 1168 Views - Last Post: 11 October 2012 - 11:24 PM Rate Topic: -----

#1 aabolt11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-October 12

I'm getting a Bus error: 10 and can't find it

Posted 11 October 2012 - 07:00 PM

[/
#include <stdio.h>
#include <stdlib.h>
#include "useCurses.h"

#include "robotModel.h"
#include "maze.h"

/* function prototype for initializing maze and robot from command line arguments */
int initMazeAndRobotFromCLArgs(int argc, char *argv[], Maze *maze, RobotModel *robot);

int main( int argc, char *argv[] )
{
    Maze maze;              // strucutre to store maze 
    RobotModel robot;       // structure to model robot (current position + direction)
	int station;			// Initializes the variable station 
	char direction[100];	// creates the array for direction
	int move;				// initializes the variable move for the array direction
	int endpoint;			// initalizes the variable endpoint
	int i;

    // initialize curses mode 
    initscr();

    // if successfully have initialized robot and maze, solve maze
	
	if (initMazeAndRobotFromCLArgs(argc, argv, &maze, &robot)) // while loop that runs until maze hits an endpoint
	{
		
		printMazePlusCurrentPos(maze,robot);	
		printw("\nWhich station would you like your robot to go to? 1 (Food Station), 2 (Drink Station), 3 (Pay Station)  "); //Prints the question prompting what station to go to
		scanw("%d", &station); 	// scans in the 1, 2, or 3 for the station
		endpoint = 0;	// initializes the endpoint for to be 0 to get into the while loop
		direction[move] = 0;		// initializes the array to start at 0
		move = 0;
		i=0;

		while (endpoint<station)		// while loop the only runs when the endpoint is less then the station number
		{
			if (blackToLeft(maze,robot) && blackInFront(maze, robot) && blackToRight(maze,robot) && onBlack(maze,robot))		//if statement to discover the endpoint
			{
				moveStraight(&robot);	//check to make sure it is in the endpoint

				if (blackToLeft(maze,robot) && blackToRight(maze,robot))		//if statement to check again if it is in the end point
				{ 
					endpoint++;
					if (endpoint<station)		// if statement to turn around if the robot is not in the right if statement
					{
						uTurn(&robot);		// makes the robot do a u-turn
						direction[move] = 4;  // set first element of array to 4
						move = move + 1;         // increment index
							moveStraight(&robot);	// makes the robot move straight to get out of the endpoint
							moveStraight(&robot);	
					}
				}
				else if (!blackToLeft(maze, robot) && !blackInFront(maze, robot) && !blackToRight(maze, robot)) //else if statement to make the robot reverse if it isn't in an endpoint
				{

				reverse(&robot);		//makes the robot go backwords
				turnLeft(&robot);	//tells the robot to turn left
				direction[move] = 1;  // set first element of array to 4
				move = move + 1;         // increment index
				}
			}	

			else if (blackToLeft(maze, robot))		//applies the left hand on wall rule to start out forcing the robot to turn left if it can
			{
				turnLeft(&robot);	//makes the robot go left
				direction[move] = 1;  // set first element of array to 1
				move = move + 1;         // increment index
				printMazePlusCurrentPos(maze, robot);	// printing the current position of the "robot"
			}
			else if (blackInFront(maze, robot))		//runs if the robot can't go left so it wants it to go straight
			{
				moveStraight(&robot);		//makes the robot move straight

				if (blackToRight(maze, robot))		//if statement to only count the straights that the robot makes at an intersection
				{
					direction[move] = 2;  // set first element of array to 2
					move = move + 1;         // increment index
					printMazePlusCurrentPos(maze, robot);	// printing the current position of the "robot"
				}
			}
			else if (blackToRight(maze, robot))	// runs if the robot can't go left or straight
			{
				turnRight(&robot);		//makes the robot turn right
				direction[move] = 3;  // set first element of array to 3
				move = move + 1;         // increment index
				printMazePlusCurrentPos(maze, robot);	// printing the current position of the "robot"
			}
			else if (!blackToLeft(maze, robot) && !blackInFront(maze, robot) && !blackToRight(maze, robot))		//runs if the robot can't go left, straight, or right
			{

				uTurn(&robot);		//makes the robot do a U-turn
				direction[move] = 4;  // set first element of array to 4
				move = move + 1;         // increment index
				printMazePlusCurrentPos(maze, robot); 	// printing the current position of the "robot"
			}
		}

		for (i=0; i<=move; i++)		//for loop to print out the directions
		{
			if (direction[move] == 1)
			{
				printw("Left ");
			}
			else if (direction[move] == 2)
			{
				printw("Straight ");
			}
			else if (direction[move] == 3)
			{
				printw("Right ");
			}
			else if (direction[move] == 4)
			{
				printw("UTurn ");
			}
		}
		for (i=move; i>0; i--)		//for loop to print out the directions to go back to start
		{
			if (direction[move] == 1)
			{
				printw("Right ");
			}
			else if (direction[move] == 2)
			{
				printw("Straight ");
			}
			else if (direction[move] == 3)
			{
				printw("Left ");
			}
			else if (direction[move] == 4)
			{
				printw("UTurn ");
			}
		}
	}
		
    printw("\nDONE!");
    getch();
    endwin();
    return 0;
}
]

MOD EDIT: USE CODE TAGS WHEN POSTING CODE LIKE THIS:

:code:

HINT: Highlight your code and then press the [CODE] Icon at the top of the edit window.

This post has been edited by jimblumberg: 11 October 2012 - 08:30 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: I'm getting a Bus error: 10 and can't find it

#2 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: I'm getting a Bus error: 10 and can't find it

Posted 11 October 2012 - 07:09 PM

The bus is a band of connections, connecting the cpu to the motherboard. The CPU uses the bus to fetch data from memory, but if the address is impossible to target, you get a bus error. The CPU fetches multiple bytes at a time, so for example, if you are attempting to fetch an integer out of memory, the address must be divisible by four, otherwise you will receive an error.

Bus errors are usually because you have a type, such as an int pointer for example, and the address it points to is invalid (or garage) and is not divisible by four (this applies to all types)

I don't see anything that would stand out as a bus error, could you post the contents, or at least the prototypes of the function decorations within the headers that you are including?

EDIT: posting your code with code tags would help

This post has been edited by jjl: 11 October 2012 - 07:12 PM

Was This Post Helpful? 0
  • +
  • -

#3 aabolt11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-October 12

Re: I'm getting a Bus error: 10 and can't find it

Posted 11 October 2012 - 07:14 PM

There is two that i will be posting.


[/#
ifndef ROBOTMODEL_H
#define ROBOTMODEL_H

struct robotModel {
    int row; 
    int col; 
    int dir; 
};

typedef struct robotModel RobotModel;

void moveStraight(RobotModel *robot);

void turnLeft(RobotModel *robot);

void turnRight(RobotModel *robot);

void uTurn(RobotModel *robot);

void reverse(RobotModel *robot);

#endif]



Here's the next



[/#ifndef MAZE_H
#define MAZE_H

#include "robotModel.h"

struct maze {
    int *mazeArray; 
    int rows;       
    int cols;     
};

typedef struct maze Maze;

int readMaze(const char *fileName, Maze *m, RobotModel *robot);

void clearMaze(Maze *m);

void printMazePlusCurrentPos(Maze m, RobotModel robot);

int blackToLeft(Maze m, RobotModel robot);

int blackToRight(Maze m, RobotModel robot);

int blackInFront(Maze m, RobotModel robot);

int onBlack(Maze m, RobotModel robot);

#endif]

This post has been edited by jimblumberg: 11 October 2012 - 08:30 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2555
  • View blog
  • Posts: 4,739
  • Joined: 30-May 10

Re: I'm getting a Bus error: 10 and can't find it

Posted 11 October 2012 - 11:24 PM

033 direction[move] = 0; // initializes the array to start at 0
034 move = 0;
Well since move is garbage on line 33, and 0 on line 34, perhaps you're accessing direction out of bounds.

I'm guessing you're on Linux, given your use of curses.

In which case, do this when you compile
$ gcc -g prog.c
$ gdb ./a.out
...
(gdb) run

Now, when it crashes, gdb will point you at a line of code and allow you to examine variables and so forth.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1