【问题标题】:Malloc statement giving segmentation fault 11Malloc 语句给出分段错误 11
【发布时间】:2017-02-13 19:21:13
【问题描述】:

对于一个我正在学习在 c 中使用 malloc/realloc 的项目,但我无法弄清楚为什么这段代码会给我一个 seg 错误!

#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 之后,我被严重卡住了!任何信息都有帮助!

谢谢

编辑:

感谢你们的帮助!但是我在程序的后面仍然遇到了段错误,可能是因为类似的错误,但是我尝试了很多东西并不能让它工作,这是我所有的 malloc/realloc 调用:

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;

然后当我稍后去打印一些东西时:

printf("%d\n", index_data->index_data_array[0].total_docs);

我遇到了段错误,我是否再次错过了 malloc 或类似情况?

谢谢

【问题讨论】:

  • index_data_t *index_data=NULL; index_data-&gt;index_data_array = ... 所以NULL-&gt;index_data_array
  • malloc() 极不可能导致段错误。这里的问题是你取消引用index_data,这是一个空指针
  • 在你的 sizeof() 中,你试图访问 index_data,它在前面的语句中被标记为 NULL。
  • @hashdefine sizeof 的参数未计算,因此允许在其中取消引用 null

标签: c segmentation-fault malloc


【解决方案1】:

试图访问来自 NULL 地址空间的元素。 试试这个:

index_data = (index_data_t*)malloc(sizeof(*(index_data)));

等等,你可以像这样填充你的数组:

index_data->index_data_array = (data_t*)malloc(sizeof(*(index_data->index_data_array))*INITIAL_ALLOCATION);

【讨论】:

  • 对,有道理,但我现在有点困惑,我明白我已经将它设为空指针,这是否意味着在这个 malloc 语句之后我现在不能使用我拥有 malloc 的数组' d?我不应该将它转换为空指针吗?或者如果没有,我现在怎么去添加一些东西说 index_data->index_data_array.word?
  • 可能是 sizeof(index_data_t) 或 sizeof(struct index_data),不是吗?编辑:哦,我错了。我错过了您声明了 index_data_t 类型的 index_data 变量。然后没关系,虽然将结构和变量命名为相同有点混乱
  • @sharcashmo 你不必提及类型。
  • 我知道,我在一分钟前编辑了我的评论。我错过了 index_data 是变量的名称:D
  • 我知道答案有点混乱,但我不喜欢修改太多以按照@Tehmo3 的方式进行。
【解决方案2】:
  • 您不需要所有这些类型定义
  • 你不需要投射
  • sizeof *ptr 给出了ptr 指向的对象的大小
  • 恕我直言,没有 casts&typedefs 的代码更清晰:

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多