gpt4 book ai didi

c - 段错误 - 错误分配?

转载 作者:行者123 更新时间:2023-11-30 20:35:17 24 4
gpt4 key购买 nike

当我尝试使用这些结构来使用这些 malloc 语句时(这些语句在我的实际代码中并不都是连续的,而是内置到根据需要进行 malloc/reallocs 的函数中),我相信我的问题出在这些语句中,所以我只包含了这些,因为我相信我目前没有正确分配内存,以便将内存存储在数组数据中的 word_data_t 结构中,数据存储在类型为 data_t 的结构中,存储在索引_data_t 类型的结构中的数组中:

编辑:添加了可编译代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef struct word_data word_data_t;
typedef struct data data_t;
typedef struct index_data index_data_t;
#define INITIAL_ALLOCATION 2

void test();

struct word_data {

int docunumber;
int freq;
};

struct data {
char *word;
int total_docs;
word_data_t *data;
};

struct index_data {
data_t *index_data_array;
};



int main(int argc, char const *argv[]) {
test();
return 0;
}

void test() {
/* Inside a function called from main */
char entered_word[4] = "wow";
int docunum = 4, index=0, freq=6, current_size_outer_array=0, current_size_inner_array=0;
int total_docs_in=1, doc_freq_pairs=1;
index_data_t *index_data=NULL;
for (index=0; index<4; index++) {
index_data = (index_data_t*)malloc(sizeof(*index_data)*INITIAL_ALLOCATION);
index_data->index_data_array = (data_t*)malloc(sizeof(*index_data->index_data_array)*INITIAL_ALLOCATION);
current_size_outer_array = INITIAL_ALLOCATION;
if (index == 2) {
index_data->index_data_array = realloc(index_data->index_data_array, current_size_outer_array*sizeof(*(index_data->index_data_array)));
}
index_data->index_data_array[index].word=malloc(strlen(entered_word)+1);
index_data->index_data_array[index].word=entered_word;
index_data->index_data_array[index].data = (word_data_t *)malloc(sizeof(word_data_t)*INITIAL_ALLOCATION);
current_size_inner_array = INITIAL_ALLOCATION;
index_data->index_data_array[index].total_docs=total_docs_in;
if (/* Need more data points */ doc_freq_pairs<2) {
index_data->index_data_array[index].data = realloc(index_data->index_data_array[index].data, current_size_inner_array*(sizeof(*(index_data->index_data_array[index].data))));
}
index_data->index_data_array[index].data->docunumber = docunum;
index_data->index_data_array[index].data->freq = freq;
}
printf("%d\n", index_data->index_data_array[0].total_docs);
printf("%s\n", index_data->index_data_array[1].word);
printf("%d\n", index_data->index_data_array[1].data->freq);
}

我遇到了段错误,我似乎无法弄清楚为什么,我期望发生的是第二个malloc调用为index_data->index_data_array[0]创建空间[1],但也许我需要以另一种方式为它们留出内存?这个 malloc 的东西让我很困惑。

谢谢!

最佳答案

已经在评论中提到了这一点,只是为了完整性而在此处声明:您不应该从 *alloc 转换返回值!另外:您应该检查 *alloc 中的所有 retvals 是否为 NULL。特别是在 realloc 的情况下,这意味着: void * tmp = realloc(old_ptr, new_size); if (!tmp) { 错误处理 } else { old_ptr = tmp; }

现在,让我们来讨论几个问题:

==14011==    definitely lost: 96 bytes in 9 blocks
==14011== indirectly lost: 176 bytes in 5 blocks

a.您输入 for 循环,然后在其中在每次迭代中初始化 index_data!也许,第一个 malloc 应该在 for 循环之外(这样可以消除前 48 字节的内存泄漏)。

==14127==    definitely lost: 192 bytes in 9 blocks
==14127== indirectly lost: 32 bytes in 2 blocks

此外,index_data->index_data_array 的第一次初始化也应该在 for 循环之前完成。另外 80 字节的内存泄漏消失了。

==14163==    definitely lost: 64 bytes in 7 blocks
==14163== indirectly lost: 80 bytes in 3 blocks

b.为什么?:

if (index == 2) {

您正在使用 current_size_outer_array 计算数组 index_data_array 中的元素数量。因此,请使用它来检查是否还有足够的空间。

if (index == current_size_outer_array) {
}

此外,不要再次使用该值重新分配,而是之前增加它。

if (index == current_size_outer_array) {
current_size_outer_array *= 2;
}

并使用正确的 sizeof (与上面的初始 malloc 调用相同)

if (index == current_size_outer_array) {
current_size_outer_array *= 2;
void * tmp = realloc(index_data->index_data_array, sizeof(*index_data->index_data_array)*current_size_outer_array);
if (!tmp) exit(2);
index_data->index_data_array=tmp;
}

还有...

1
wow
6

中提琴

所以,现在这段代码仍然在泄漏内存。为了解决这个问题,您必须进行一些 free() 调用。

哦,你想知道我是如何发现内存泄漏的:valgrind 是你的 friend 。

这里是修改后的代码,仅进行功能测试,其余不变:

void test() {
/* Inside a function called from main */
char entered_word[4] = "wow";
int docunum = 4, index=0, freq=6, current_size_outer_array=0, current_size_inner_array=0;
int total_docs_in=1, doc_freq_pairs=1;
index_data_t *index_data=NULL;
index_data = malloc(sizeof(*index_data)*INITIAL_ALLOCATION);
index_data->index_data_array = malloc(sizeof(*index_data->index_data_array)*INITIAL_ALLOCATION);
current_size_outer_array = INITIAL_ALLOCATION;
for (index=0; index<4; index++) {
if (index == current_size_outer_array) {
current_size_outer_array *= 2;
void * tmp = realloc(index_data->index_data_array, sizeof(*index_data->index_data_array)*current_size_outer_array);
if (!tmp) exit(2);
index_data->index_data_array=tmp;
}
index_data->index_data_array[index].word=malloc(strlen(entered_word)+1);
index_data->index_data_array[index].word=entered_word;
index_data->index_data_array[index].data = (word_data_t *)malloc(sizeof(word_data_t)*INITIAL_ALLOCATION);
current_size_inner_array = INITIAL_ALLOCATION;
index_data->index_data_array[index].total_docs=total_docs_in;
if (/* Need more data points */ doc_freq_pairs<2) {
index_data->index_data_array[index].data = realloc(index_data->index_data_array[index].data, current_size_inner_array*(sizeof(*(index_data->index_data_array[index].data))));
}
index_data->index_data_array[index].data->docunumber = docunum;
index_data->index_data_array[index].data->freq = freq;
}
printf("%d\n", index_data->index_data_array[0].total_docs);
printf("%s\n", index_data->index_data_array[1].word);
printf("%d\n", index_data->index_data_array[1].data->freq);
}

关于c - 段错误 - 错误分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39891467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com