当我尝试通过这些结构使用这些malloc语句时(这些语句并非在我的实际代码中排成一排,而是内置于根据需要分配malloc / reallocs的函数中),我认为我的问题出在这些语句之内,所以我只包括这些,因为我相信我当前没有正确分配内存,以便将内存存储在数组数据内的word_data_t结构中,而不是索引类型为index_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);
}
我得到一个
seg fault
,我似乎无法弄清楚为什么,我期望发生的是第二次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
一种。您输入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) {
您正在使用
index_data_array
计算数组current_size_outer_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是您的朋友。
这是更改后的代码,只有功能测试,其余保持不变:
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);
}