c - "Stack around the variable was corrupted."

标签 c string memory-management dynamic-memory-allocation stack-corruption

我有一个应该从文件读取、创建数据结构并返回的函数。该函数可以工作,就在返回行之前,一切看起来都很好,并且结构看起来也不错。但是随后,该函数失败了 -

"Run-Time Check Failure #2 - Stack around the variable 'output' was corrupted."

该文件包含有关发电站和城市的信息。(输出、位置、名称等) 有些行是城市,有些是发电站,它们与行中最后一个整数不同(或缺少)。如果它存在(我们称他为X),这条线是一个发电站,接下来的X线是与其连接的城市。

这个函数。应该创建指向车站指针(车站**)的指针,其中所有城市都连接到每个车站。

station** read_from_file(FILE *file , station **power_grid){

    int output  , cities_connected ,i, counter = 0 ,j =0;
    double x , y;
    char name[256] = {0};
    station *st;


    while (fscanf(file, "%*c%[^\"]%*c%lf%lf%lf%d\n", name, &output, &x, &y, &cities_connected) != EOF){
        counter++;
        for( i = 0; i < cities_connected; i++){
            fscanf(file , "%*c%[^\"]%*c%lf%lf%lf\n" , name , &output , &x ,&y);
        }
    }

    power_grid = (station **)malloc(sizeof(station *)* counter);

    rewind(file);

    while (fscanf(file, "%*c%[^\"]%*c%lf%lf%lf%d\n", name, &output, &x, &y, &cities_connected) != EOF)
    {
        st = (station *)malloc(sizeof(station));
        st->capacity = output;
        st->cities_list = NULL;
        st->num_of_cities = cities_connected;
        st->name = (char *)malloc(strlen(name));
        strcpy(st->name , name);
        st->location[0] = x;
        st->location[1] = y;

        st->cities_list = (city **)malloc(sizeof(city *)*cities_connected);

        for( i = 0; i < cities_connected; i++){
            fscanf(file , "%*c%[^\"]%*c%lf%lf%lf\n" , name , &output , &x ,&y);
            st->cities_list[i] = (city *)malloc(sizeof(city));
            st->cities_list[i]->consumption = output;
            st->cities_list[i]->location[0] = x;
            st->cities_list[i]->location[1] = y;
            st->cities_list[i]->name = (char *)malloc(strlen(name)+1);
            strcpy(st->cities_list[i]->name , name);
        }
        power_grid[j] = st;
        j++;
    }

    fclose(file);
    return;

}

车站和城市结构-

typedef struct city {
    char * name;
    double location[2];
    double consumption; 
}city;

typedef struct station {
    char * name;
    double location[2];
    city ** cities_list;
    int num_of_cities;
    double capacity;    
}station; 

测试文件 - here

最佳答案

您的类型与此处不匹配:

fscanf(file, "%*c%[^\"]%*c%lf%lf%lf%d\n", name, &output, ...

因为 output 被声明为 int,而您使用 %lf。将其更改为 %d

此外,当您为 sr->name 分配内存时,您没有为 name 本身和 null 终止符分配足够的空间。

因此,更改此:

st->name = (char *)malloc(strlen(name));

对此:

st->name = malloc(strlen(name) + 1);

请注意,我没有强制转换 malloc() 返回的内容,您也不应该强制转换 ( Do I cast the result of malloc? )。

关于c - "Stack around the variable was corrupted.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46511827/

相关文章:

C语言: structs and character arrays

android - sadio register_driver() 会创 build 备名称吗?

python - 二进制到字符串,比字典好?

java - 截断内存映射文件

C - 在 Visual Studio 中按值传递

C: 在十六进制计算中添加自动前缀

php - 如何判断字符串是否被压缩?

c - 某些 C 实现是否定义了 string 关键字?

iphone - 释放内存分配的正确方法?

c# - 使用数据绑定(bind)和线程关闭 WPF 应用程序