Tehmo3
Tehmo3

Reputation: 61

Malloc statement giving segmentation fault 11

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);

Im seriously stuck for ideas after trying a bunch of stuff and searching stackoverflow! any info helps!

Thanks

EDIT:

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);

I get a segfault, have I missed a malloc again or similar?

Thanks

Upvotes: 1

Views: 270

Answers (2)

cagdas
cagdas

Reputation: 1644

Trying to access an element which is from NULL address space. Try this :

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

So on, you can fill your array like :

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

Upvotes: 0

wildplasser
wildplasser

Reputation: 44230

  • you don't need all these typedefs
  • you don't need to cast
  • sizeof *ptr gives the size of the object that ptr 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;
}

Upvotes: 2

Related Questions