Allocation - pointer-to-pointer
I would like to ask for help with allocation .... I got this homework to school...I have to write program which will load one G matrix and second G matrix and will search second G matrix for number of presences of the first G matrix....But, when I try to run my program I got Segmentation fault message... Thanks in advance. Example how the progr开发者_如何学Cam is supposed to work....
...
Enter number of lines of wanted g matrix: 3 Enter the wanted g matrix:
121212 212121 121212 G matrix to be searched: 12121212121212 21212121212121 12121212123212 21212121212121 12121212121212 G matrix found 8 times....
this is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * get_line(void) // get line
{
char * string;
if((string = (char *)malloc(100 * sizeof(char))) == NULL)
{
printf("Nedostatek pameti.\n"); // not enough memory
exit(1);
}
int i = 0;
while((string[i] = fgetc(stdin)) != '\n')
{
i++;
if((i % 100) == 0)
{
if((string = (char *)realloc(string, 100 * ( i - 1 ) * sizeof(char))) == NULL)
{
printf("Nedostatek pameti.\n"); // not enough memory
exit(1);
}
}
}
return ( string );
}
char ** get_wanted_g_matrix(int pocetradek /*number of lines*/) // get wanted g matrix
{
char ** string;
printf("Zadejte hledanou matici:\n");
int i = 0;
if(( * string = (char ** )malloc(100 * sizeof(char *))) == NULL)
{
printf("Nedostatek pameti.\n"); // not enough memory
exit(1);
}
while(i <= (pocetradek - 1))
{
string[i] = get_line();
if((i > 1) && (*string[i-1] != strlen(*string[i])))
{
printf("Nespravny vstup.\n"); // not enough memory
exit(1);
}
printf("%s", string[i]);
i++;
if((i % 100) == 0)
{
if((* string = (char **)realloc(* string, 100 * ( i - 1 ) * sizeof(char *))) == NULL)
{
printf("Nedostatek pameti.\n"); // not enough memory
exit(1);
}
}
}
return (string);
}
int get_number_of_lines(void) // get number of lines
{
int number_of_lines;
printf("Zadejte pocet radek hledane matice:\n"); // enter the number of lines of wanted g matrix
if(scanf("%d", &number_of_lines) != 1)
{
printf("Nespravny vstup.\n"); // Error
exit(1);
}
return ( number_of_lines );
}
char ** get_searched_g_matrix(void) // get wanted g matrix
{
char ** string;
printf("Matice, ktera bude prohledana:\n"); // G matrix to be searched
int i = 0;
if(( * string = (char ** )malloc(100 * sizeof(char *))) == NULL)
{
printf("Nedostatek pameti.\n"); // not enough memory
exit(1);
}
while(!feof(stdin))
{
string[i] = get_line();
if((i > 1) && (*string[i-1] != strlen(*string[i])))
{
printf("Nespravny vstup.\n"); // error
exit(1);
}
printf("%s", string[i]);
i++;
if((i % 100) == 0)
{
if((* string = (char **)realloc(* string, 100 * ( i - 1 ) * sizeof(char *))) == NULL)
{
printf("Nedostatek pameti.\n"); // not enough memory
exit(1);
}
}
}
if(feof(stdin))
{
return string;
}
}
int search( char ** string1, char ** string2 ) // search
{
int string1width = strlen(*string1[0]);
int string2width = strlen(*string2[0]);
int string2height = strlen(**string2);
int number_of_lines = get_number_of_lines();
unsigned int g = 0, h = 0, i2, j2, l = 0, i = 0, j;
while( i <= (string2height - 2) )
{
j = 0;
while( j <= string2width - 2 )
{
g = 0; h = 0;
if(string2[i][j] == string1[g][h])
{
i2 = i;
while((g <= number_of_lines - 1) && (i2 <= string2height - 2))
{
j2 = j; h = 1;
while(((string2[i2][j2] == string1[g][h]) && (j2 <= string2height - 2)) && (h <= string1width - 2))
{
j2++;
h++;
}
if(h != string1width - 1)
{
break;
}
if(g == number_of_lines - 1)
{
l++;
break;
}
i2++;
g++;
}
}
j++;
}
i++;
}
return ( l );
}
int main(void)
{
char ** string1;
char ** string2;
int number_of_lines = get_number_of_lines();
string1 = get_wanted_g_matrix(number_of_lines);
string2 = get_searched_g_matrix();
if(feof(stdin))
{
printf("Matice nalezena %d krat.\n", search( ** string1, **string2 )); // G matrix found %d times.
}
return 0;
}
In this code:
char ** get_wanted_g_matrix(int pocetradek /*number of lines*/) // get wanted g matrix
{
char ** string;
printf("Zadejte hledanou matici:\n");
int i = 0;
if(( * string = (char ** )malloc(100 * sizeof(char *))) == NULL)
You dereference string
, but it is uninitialized, so you're writing to a random location in memory. Change that * string
to just string
. The same applies here:
if((* string = (char **)realloc(* string, 100 * ( i - 1 ) * sizeof(char *))) == NULL)
..and to the corresponding lines in get_searched_g_matrix()
as well.
In this line:
if((i > 1) && (*string[i-1] != strlen(*string[i])))
You're passing a char
to strlen()
, when you should be passing a char *
. I suspect you mean just strlen(string[i])
, but that line seems somewhat nonsensical. The same problem is in get_searched_g_matrix()
as well, as well as the first three calls to strlen()
in search()
.
Your get_searched_g_matrix()
can fall off the end without returning a value - you need to consider what to return if feof(stdin)
is not true.
In main()
, your call to search()
passes char
values, but the function expects char **
. You probably mean:
printf("Matice nalezena %d krat.\n", search( string1, string2 ));
(The above won't be sufficient to fix your code - you also appear to have some logic problems. But it's a necessary start.)
In future, you should compile your code with a higher level of warnings enabled, then fix the problems that the compiler identifies (if you're using gcc, compile with -Wall
).
Just some comments and style hints:
I'd say it's quite large for what it should do (and hard to follow), try simplifying it a bit.
1) Save vertical space, most people nowadays have wide screens, and it's quite annoing when you can't see the corresponding closing bracket.
1.1) It's very good that you check for error conditions, but try using something like
void err(const char* msg){
printf("\n\nFATAL ERROR: %s\n", msg);
exit(1);
};
so that you can do
if (!(x = malloc(sz)))
err("Not enough memory!");
1.2) While it's considered safer to use brackets for a signle statement in if
, I'd suggest avoiding them when possible, or at least use fewer newlines. Brackets are for compiler, people preffer tabs.
2) There are several while
statements in your search
function that should be written as for
s.
3) Why would you need two distinct functions to read the matrices? One would be enough.
4) As @caf pointed out, you have also errors in input functions. Test each function before going further. It takes years of experience before you can write the whole program at once.
精彩评论