1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Segmentation Faults with 2D-array

Discussion in 'C' started by Damon Dike, Apr 2, 2010.

  1. Damon Dike

    Damon Dike New Member

    Joined:
    Apr 2, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to write a program to solve simple mazes but somehow I can't get it to put the maze in a 2D-array, this is my code:

    Code:
    int **doolhof;
    
    void allocateMemory(int *column, int *row){
    	int i;
    	doolhof = malloc((*column)*sizeof(int));
    	for(i = 0; i < *column; i++){
    		doolhof[i] = malloc((*row)*sizeof(int));
    		if(debug) printf("alloceerGeheugen - i = %d\n", i);
    		if (doolhof[i] == NULL) {
    			printf("Not enough memory\n");
    			exit(-1);
    		}
    	}
    }
    
    void getMaze(int *maxStap, int *column, int *row){
    	int i, j;
    	char temp;
    	scanf("%d %d", row, column);
    	*maxStap = (*row) * (*column) + 1;
    	allocateMemory(column, row);
    	for (i = 0; i < *row; i++){
    		for (j = 0; j < *column; j++){
    			scanf("%c", &temp);
    			switch (temp) {
    				case '.':
    					if(j == (*column) - 1 || j == 0 || i == (*row) - 1 || i == 0) doolhof[i][j] = -4;
    					else doolhof[i][j] = *maxStap;
    					break;
    				case '#':
    					doolhof[i][j] = -1;
    					break;
    				case '^':
    					doolhof[i][j] = -2;
    					break;
    				case '%':
    					doolhof[i][j] = -3;
    					break;
    				default:
    					j--;
    					break;
    			}
    		}
    	}
    	/* Filling the center of the maze with zero */
    	doolhof[*row/2][*column/2] = 0;
    }
    
    void readIn(int *types, int *column, int *row, int *maxStep){
    	int gate, flower, personen, i, lengte;
    	scanf("%d %d", &gate, &flower);
    	scanf("%d", &personen);
    	for(i = 0; i < personen; i++){
    		scanf("%d", &lengte);
    		if(lengte > flower) {
    			if(lengte < gate) {
    				types[0] = 1;
    			} else {
    				types[2] = 1;
    			}
    		} else {
    			if( lengte < gate) {
    				types[1] = 1;
    			} else {
    				types[3] = 1;
    			}
    		}
    	}
    	getMaze(maxStep, column, row);
    }
    
    int main(int argc, char *argv[]){
    	int n, i, result;
    	int column = 0, row = 0, maxStep = 0;
    	int types[3];
    	scanf("%d", &n);
    	resetTypes(types);
    	for (i = 0 ; i < n; i++){
    		readIn(types, &column, &rij, &maxStep);	
    	}
    	return 0;
    }
    
    n is the number of mazes to be solved.
    readIn is supposed to read in all information relevant to solving the maze such as the maximum hight to be able to step through a gate represented by ^ and -2, the minimum size to be able to step over a flowerbed represented by % and -3. Personen is the size of the team, lengte containts the size of each team member. The four types of team members are:
    0 these can step over flowerbeds and walk through gates.
    1 these can walk through gates.
    2 these can step over flowerbeds.
    3 these can not step over flowerbeds nor can they walk through gates, they can only walk on the grass, represented by '.'

    Get maze actually fills the 2D array with numbers representing the obstacles in the mazes. Places where everybody can walk are represented by column*row+1, this to facilitate the use of Dijkstra's algorithm later on.

    Whenever I try to run this program I get a segmentation fault while reading in the last lines of the mazes. I can't imagine what goes wrong especially as reading in the first few lines doesn't seem to be a problem.

    I hope anyone can help me.
     
  2. Damon Dike

    Damon Dike New Member

    Joined:
    Apr 2, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    If anyone could tell how I can switch HTML code from off to on I would be grateful, cause this doesn't look very inviting.
     
  3. Damon Dike

    Damon Dike New Member

    Joined:
    Apr 2, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Nevermind this one I should have used capital letters and a forward slash instead of a backward. I hope one of the admins can change it as I can't edit my own posts or can't find the right button to do that.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    Done that and edit option will be available after you have double digit post count.
     
  5. Damon Dike

    Damon Dike New Member

    Joined:
    Apr 2, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Solved, the code seems to work for now, here it is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int **doolhof;
    
    void printMaze(int *column, int *row){
    	int i, j;
    	for (i = 0; i < *row; i++){
    		for (j = 0; j < *column; j++){
    			printf("%d ", doolhof[i][j]);
    		}
    		printf("\n"); 
    	}
    	printf("\n");
    }
    
    void allocateMemory(int *column, int *row){
    	int i;
    	doolhof = calloc( *column, sizeof(int*));
    	for(i = 0; i < *column; i++){
    		doolhof[i] = calloc(*column, sizeof(int));
    		if (doolhof[i] == NULL) {
    			printf("Not enough memory\n");
    			exit(-1);
    		}
    	}
    }
    
    void getMaze(int *maxStap, int *column, int *row){
    	int i, j;
    	char temp;
    	scanf("%d %d", row, column);
    	*maxStap = (*row) * (*column) + 1;
    	allocateMemory(column, row);
    	scanf("%c", &temp);
    	for (i = 0; i < *row; i++){
    		for (j = 0; j < *column; j++){
    			scanf("%c", &temp);
    			if(debug) printf("i = %d, j=%d, temp = %c\n", i, j, temp);
    			switch (temp) {
    				case '.':
    					if(j == (*column) - 1 || j == 0 || i == (*row) - 1 || i == 0) doolhof[i][j] = -4;
    					else doolhof[i][j] = *maxStap;
    					break;
    				case '#':
    					doolhof[i][j] = -1;
    					break;
    				case '^':
    					doolhof[i][j] = -2;
    					break;
    				case '%':
    					doolhof[i][j] = -3;
    					break;
    				default:
    					j--;
    					break;
    					printMaze(column, row);
    			}
    		}
    	}
    	/* Midden van het doolhof op nul zetten */
    	doolhof[*row/2][*column/2] = 0;
    }
    
    void readIn(int *types, int *column, int *row, int *maxStep){
    	int gate, flower, personen, i, lengte;
    	scanf("%d %d", &gate, &flower);
    	scanf("%d", &personen);
    	for(i = 0; i < personen; i++){
    		scanf("%d", &lengte);
    		if(lengte > flower) {
    			if(lengte < gate) {
    				types[0] = 1;
    			} else {
    				types[2] = 1;
    			}
    		} else {
    			if( lengte < gate) {
    				types[1] = 1;
    			} else {
    				types[3] = 1;
    			}
    		}
    	}
    	getMaze(maxStep, column, row);
    }
    
    int main(int argc, char *argv[]){
    	int n, i, result;
    	int column = 0, row = 0, maxStep = 0;
    	int types[4];
    	q = malloc(sizeof(struct queue));
    	q->first = NULL;
    	q->last = NULL;
    	scanf("%d", &n);
    	resetTypes(types);
    	for (i = 0 ; i < n; i++){
    		readIn(types, &column, &row, &maxStep);
    	}
    	return 0;
    }
    
     

Share This Page