0

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

Tehmo3
  • 51
  • 6
  • 5
    `index_data_t *index_data=NULL; index_data->index_data_array = ...` so `NULL->index_data_array` – BLUEPIXY Oct 05 '16 at 11:15
  • 1
    `malloc()` is highly unlikely to cause a segfault. The problem here is that you're dereferencing `index_data`, which is a null pointer – r3mainer Oct 05 '16 at 11:15
  • Inside your sizeof() you are trying to access index_data, which is marked as NULL in there previous statement. – hashdefine Oct 05 '16 at 11:15
  • 2
    @hashdefine the argument of `sizeof` is unevaluated, so it is permitted to dereference null inside it – M.M Oct 05 '16 at 12:14

2 Answers2

2
  • 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;
}
wildplasser
  • 43,142
  • 8
  • 66
  • 109
0

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);
cagdas
  • 1,634
  • 2
  • 14
  • 27
  • right, makes sense, but I'm a little confused now I understand that I've made it a null pointer, does that mean after this malloc statement I now cant use the array I have malloc'd? Should I just not cast this to a null pointer? Or if not, how do I now go add something to say index_data->index_data_array.word? – Tehmo3 Oct 05 '16 at 11:33
  • it could be sizeof(index_data_t) or sizeof(struct index_data), isn't it? Edit: Oh, I'm wrong. I had missed that you declared an index_data variable of type index_data_t. Then it's Ok although naming both the struct and the variable the same is a bit confusing – sharcashmo Oct 05 '16 at 11:38
  • 1
    @sharcashmo you do not have to mention the type. – cagdas Oct 05 '16 at 11:41
  • I know, I edited my comment a minute ago. I missed that index_data was the name of the variable :D – sharcashmo Oct 05 '16 at 11:42
  • I know the answer get a lil bit confusing but I do not like to modify too much to do it in @Tehmo3's fashion. – cagdas Oct 05 '16 at 11:44
  • @cadgas Thanks for your help cagdas, I really am confused here, how would I access these sub-struct elements to give them data? doing it as I normally would (index_data->index_data_array->etc) doesn't work since its a null pointer? – Tehmo3 Oct 05 '16 at 11:49
  • For sub-struct elements you'll also need them to be filled via malloc. Check this post : http://stackoverflow.com/questions/14768230/malloc-for-struct-and-pointer-in-c – cagdas Oct 05 '16 at 12:04
  • @cagdas thanks! I think I understand that part but I seem to still be getting an error... I've updated my post with some new info if you could take a look I would be very thankful! – Tehmo3 Oct 05 '16 at 23:46