Christian Steuer
Christian Steuer

Reputation: 139

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. 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. This array should be printed in a 2nd .txt file. 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.

}

Upvotes: 1

Views: 81

Answers (1)

MFisherKDX
MFisherKDX

Reputation: 2866

You are allocating to result incorrectly. You are allocating MAX_LAENGE_ARR*sizeof(char) bytes. You need to allocate MAX_LAENGE_ARR*sizeof(char *) bytes. Also you are casting the return value of malloc to the wrong type. 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?

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:

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

Upvotes: 1

Related Questions