Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    segment fault in function

    hi,

    I have some code that compiles 100% error/warning free, but whenever i execute the program its spits back segment faults.

    The aim of the code is to search a 2d array (size rowXcol) for a specific character (in the first case 'W') and print to a file a string ("wumpus") plus the x/y co ordinates of where it occurs. Each time it occurs it should print the same string and different x/y co ords on a newline (FILE * passed down 1 line).
    Then it should return the FILE * to the calling function where it can search for a new char , then print another string/x/y into the file and so on...


    here is the code:
    somewhere in int main() {
    ...
    FILE* outFilePtr;

    outFilePtr = insert_data( outFilePtr, 'W' , "wumpus", map, data );
    outFilePtr = insert_data(outFilePtr, 'G' , "gold", map, data);
    outFilePtr = insert_data(outFilePtr, 'P' , "pit", map, data);
    outFilePtr = insert_data(outFilePtr, 'E' , "exit", map, data);
    ... }
    FILE * insert_data( FILE * filePtr, char toFind, char word[], char ** map, int * data)
    {
    int c=0, d=0;
    char line[25];
    int row = data[5];
    int col = data[6];


    while( c < col ) {
    while ( d < row ) {
    if( map[c][d] == toFind ) {
    sprintf( line,"%s %d %d\n", word , d, c);
    fputs(line, filePtr);
    }
    d++;
    }
    d=0;
    c++;
    }
    return filePtr;
    }




    Now i know the segment error occurs the first time it calls insert_data, but i can't find where. Possible its some error where a < is put in place of a > or something similar, but whatever it is, any help would be really appreciated.

    Thanks.

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It actualy works for me. I changed nothing of your insert_data function and it works.
    I did guess on what to put in the data and map array, but this was my output:
    Code:
    gold 1 1
    pit 0 3
    exit 4 0
    Could this be right? there was no 'W' in my map array. I filled it from 'A' to 'T'. So that explains why wumpus isn't in the file

    Here is the complete code I used to test it:
    Code:
    #include <stdio.h>
    
    FILE * insert_data( FILE * filePtr, char toFind, char word[], char **map, int * data) ; 
    
    
    int main() 
    { 
    	FILE* outFilePtr; 
    	int data[]={0,0,0,0,0,0,0};//initialize data with 0
    	char **map;
    	int i,j;
    	int num_rows=4;//the max rows for the map array
    	int num_cols=5;//the max cols for the map array
    
    	map = new char*[num_rows];//allocata memory for the rows
    	for ( i = 0 ; i < num_rows; i++ ) map[i] = new char[num_cols];//allocate memory for the cols
    	
    	for(i=0;i<4;i++)
    	{
    		for(j=0;j<5;j++)
    		{
    			map[i][j]='A'+((i*5)+j);//fill the map array with the first 20 chars of the alfabet
    			printf("%c",map[i][j]);//display them on screen
    		}
    		printf("\n");
    	}
    	
    	data[6]=num_rows;//I have no idea what the data array should be holding
    	data[5]=num_cols;//I assume it is info on the map array, like max rows and max cols
    	outFilePtr=fopen("test.txt","w+");//open the file test.txt for the output of the insert_data function
    
    	outFilePtr = insert_data( outFilePtr, 'W' , "wumpus", map, data ); 
    	outFilePtr = insert_data(outFilePtr, 'G' , "gold", map, data); 
    	outFilePtr = insert_data(outFilePtr, 'P' , "pit", map, data); 
    	outFilePtr = insert_data(outFilePtr, 'E' , "exit", map, data); 
    
    	for ( i = 0 ; i < num_rows ; i++ ) delete map[i];//remove the map array from memory
    	delete map;
    
    	fclose(outFilePtr);//close the test.txt file
    
    	return 0;
    } 
    
    
    
    //Haven't changed anything here
    FILE * insert_data( FILE * filePtr, char toFind, char word[], char ** map, int * data) 
    { 
    int c=0, d=0; 
    char line[25]; 
    int row = data[5]; 
    int col = data[6]; 
    
    while( c < col ) { 
    	while ( d < row ) { 
    		if( map[c][d] == toFind ) { 
    			sprintf( line,"%s %d %d\n", word , d, c); 
    			fputs(line, filePtr); 
    		} 
    		d++; 
    	} 
    	d=0; 
    	c++; 
    } 
    
    return filePtr; 
    }

  • #3
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    error still there

    hmmm i should have really said this, but as I'm just learning C now, i don't know much about C++ (as i see you used new/delete and im not sure what they do, maybe malloc and free?).
    I dont know if that makes a difference but anyway...


    i have found out that the error is in the if statement in insert_data, (when i /* */ quote it out it runs fine), so can anyone see a problem with anything inside the if statement?


    oh and on a bit of a different note, how do u make that 'code' quote with the 2 separators show up in your msg?

    thanks

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    124
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah new and delete are the equivalent (sp?) of malloc() and free()

    Personaly, I can't see anything wrong with your if statement. but then again, I'm no C/C++ guru.
    Are you sure you allocate the memory for your map array right?
    Because when you go out of the scope of your array, that can give errors

    >>oh and on a bit of a different note, how do u make that 'code' quote with the 2 separators show up in your msg?
    It is the same as the quote tags, but you put them between [code] and [/code]

  • #5
    New Coder
    Join Date
    Sep 2002
    Location
    Australia
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem found

    thanks for the help


    but the problem was in my main function,

    for some reason i 'free' 'd the 2d array before i had finished using it.

    but problem solved now.

    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •