Malloc语句给出分段错误11 [英] Malloc statement giving segmentation fault 11
问题描述
对于一个项目,我正在学习在c中使用malloc/realloc,但是我无法弄清楚为什么这段代码给我一个段错误!
For a project I am learning to use malloc/realloc in c, but I cannot figure out why this code gives me a seg fault!
#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;
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;
};
/* Inside a function called from main */
index_data_t *index_data=NULL;
index_data->index_data_array = (data_t*)malloc(sizeof(*(index_data- >index_data_array))*INITIAL_ALLOCATION);
尝试了很多东西并搜索stackoverflow之后,我被想法严重卡住了!任何信息都可以帮助!
Im seriously stuck for ideas after trying a bunch of stuff and searching stackoverflow! any info helps!
谢谢
感谢您的帮助!但是我稍后在程序中仍然会遇到段错误,可能是由于类似的错误所致,但是我尝试了很多东西并无法使其正常工作,这是我所有的malloc/realloc调用:
Thanks for your help guys! but im still running into a seg fault later in the program, probably because of a similar mistake, but I've tried a bunch of stuff and cant get it to work, here are all my malloc/realloc calls:
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);
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(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);
index_data->index_data_array[index].total_docs=atoi(word);
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;
然后,当我稍后再打印出一些内容时:
then when I go to print out something later:
printf("%d\n", index_data->index_data_array[0].total_docs);
我遇到了段错误,是否再次错过了malloc或类似的问题?
I get a segfault, have I missed a malloc again or similar?
谢谢
推荐答案
- 您不需要所有这些typedefs
- 您不需要投射
-
sizeof *ptr
给出> 指向 的对象的大小
- 恕我直言,没有强制转换和类型定义的代码要清晰得多:
- you don't need all these typedefs
- you don't need to cast
sizeof *ptr
gives the size of the object thatptr
points to- IMHO the code without casts&typedefs is much clearer:
#include <stdlib.h>
struct thing {
int type;
char name[13];
};
struct box {
unsigned nthing;
struct thing *things;
};
struct box *make_box(unsigned thingcount)
{
struct box *thebox;
thebox = malloc (sizeof *thebox);
if (!thebox) return NULL;
thebox->things = malloc (thingcount * sizeof *thebox->things);
if (!thebox->things) { free(thebox); return NULL; }
thebox->nthing = thingcount;
return thebox;
}
这篇关于Malloc语句给出分段错误11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!