C 程序问题 "pointer being freed was not allocated"

标签 c arrays pointers struct

我想我的 C 现在有点生疏了,因为我不太明白这里的问题。我很确定它位于 parse_historical_data() 中。如果我将其注释掉并运行 allocate_historical_data() 和 free_historical_data() 那么程序运行得很好,但是当我取消注释并运行整个程序时,我收到一条错误消息,提示“未分配正在释放的指针”。

struct historical_data {
    char **timestamp;
    float *close;
    float *high;
    float *low;
    float *open;
    int *volume;
    int count;
};

struct historical_data *h = (struct historical_data *) malloc(
         sizeof(struct historical_data));


void allocate_historical_data(struct historical_data *h,
         int days, int interval)
{
    int i;

    h->close = malloc(days * (60/interval) * 400 * sizeof(float));
    h->high = malloc(days * (60/interval) * 400 * sizeof(float));
    h->low = malloc(days * (60/interval) * 400 * sizeof(float));
    h->open = malloc(days * (60/interval) * 400 * sizeof(float));

    h->timestamp = (char **) malloc(days * (60/interval)
                               * 400 * sizeof(char *));

    for (i = 0; i < days * (60/interval) * 400; i++)
        h->timestamp[i] = malloc(25 * sizeof(char));

    h->volume = malloc(days * (60/interval) * 400 * sizeof(int));
}


void parse_historical_data(struct historical_data *h, char *raw_data)
{
    char *csv_value;
    int i = 0;

    csv_value = strtok(raw_data, ",");
    h->timestamp[i] = csv_value;

    while (csv_value != NULL) {
        csv_value = strtok(NULL, ",");
        h->open[i] = atof(csv_value);

        csv_value = strtok(NULL, ",");
        h->high[i] = atof(csv_value);

        csv_value = strtok(NULL, ",");
        h->low[i] = atof(csv_value);

        csv_value = strtok(NULL, ",");
        h->close[i] = atof(csv_value);

        csv_value = strtok(NULL, "\n");
        h->volume[i] = atoi(csv_value);

        if (h->volume[i] == 0) // Junk data.
            i--;

        i++;

        csv_value = strtok(NULL, ",");
        h->timestamp[i] = csv_value;
    } 

    h->count = i;
}


void free_historical_data(struct historical_data *h, int days, int interval)
{
    int i;

    free(h->close);
    free(h->high);
    free(h->low);
    free(h->open);

    for (i = 0; i < days * (60/interval) * 400; i++)
        free(h->timestamp[i]);

    free(h->timestamp);
    free(h->volume);

    free(h);
}

最佳答案

我认为问题在于 h->timestamp

parse_historical_data()

csv_value = strtok(raw_data, ",");
h->timestamp[i] = csv_value;
...
...
    csv_value = strtok(NULL, ",");
    h->timestamp[i] = csv_value;

h->timestamp[i] 未分配分配的字符串/指针。相反,它只是指向相同的字符串 - char * 但来自不同的索引。

你可能也想改变它

strcpy(h->timestamp[i], csv_value); //as you have already allocated for it

关于C 程序问题 "pointer being freed was not allocated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17894985/

相关文章:

c - 在 C 中更改 void 指针值的问题

c - 在线程内打印参数

c - 如何在 C++ 中创建自己的头文件?

c - C中用于获取字符串的指针算法

python - 获取 numpy 数组中第 k 个维度的第 i 个切片

C 指针和 malloc 混淆 EXC_BAD_ACCESS

c - 了解如何将参数传递给 Assembly 中的函数

javascript - 下划线 - 使用 _.map() 转换数组

php - 使用 StdClass 对象为多维数组创建 foreach 循环时遇到问题

c - 差异 : &ary1D[0] and &ary1D