简体   繁体   English

分配-指针到指针

[英]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. 我想寻求分配方面的帮助....我把这本功课上学了...我必须编写程序,该程序将加载一个G矩阵和第二个G矩阵,并在第二个G矩阵中搜索第一个G矩阵的存在数量G矩阵...。但是,当我尝试运行我的程序时,出现了分段错误消息...在此先感谢。 Example how the program is supposed to work.... 该程序应如何工作的示例...

... ...

Enter number of lines of wanted g matrix: 3 Enter the wanted g matrix: 输入所需g矩阵的行数:3输入所需g矩阵:
121212 121212
212121 212121
121212 121212
G matrix to be searched: 要搜索的G矩阵:
12121212121212 12121212121212
21212121212121 21212121212121
12121212123212 12121212123212
21212121212121 21212121212121
12121212121212 12121212121212
G matrix found 8 times. G矩阵发现8次。

... ...

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. 您可以取消引用string ,但是它尚未初始化,因此您正在写入内存中的随机位置。 Change that * string to just string . 将该* string更改为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. ..以及get_searched_g_matrix()的相应行。

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 * . 你传递一个charstrlen()当你要传递char * I suspect you mean just strlen(string[i]) , but that line seems somewhat nonsensical. 我怀疑您的意思只是strlen(string[i]) ,但是那条线似乎有些荒谬。 The same problem is in get_searched_g_matrix() as well, as well as the first three calls to strlen() in search() . 同样的问题也在get_searched_g_matrix() ,以及在search()strlen()的前三个调用中。

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. 您的get_searched_g_matrix()可能会在没有返回值的情况下get_searched_g_matrix() -如果feof(stdin)不为真,则需要考虑返回什么。

In main() , your call to search() passes char values, but the function expects char ** . main() ,对search()调用传递了char值,但是该函数期望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 ). 将来,您应该在启用更高级别的警告的情况下编译代码,然后解决编译器识别出的问题(如果使用的是gcc,请使用-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.1) It's very good that you check for error conditions, but try using something like 1.1)检查错误情况非常好,但是请尝试使用类似

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. 1.2)虽然在if使用带括号的signle语句被认为更安全,但我建议尽可能避免使用括号,或者至少使用较少的换行符。 Brackets are for compiler, people preffer tabs. 括号用于编译器,人们喜欢使用制表符。

2) There are several while statements in your search function that should be written as for s. 2)有几种while在你的语句search功能应该写成for秒。

3) Why would you need two distinct functions to read the matrices? 3)为什么您需要两个不同的函数来读取矩阵? One would be enough. 一个就足够了。

4) As @caf pointed out, you have also errors in input functions. 4)正如@caf指出的那样,您在输入函数中也有错误。 Test each function before going further. 在进一步测试之前,请测试每个功能。 It takes years of experience before you can write the whole program at once. 您需要多年的经验才能立即编写整个程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM