Segmentation fault

This is a discussion on Segmentation fault within the C Programming forums, part of the General Programming Boards category; I am getting a segmentation fault when I try to run the program.. IT compiles fine but when i run ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    Segmentation fault

    I am getting a segmentation fault when I try to run the program.. IT compiles fine but when i run it it gives me a segmentation fault.

    Code:
    #include <stdio.h>
    
    #define START 0
    #define WALL  -1
    #define CORR  -2
    #define END   -3
    #define SOL   -4
    
    int m[101][101], nx, ny;
    int print_maze(FILE *outF)
    {
      /* Print the maze, and return the number of dots printed. */
    
      int ix, iy, ndots = 0;
    
      for (iy = 1;  iy <= ny;  ++iy) {
        for (ix = 1;  ix <= nx;  ++ix) {
          switch (m[ix][iy]) {
          case CORR:  fprintf(outF, " ");  break;
          case END:   fprintf(outF, "$");  break;
          case SOL:   fprintf(outF, ".");  ++ndots;  break;
          case START: fprintf(outF, "*");  break;
          case WALL:  fprintf(outF, "X");  break;
          default:    fprintf(outF, " ");
          }
        }
        fprintf(outF, "\n");
      }
      return ndots;
    }
    
    
    void read_maze(FILE *inF)
    {
      int ix, iy, c = ' ';
    
      for (iy = 1;  iy <= ny;  ++iy) {
        while (c != '\n') c = fgetc(inF);
    
     for (ix = 1;  ix <= nx;  ++ix) {
          c = fgetc(inF);
          switch (c) {
            case 'X': m[ix][iy] = WALL;   break;
            case ' ': m[ix][iy] = CORR;   break;
            case '*': m[ix][iy] = START;  break;
            case '$': m[ix][iy] = END;    break;
          }
        }
      }
    }
    
    
    void retrace(int ix, int iy, int n)
    {
      if (n == 0) return;
    
      if (ix>1 && m[ix-1][iy]==n) {
        m[ix-1][iy] = SOL;  retrace(ix-1, iy, n-1);
      } else if (ix<nx && m[ix+1][iy]==n) {
        m[ix+1][iy] = SOL;  retrace(ix+1, iy, n-1);
      } else if (iy>1 && m[ix][iy-1]==n) {
        m[ix][iy-1] = SOL;  retrace(ix, iy-1, n-1);
      } else if (iy<ny && m[ix][iy+1]==n) {
        m[ix][iy+1] = SOL;  retrace(ix, iy+1, n-1);
      }
    }
    
    
    int check(int ix, int iy, int n)
    {
      if (m[ix][iy] == CORR) {
        m[ix][iy] = n;  return 1;
      }
      if (m[ix][iy] == END) {
        retrace(ix, iy, n-1);  return -10;
      }
      return 0;
    }
    int solve_maze(void)
    {
      int ix, iy, anymore, n = 0, status;
    
      do {
        ++n;  anymore = 0;
        for (ix = 1;  ix <= nx;  ++ix) {
          for (iy = 1;  iy <= ny;  ++iy) {
            if (m[ix][iy] == n-1) {
     status = 0;
              if (ix > 1)  status += check(ix-1,iy,n);
              if (ix < nx) status += check(ix+1,iy,n);
              if (iy > 1)  status += check(ix,iy-1,n);
              if (iy < ny) status += check(ix,iy+1,n);
              if (status < 0) return 1;
              if (status) anymore = status;
            }
          }
        }
      } while (anymore);
    
      return 0;
    }
    
    
    int main(void)
    {
      FILE *inF  = fopen("maze.in", "r"),
           *outF = fopen("maze.out", "w");
      int  n, i, ok, nsteps;
    
      fscanf(inF, "%d", &n);
      for (i = 1;  i <= n;  ++i) {
        fscanf(inF, "%d%d", &nx, &ny);
        read_maze(inF);  ok = solve_maze();
        nsteps = print_maze(outF)+1;
        if (ok) fprintf(outF, "The optimal path is %d steps.\n", nsteps);
        else    fprintf(outF, "This maze has no solutions.\n");
      }
      fclose(inF);  fclose(outF);
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    first error i found: are the files found?

    youd check if

    FILE *inF = fopen("maze.in", "r"),

    that succeeded.

    doesnt your ide youre using support step by step debugging?
    thats the easiest way to track down errors
    signature under construction

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    no does not support line by line debugging using a unix system.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    well there is gdb and ddd - but dont ask me how to use them


    at least i know that ddd means dhe debug dool just kidding
    signature under construction

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, arrays start at zero and go through size - 1. You're probably charging off of the end of your array some place you don't belong.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21