c - C 中的内存错误,没有编译器错误

标签 c memory

我不明白为什么我在这里遇到内存错误。编译器在此代码中不会返回任何错误或警告:

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);
    char** result;
    int storage=0;
    int result_storage=0;

        for(int i = 0; i < len; i++){
        if((strcmp(bundesland, laender[i]) == 0) && (bewohner[i] >= anzahl)){
            storage++;
        }
    }
    result = (char**) malloc(storage);
    for (int j = 0; j < storage; j++){
        result[j] = (char*) malloc(100);
    }
    for(int i = 0; i < len; i++){
        if((strcmp(bundesland, laender[i]) == 0) && (bewohner[i] >= anzahl)){
            sprintf(result[result_storage], "Die Stadt %s hat %d Einwohner", staedte[i], bewohner[i]);
            result_storage++;
        }
    }
    len = storage;
    write_file(result, len);
    for(int p = 0; p < storage ; p++={
        free(result[p]);
    }
    free(result);
    return(0);
}

结果保存在 .txt 文件中。如果在启动程序时在 Notepad++ 中打开此文件,则 Notepad++ 想要重新加载该文件并崩溃。函数read_file和write_file位于文件input.c中:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "input3.h"

int MAX_LAENGE_STR = 255;
int MAX_LAENGE_ARR = 100;

void write_file(char *result[], int len) {
    FILE *fp = fopen("resultat.txt", "w");
    if (fp == NULL){
        perror("resultat.txt");
        exit(1);
    }
    for (int i=0; i<len; i++) {
        fprintf(fp, "%s\n", result[i]);
    }
    fclose(fp);
}

int read_file(char *dateiname, char laender[][MAX_LAENGE_STR], char staedte[][MAX_LAENGE_STR], int bewohner []) {
    FILE *fp = fopen(dateiname, "r");

    if (fp == NULL){
        perror(dateiname);
        exit(1);
    }

    char stadt[MAX_LAENGE_STR];
    char land[MAX_LAENGE_STR];
    int anzahl;
    int i = 0;
    int len;

    while(fscanf(fp, "\"%[^\"]\";\"%[^\"]\";%d\n", land, stadt, &anzahl) != EOF)
    {
        if (i >= MAX_LAENGE_ARR) {
            printf("ERROR: Die Datei ist größer als erwartet!");
            return i;
        }
        len = strlen(land) + 1;
        strncpy(laender[i], land, len-1);
        laender[i][len-1] = '\0';

        len = strlen(stadt) + 1;
        strncpy(staedte[i], stadt, len-1);
        staedte[i][len-1] = '\0';

        bewohner[i] = anzahl;
        i++;
    }
    fclose(fp);
    return i;
}

我无法更改 input.c 文件中的任何内容,并且“staedte.csv”文件的结构如下:

"Berlin";"Berlin";3460725
"Hamburg";"Hamburg";1786448
"München";"Bayern";1353186
"Köln";"Nordrhein-Westfalen";1007119
"Frankfurt am Main";"Hessen";679664
"Stuttgart";"Baden-Württemberg";606588
"Düsseldorf";"Nordrhein-Westfalen";588735
"Dortmund";"Nordrhein-Westfalen";580444
"Essen";"Nordrhein-Westfalen";574635
"Bremen";"Bremen";547340

最佳答案

关于这些行:

result = (char**) malloc(storage);
for (int j = 0; j < storage; j++)
{
    result[j] = (char*) malloc(100);
}

不要转换 malloc() 的返回值,因为返回值是 void * 类型,因此可以分配给任何指针变量,只会使代码困惑,导致调试或调试时感到头疼。进行维护。

始终检查(!=NULL)每次调用 malloc() 的返回值,以确保操作成功。

这一行:

result = (char**) malloc(storage);

正在分配“storage”字节,应该分配“storage*sizeof(result)”字节。

(注意:sizeof(result) 与 sizeof(char**) 相同

所以代码没有分配足够的存储空间。结果是这一行:

result[j] = (char*) malloc(100);

很快就会超出分配存储的末尾,这会破坏堆结构,从而导致未定义的行为,并且可能/将会导致段错误事件。

关于c - C 中的内存错误,没有编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33321382/

相关文章:

c - free()-ing char* 在 sscanf() 后停止工作”?

.net - "ContentPresenter.Content"内存泄漏

c - 程序运行时学习C scanf函数的问题(编译时没有错误)C编程绝对初学者指南第8章

c - ragel如何从文件中读取源代码?

编译器向已对齐的 union 位字段成员添加填充

c - main(int argc, char *argv[]) 对于 * 作为命令行参数给出奇怪的结果

c - getc(stdin) 后程序停止工作

c++ - C++中的字符串 vector 消耗的内存

android - 关于Android的内部存储和外部SD卡

swift - 快速,删除 SKScene 后如何使内存恢复正常?