realloc 读取文件时出错

戴维德·里佐·皮纳

我对这段代码有问题。有时它运行良好,但有时它会在最后一次打印之前停止并显示错误消息:“'./ga'中的错误:realloc():无效指针:0x00007f97d1304ac6”。

我快疯了,因为我没有使用 realloc()!

我怀疑文件reding部分有一些想法错误,因为当我将这部分添加到代码中时就会出现这个问题(之前我用其他两个数组设置数据)。

#include <limits> // std::numeric_limits<double>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <utility>
//#include <math.h>
#include <algorithm>    // std::lower_bound, std::find
#include <random>
#include <cmath> 
#include <cstring>
#include <iomanip>      // std::setprecision
#include <vector>       // std::vector

#define NUM_CITIES 14 

long size_pop;
long tot_elem;


int main(int argc, char *argv[]) {

    size_pop = atol(argv[1]);

    std::cout << size_pop << "\n";

    std::cout << "double: " << sizeof(double) << "\n";
    std::cout << "float: " << sizeof(float) << "\n";
    std::cout << "int: " << sizeof(int) << "\n";
    std::cout << "long: " << sizeof(long) << "\n";

    tot_elem = NUM_CITIES * size_pop;
    std::cout << "tot_elem: " << tot_elem << "\n";

    // std::cin.get() != '\n';

    struct timeval start, end, setup_start, setup_end, fitness_start, fitness_end, next_gen_start, next_gen_end, sort_start, sort_end;
    struct timeval fitness_total_start, fitness_total_end, probability_start, probability_end, selection_start, selection_end;
    struct timeval crossover_start, crossover_end, mutation_start, mutation_end;
    gettimeofday(&start, NULL);

    std::vector<double> v_set;
    std::vector<double> v_fit;
    std::vector<double> v_sor;
    std::vector<double> v_sel;
    std::vector<double> v_cros;
    std::vector<double> v_mut;

    // coordinate delle città
    // int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    // int y[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // città
    //city city_set[NUM_CITIES];
    int city_set_x[NUM_CITIES];
    int city_set_y[NUM_CITIES];
    int city_set_id[NUM_CITIES];

    // popolazione composta da path (possibii soluzioni al problema)
    int *city_x = (int *)malloc(tot_elem * sizeof(int));
    // memset(city_x, -1, tot_elem * sizeof(int));
    int *city_y = (int *)malloc(tot_elem * sizeof(int));
    // memset(city_y, -1, tot_elem * sizeof(int));
    int *city_id = (int *)malloc(tot_elem * sizeof(int));
    // memset(city_id, -1, tot_elem * sizeof(int));
    fit *fitness_element = (fit *)malloc(size_pop * sizeof(fit));

    // mating_pool, i migliori elementi della popolazione
    int *mating_x = (int *)malloc(NUM_CITIES * SIZE_MATING * sizeof(int));
    int *mating_y = (int *)malloc(NUM_CITIES * SIZE_MATING * sizeof(int));
    int *mating_id = (int *)malloc(NUM_CITIES * SIZE_MATING * sizeof(int));

    srand(time(NULL));

    // std::cin.get() != '\n';
    std::cout << "read from file\n";

    // leggo le coordinate delle città
    const char *filename = "/home/davide/Documenti/GA/BURMA14.txt";

    char *line;
    size_t n = 5;

    FILE *coordFile = fopen(filename, "r");
    //FILE *f = fopen("/home/davide/Documenti/GA/result.txt", "w");

    int i; // indice dell'array
    int x, y; // coordinate delle città

    while(getline(&line, &n, coordFile) != -1 && i <= NUM_CITIES)
    {
        int items = sscanf(line, "%d %d %d", &i, &x, &y);
        if(items != 3)
            exit(EXIT_FAILURE);
        --i;
        //std::cout << x << "\n";
        //std::cout << y << "\n";
        //std::cout << i << "\n";

        city_set_x[i] = x;
        city_set_y[i] = y;
        city_set_id[i] = i;
    }
    fclose(coordFile);

    // std::cin.get() != '\n';


    // stampa
    std::cout << "[CITTA.X]\n";
    for(int i = 0; i < NUM_CITIES; ++i) {

        // city_set_x[i] = x[i];
        // city_set[i].x = i + 1;
        std::cout << city_set_x[i] << " ";
    }
    std::cout << "\n";

    std::cout << "[CITTA.Y]\n";
    for(int i = 0; i < NUM_CITIES; ++i) {

        // city_set_y[i] = y[i];
        // city_set[i].y = i + 1;
        std::cout << city_set_y[i] << " ";
    }
    std::cout << "\n";

    std::cout << "[CITTA.ID]\n";
    for(int i = 0; i < NUM_CITIES; ++i) {

        // city_set_id[i] = i;
        std::cout << city_set_id[i] << " ";
    }
    std::cout << "\n";
}

我读到的文件是这个。

1 16 96
2 16 94
3 20 92
4 22 93
5 25 97
6 22 96
7 20 97
8 17 96
9 16 97
10 14 98
11 16 97
12 21 95
13 19 97
14 20 94
安德鲁·亨勒

这段代码是错误的:

char *line;
size_t n = 5;
...
int i; // indice dell'array
...
while(getline(&line, &n, coordFile) != -1 && i <= NUM_CITIES)
{
    int items = sscanf(line, "%d %d %d", &i, &x, &y);
...

line未初始化,这几乎肯定会导致报告的realloc()错误。

getline()标准

应用程序应确保这*lineptr是一个可以传递给free()函数的有效参数如果*n非零,应用程序应确保*lineptr要么指向一个大小至少为*n字节的对象,要么是一个null指针。

请注意,i它也没有被初始化,它的值直到循环开始sscanf()后立即调用才被设置while()但是iwhile-loop 的条件子句中使用,可能会导致循环控制问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在读取文件时使用realloc()

来自分类Dev

在读取文件时使用realloc()

来自分类Dev

使用realloc时程序崩溃

来自分类Dev

从文件读取时出错

来自分类Dev

在C中读取多个文件时,“ *** glibc检测到*** ./a.out:realloc():无效的下一个大小:”

来自分类Dev

读取Excel文件时出错

来自分类Dev

读取csv文件时出错

来自分类Dev

读取文件 C 时出错

来自分类Dev

指向结构时如何正确使用malloc()和realloc()?

来自分类Dev

为什么在使用realloc()时出现分段错误?

来自分类Dev

malloc'和realloc'指针导致返回时的内存泄漏

来自分类Dev

调用函数时,无效的free()/ delete / delete [] / realloc()错误

来自分类Dev

为什么在使用realloc()时出现分段错误?

来自分类Dev

C - 在结构内的指针上使用 Realloc 时崩溃

来自分类Dev

While 循环使用 realloc,打印时不会更新数组

来自分类Dev

从文件管道输入时,realloc无法扩展char数组

来自分类Dev

***输入'./a.out'时出错:realloc():下一个无效大小:0x00000000009c4010 ***

来自分类Dev

Realloc()行为

来自分类Dev

在AngularJS中读取JSON文件时出错

来自分类Dev

从Mac读取文件时输出错误

来自分类Dev

从Java中的文件读取时出错

来自分类Dev

使用C ++读取WAV文件时出错

来自分类Dev

读取配置文件时出错。MongoDB

来自分类Dev

在Golang中读取Excel文件时出错

来自分类Dev

尝试从网站读取Excel文件时出错

来自分类Dev

在R中读取CSV文件时出错

来自分类Dev

使用python读取csv文件时出错

来自分类Dev

从文件获取和读取数据时出错

来自分类Dev

使用C ++读取WAV文件时出错