简体   繁体   English

C:动态数组和指针数组创建分段错误

[英]C: Dynamic Array and Pointer array creates Segmentation fault

Hello i'm trying to create a program in C, which should read Values from another file and display them in another file with some exceptions.你好,我正在尝试用 C 创建一个程序,它应该从另一个文件中读取值并将它们显示在另一个文件中,但有一些例外。 The problem i'm getting is a segmentation fault which occurs when i'm trying to read a part of my result-array which is empty.我遇到的问题是分段错误,当我尝试读取空的结果数组的一部分时会发生该错误。 My For-loop scans a file for every line and if a line in this particular file matches my needs, it's values should get saved in an array.我的 For 循环为每一行扫描一个文件,如果这个特定文件中的一行符合我的需要,它的值应该保存在一个数组中。 This array should be printed in a 2nd .txt file.这个数组应该打印在第二个 .txt 文件中。 I wanted to printf some values of my array for testing purposes.我想为测试目的打印数组的一些值。 I guess it's a mistake in my arrays or pointers.我想这是我的数组或指针中的错误。

/* Die Konstanten:
 *  int MAX_LAENGE_STR - die maximale String Länge
 *  int MAX_LAENGE_ARR - die maximale Array Länge
 *  sind input3.c auf jeweils 255 und 100 definiert
 */
int main(int argc, char **argv) {
        if (argc < 3) {
            printf("Aufruf: %s <anzahl> <bundesland>\n", argv[0]);
            printf("Beispiel: %s 100 Bayern\n", argv[0]);
            printf("Klein-/Großschreibung beachten!\n");
            exit(1);
        }
        int anzahl = atoi(argv[1]);
        char *bundesland = argv[2];

// Statisch allokierter Speicher
char staedte[MAX_LAENGE_ARR][MAX_LAENGE_STR];
char laender[MAX_LAENGE_ARR][MAX_LAENGE_STR];
int bewohner[MAX_LAENGE_ARR];

int len = read_file("staedte.csv", staedte, laender, bewohner);

// Hier implementieren
int j;
char** result = (char *) malloc (MAX_LAENGE_ARR * sizeof(char));
if (result == NULL) {
    perror("malloc failed while allocating memory");
    exit(1);
    } 
for (int i = 0; i < len; i++) {
    if (strcmp(bundesland, laender[i]) == 0 && *bewohner > anzahl) {
        result[i] = malloc(MAX_LAENGE_STR * sizeof(char));
        if (result == NULL) {
            perror("malloc failed while allocating memory");
            exit(1);
        }
        snprintf(result[i], MAX_LAENGE_ARR, "Die Stadt %s hat %d Einwohner.", staedte[i], bewohner[i]);
        //printf("%s\n", result[i]);
    }
} 
printf("%s", result[0]);
// Mithilfe von write_file(...) soll das Ergebnis in die "resultat.txt"
// geschrieben werden. 
write_file(result, len);
// Dynamisch allozierter Speicher muss hier freigegeben werden.

} }

You are allocating to result incorrectly.您分配的result不正确。 You are allocating MAX_LAENGE_ARR*sizeof(char) bytes.您正在分配MAX_LAENGE_ARR*sizeof(char)字节。 You need to allocate MAX_LAENGE_ARR*sizeof(char *) bytes.您需要分配MAX_LAENGE_ARR*sizeof(char *)字节。 Also you are casting the return value of malloc to the wrong type.此外,您将malloc的返回值转换为错误的类型。 If you compile with warnings on, the compiler should have caught this error.如果你编译时打开警告,编译器应该已经发现了这个错误。 But, you don't need to cast the return value of malloc in C. Do I cast the result of malloc?但是,你不需要在 C 中转换malloc的返回值。 我是否转换了 malloc 的结果?

char** result = malloc (MAX_LAENGE_ARR * sizeof(*result));

Also, I think you need to replace MAX_LAENGE_ARR with MAX_LAENGE_STR in the following line:另外,我认为您需要在以下行中用MAX_LAENGE_STR替换MAX_LAENGE_ARR

snprintf(result[i], MAX_LAENGE_ARR, "Die Stadt %s hat %d Einwohner.", staedte[i], bewohner[i]);

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

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