Thread: Segmentation fault while using 2D array

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Segmentation fault while using 2D array

    I am trying to write a program to solve simple mazes in C 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 in the function get maze somewhere in the switch 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 compile with gdb.

    I hope anyone can help me.
    Last edited by Damon Dike; 04-02-2010 at 04:21 PM. Reason: Adding the compiler I used

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    types[3] = 1;
    this is out of bounds access

    doolhof = malloc((*column)*sizeof(int));

    you need sizeof (int*) or better yet
    sizeof (*doolhof) here

    Code:
    	for (i = 0; i < *row; i++){
    		for (j = 0; j < *column; j++){
    you have column x row allocation while accessing it as
    row x column...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    I can see why I should use sizeof(*int) but I don't get why sizeof(*doolhof) would be right. I am not making an array with pointers to doolhof but an array with pointers to ints.

    You are right, I have swapped row and column and types should be declared as types[4].

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Damon Dike View Post
    but I don't get why sizeof(*doolhof) would be right. I am not making an array with pointers to doolhof but an array with pointers to ints.
    What happens if you dereference a pointer to an integer? You end up with an integer. What happens if you use sizeof on an integer?


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

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Quote Originally Posted by quzah View Post
    What happens if you dereference a pointer to an integer? You end up with an integer. What happens if you use sizeof on an integer?


    Quzah.
    You get some space the with the sizeof an int while you want to put a pointer to an int in there. But I still don't get while you should use *doolhof.

    Btw. If I use
    Code:
    	doolhof = malloc((*column)*sizeof(*int));
    I get an error saying: "expected expression before int". Anyone, because I don't see how this could be wrong, it is obviously but I don't get why.

    Edit: solved the * should be placed after the int instead of before.
    Last edited by Damon Dike; 04-03-2010 at 02:00 AM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    *doolhof is of type int*

    so sizeof (*doolhof) == sizeof(int*)

    first is better because if you change type of doolhof sizeof will adjust automatically

    sizeof(*int) has no meaning
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    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;
    }

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Segfault ? What segfault ? This won't even compile.
    What you have posted both times is missing stuff.
    I get this on your last:
    Code:
    ~>  gcc -Wall -W -pedantic segfault_2darr-2.c  -o  100401_segfault_2darr-2
    segf_2darr-2.c:17: warning: unused parameter ‘row’
    segf_2darr-2.c: In function ‘getMaze’:100401_segfault_2darr-2.c:39: error: ‘debug’ undeclared (first use in this function)
    segf_2darr-2.c: In function ‘main’:
    segf_2darr-2.c:92: error: ‘q’ undeclared (first use in this function)
    segf_2darr-2.c:92: error: invalid application of ‘sizeof’ to incomplete type ‘struct queue’     
    segf_2darr-2.c:96: warning: implicit declaration of function ‘resetTypes’
    What is "struct queue"? Are we supposed make that and resetTypes() up ?
    I mean , throw us a half decent bone dude!
    Last edited by HowardL; 04-03-2010 at 08:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic array segmentation fault
    By Argentius in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2007, 04:50 AM
  2. Segmentation fault with array?
    By whiphub in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 01:51 PM
  3. Replies: 3
    Last Post: 04-19-2004, 06:42 PM
  4. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 07:03 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM