user1684402
user1684402

Reputation: 11

Segmentation error when inserting a struct into a queue

I am writing a program that solves a maze. It begins at start square "o" and finishes at "*". "#"s are walls and "." are squares that can be explored. The algorithm is to add the start square to an empty queue at the start of the program. Each subsequent step after that you take a square out of the queue and check if it is a square already recorded, is a finish square or if it's explorable. If it's explorable add the adjacent non-wall squares to the queue. My problem is when I add the start square to the queue I'm getting a segmentation error and I have no idea why. Below is the relevant code:

mazeQ.c:

#include <stdio.h>
#include <stdlib.h>
#include "QueueInterface.h"

int main(void)
{
    Queue Q;
    InitializeQueue(&Q);

    int row;
    int column;

    int x;
    int y;

    scanf("%d %d", &column, &row); //scan dimensions of maze

    char input[row][column];
    ItemType maze[row][column];
    for(x = 0; x <= row; x++) {
            fgets(input[x],column+2,stdin);
    }
    row++;

    for (x = 1; x < row; x++){
            for (y = 0; y < column; y++) {
                    maze[x][y].squareType = input[x][y];
                    maze[x][y].xCoordinate = x-1;
                    maze[x][y].yCoordinate = y;
                    maze[x][y].recordedSquare = 0;
            }
    }

    for(x = 1; x < row; x++){
            for(y = 0; y < column; y++) {
                    if (maze[x][y].squareType == 'o')
                      Insert(maze[x][y],&Q);  //INSERTED HERE
            }
    }
    for (x = 1; x < row; x++) {
            for (y = 0; y < column; y++) {
                    printf("%c", maze[x][y].squareType);
            }
    printf("\n");
    }

}

UserTypes.h:

#include <stdio.h>
#include <stdlib.h>
typedef struct {
    char squareType;
    int xCoordinate;
    int yCoordinate;
    int recordedSquare;
} ItemType;

The two files below have to remain the way they are. QueueImplementation.c:

#include <stdio.h>                   
#include <stdlib.h>                 
#include "QueueInterface.h"         


void SystemError(char *errorMsg) {fprintf(stderr,errorMsg);}

void InitializeQueue(Queue *Q)
{
  Q->Front = NULL;
  Q->Rear  = NULL;
}

int Empty(Queue *Q)
{
  return (Q->Front == NULL);
}

int Insert(ItemType R, Queue *Q)
{   
  QueueNode *Temp;
                                               /* attempt to allocate */
  Temp = (QueueNode *) malloc(sizeof(QueueNode));       /* a new node */

  if (Temp == NULL) {               /* Temp = NULL signals allocation */
     SystemError("system storage is exhausted");           /* failure */
     return 0;
  } else {
     Temp->Item = R;
     Temp->Link = NULL;
     if ( Q->Rear == NULL ) {
        Q->Front = Temp;
        Q->Rear = Temp;
     } else {
        Q->Rear->Link = Temp;
        Q->Rear = Temp;
     }
   }
   return 1;
}

QueueInterface.h:

#include "UserTypes.h"                          /* get ItemType definition */

typedef  struct  QueueNodeTag {
        ItemType               Item;   
        struct  QueueNodeTag  *Link;
     }QueueNode;

typedef  struct {                                 /* a queue is empty iff */
        QueueNode  *Front;                       /* its Front == NULL */
        QueueNode  *Rear;
     }Queue;


/* defined operations */

extern void InitializeQueue(Queue *Q);
  /* Initialize the queue Q to be the empty queue */

extern int Empty(Queue *Q);
  /* Returns TRUE == 1 if and only if the queue Q is empty */

extern int Full(Queue *Q);
  /* Returns TRUE == 1 if and only if the queue Q is full */

extern int Insert(ItemType R, Queue *Q);
  /* If Q is not full, insert a new item R onto the rear of Q */

extern int Remove(Queue *Q, ItemType *F);
  /* If Q is non-empty, remove the frontmost item of Q and put it in F */

The program is run by typing mazeQ < sampleMaze

sampleMaze

12 10
############
#..........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#
############

Upvotes: 0

Views: 365

Answers (2)

user1684402
user1684402

Reputation: 11

Apparently the problem was that the directory I was putting my program files in had too many other junk files taking up memory. All I had to do was move my program files to another directory.. I guess I learned my lesson to always make a new directory for a new program. Thanks for the help everyone.

Upvotes: 0

Serge
Serge

Reputation: 6095

I don't know why do you get it seg-fault. I got this output by running it:

$ gcc *.c -o maze
$ ./maze <sample 
ļ##
ļ####
ļ######
ļ########
##########
############
###########.
#########...
#######.....
#####......#

Ok, after some cleanup I got this code:

#include <stdio.h>
#include <stdlib.h>
#include "QueueInterface.h"

int main(void)
{
    Queue Q;
    InitializeQueue(&Q);

    int row;
    int column;

    int x;
    int y;

    scanf("%d %d\n", &column, &row); //scan dimensions of maze


    char input[row][column+2];
    ItemType maze[row][column];
    for(x = 0; x < row; x++) {
            fgets(input[x],column+2,stdin);
    }

    for (x = 0; x < row; x++){
            for (y = 0; y < column; y++) {
                    maze[x][y].squareType = input[x][y];
                    maze[x][y].xCoordinate = x;
                    maze[x][y].yCoordinate = y;
                    maze[x][y].recordedSquare = 0;
            }
    }

    for(x = 0; x < row; x++){
            for(y = 0; y < column; y++) {
                    if (maze[x][y].squareType == 'o')
                      Insert(maze[x][y],&Q);  //INSERTED HERE
            }
    }
    for (x = 0; x < row; x++) {
            for (y = 0; y < column; y++) {
                    printf("%c", maze[x][y].squareType);
            }
    printf("\n");
    }

}

And it's output:

############
#..........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#
############

Try it.

Upvotes: 1

Related Questions